jquery.mjs.nestedSortable.js
4 years ago
jquery.ui.touch-punch.min.js
11 years ago
nestedpages-factory.js
3 years ago
nestedpages.bulk-actions.js
4 years ago
nestedpages.check-all.js
9 years ago
nestedpages.clone.js
3 years ago
nestedpages.confirm-delete.js
8 years ago
nestedpages.dropdowns.js
8 years ago
nestedpages.formatter.js
3 years ago
nestedpages.hidden-item-count.js
9 years ago
nestedpages.manual-sync.js
9 years ago
nestedpages.menu-links.js
3 years ago
nestedpages.menu-search.js
4 days ago
nestedpages.menu-toggle.js
8 years ago
nestedpages.modals.js
8 years ago
nestedpages.move-post.js
8 years ago
nestedpages.nesting.js
3 years ago
nestedpages.new-post.js
6 years ago
nestedpages.page-toggle.js
4 years ago
nestedpages.post-search.js
9 years ago
nestedpages.quickedit-link.js
8 years ago
nestedpages.quickedit-post.js
3 years ago
nestedpages.settings-admin-customization.js
6 years ago
nestedpages.settings-reset.js
6 years ago
nestedpages.settings.js
3 years ago
nestedpages.sync-menu-setting.js
8 years ago
nestedpages.tabs.js
6 years ago
nestedpages.trash-with-children.js
6 years ago
nestedpages.trash.js
8 years ago
nestedpages.userprefs-reset.js
7 years ago
nestedpages.wpml.js
8 years ago
nestedpages.new-post.js
357 lines
| 1 | var NestedPages = NestedPages || {}; |
| 2 | |
| 3 | /** |
| 4 | * Add new post(s) - Top level & child |
| 5 | * @package Nested Pages |
| 6 | * @author Kyle Phillips - https://github.com/kylephillips/wp-nested-pages |
| 7 | */ |
| 8 | NestedPages.NewPost = function() |
| 9 | { |
| 10 | var plugin = this; |
| 11 | var $ = jQuery; |
| 12 | |
| 13 | plugin.formatter = new NestedPages.Formatter; |
| 14 | plugin.parent_id = 0; // Parent ID for the post(s) to add |
| 15 | plugin.posts = ''; // The newly added posts |
| 16 | plugin.form = ''; // The active form |
| 17 | |
| 18 | plugin.bindEvents = function() |
| 19 | { |
| 20 | $(document).on('open-modal', function(e, button, modal){ |
| 21 | var target = $(button).attr('data-nestedpages-modal-toggle'); |
| 22 | if ( typeof target !== 'undefined' && target == 'np-bulk-modal' ){ |
| 23 | plugin.openModal(); |
| 24 | } |
| 25 | }); |
| 26 | $(document).on('submit', NestedPages.selectors.newPageForm, function(e){ |
| 27 | e.preventDefault(); |
| 28 | }); |
| 29 | $(document).on('click', NestedPages.selectors.newPageSubmitButton, function(e){ |
| 30 | e.preventDefault(); |
| 31 | plugin.submitForm($(this)); |
| 32 | }); |
| 33 | $(document).on('click', NestedPages.selectors.newPageTitle, function(e){ |
| 34 | e.preventDefault(); |
| 35 | plugin.addTitleField($(this)); |
| 36 | }); |
| 37 | $(document).on('click', NestedPages.selectors.newPageRemoveTitle, function(e){ |
| 38 | e.preventDefault(); |
| 39 | plugin.removeTitleField($(this)); |
| 40 | }); |
| 41 | $(document).on('click', NestedPages.selectors.addChildButton, function(e){ |
| 42 | e.preventDefault(); |
| 43 | plugin.openQuickEdit($(this)); |
| 44 | }); |
| 45 | $(document).on('click', NestedPages.selectors.cancelNewChildButton, function(e){ |
| 46 | e.preventDefault(); |
| 47 | plugin.cancelNewPage(); |
| 48 | }); |
| 49 | $(document).on('click', '[' + NestedPages.selectors.newBeforeButton + ']', function(e){ |
| 50 | e.preventDefault(); |
| 51 | plugin.openQuickEdit($(this)); |
| 52 | }); |
| 53 | $(document).on('click', '[' + NestedPages.selectors.newAfterButton + ']', function(e){ |
| 54 | e.preventDefault(); |
| 55 | plugin.openQuickEdit($(this)); |
| 56 | }); |
| 57 | $(document).on('keydown', function(e){ |
| 58 | if ( e.keyCode === 27 ) { |
| 59 | plugin.cancelNewPage(); |
| 60 | $(document).click(); // Close Dropdown |
| 61 | } |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | // Open the form modal |
| 66 | plugin.openModal = function() |
| 67 | { |
| 68 | var newform = $(NestedPages.selectors.newPageFormContainer).clone().find(NestedPages.selectors.newPageForm); |
| 69 | $(newform).addClass('in-modal'); |
| 70 | $(NestedPages.selectors.newPageModal).find('.modal-body').html(newform); |
| 71 | $(NestedPages.selectors.newPageModal).find('h3').text(nestedpages.add_multiple); |
| 72 | $(NestedPages.selectors.newPageModal).find('.page_parent_id').val(plugin.parent_id); |
| 73 | $(newform).find('.np_title').first().focus(); |
| 74 | $(newform).find(NestedPages.selectors.newPageTitle).first().prop('tabindex', '2'); |
| 75 | } |
| 76 | |
| 77 | // Open the new child quick edit |
| 78 | plugin.openQuickEdit = function(button) |
| 79 | { |
| 80 | var before = $(button).attr(NestedPages.selectors.newBeforeButton); |
| 81 | before = ( typeof before === 'undefined' || before === '' ) ? false : before; |
| 82 | |
| 83 | var after = $(button).attr(NestedPages.selectors.newAfterButton); |
| 84 | after = ( typeof after === 'undefined' || after === '' ) ? false : after; |
| 85 | |
| 86 | var parent_li = $(button).closest(NestedPages.selectors.row).parent('li'); |
| 87 | var newform = $(NestedPages.selectors.newPageFormContainer).clone(); |
| 88 | |
| 89 | // Append the form to the list item |
| 90 | if ( $(parent_li).children('ol').length > 0 ){ |
| 91 | var child_ol = $(parent_li).children('ol'); |
| 92 | $(newform).insertBefore(child_ol); |
| 93 | } else { |
| 94 | $(newform).appendTo(parent_li); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | $(newform).siblings(NestedPages.selectors.row).hide(); |
| 99 | |
| 100 | plugin.formatter.showQuickEdit(); |
| 101 | |
| 102 | $(newform).find('.parent_name').html('<em>Parent:</em> ' + $(button).attr('data-parentname')); |
| 103 | if ( !before && !after ) $(newform).find('.page_parent_id').val($(button).attr('data-id')); |
| 104 | |
| 105 | if ( before ) { |
| 106 | $(newform).find('.page_before_id').val(before); |
| 107 | $(newform).find('[data-new-post-relation-title]').text(nestedpages.insert_before + ': ' + $(button).attr('data-parentname')); |
| 108 | } |
| 109 | if ( after ) { |
| 110 | $(newform).find('.page_after_id').val(after); |
| 111 | $(newform).find('[data-new-post-relation-title]').text(nestedpages.insert_after + ': ' + $(button).attr('data-parentname')); |
| 112 | } |
| 113 | |
| 114 | $(newform).show(); |
| 115 | $(newform).find('.np_title').focus(); |
| 116 | $(newform).find(NestedPages.selectors.newPageTitle).prop('tabindex', '2'); |
| 117 | } |
| 118 | |
| 119 | // Close the form modal |
| 120 | plugin.cancelNewPage = function() |
| 121 | { |
| 122 | plugin.formatter.removeQuickEdit(); |
| 123 | $(NestedPages.selectors.newChildError).hide(); |
| 124 | $(NestedPages.selectors.newPageModal).find('.modal-body').empty(); |
| 125 | $(NestedPages.selectors.sortable).find('.new-child').remove(); |
| 126 | $(NestedPages.selectors.row).show(); |
| 127 | } |
| 128 | |
| 129 | // Add a page title field to the form |
| 130 | plugin.addTitleField = function(button) |
| 131 | { |
| 132 | var form = $(button).parents('form'); |
| 133 | var fieldcount = $(button).siblings('.new-page-titles').children('li').length + 1; |
| 134 | var html = '<li><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="handle np-icon-menu"><path d="M0 0h24v24H0z" fill="none" /><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z" class="bars" /></svg><div class="form-control new-child-row"><label>' + NestedPages.jsData.titleText + '</label><div><input type="text" name="post_title[]" class="np_title" placeholder="' + NestedPages.jsData.titleText + '" value="" tabindex="' + fieldcount + '" /><a href="#" class="button-secondary np-remove-child">-</a></div></div></li>'; |
| 135 | var container = $(button).siblings('.new-page-titles').append(html); |
| 136 | $(form).find('.np_title').last().focus(); |
| 137 | $(form).find(NestedPages.selectors.newPageTitle).prop('tabindex', fieldcount++); |
| 138 | $('.new-page-titles').sortable({ |
| 139 | items : 'li', |
| 140 | handle: '.handle', |
| 141 | }); |
| 142 | plugin.toggleAddEditButton(form); |
| 143 | } |
| 144 | |
| 145 | // Remove a page title field |
| 146 | plugin.removeTitleField = function(button) |
| 147 | { |
| 148 | var form = $(button).parents('form'); |
| 149 | $(button).parents('.new-child-row').parent('li').remove(); |
| 150 | plugin.toggleAddEditButton(form); |
| 151 | } |
| 152 | |
| 153 | // Submit the New Page Form |
| 154 | plugin.submitForm = function(button) |
| 155 | { |
| 156 | plugin.toggleLoading(true); |
| 157 | plugin.form = $(button).parents('form'); |
| 158 | |
| 159 | var addedit = ( $(button).hasClass('add-edit') ) ? true : false; |
| 160 | var action = NestedPages.formActions.newPage; |
| 161 | if ( $(plugin.form).find('.page_before_id').val() !== '' ) action = NestedPages.formActions.newBeforeAfter; |
| 162 | if ( $(plugin.form).find('.page_after_id').val() !== '' ) action = NestedPages.formActions.newBeforeAfter; |
| 163 | |
| 164 | $.ajax({ |
| 165 | url: NestedPages.jsData.ajaxurl, |
| 166 | type: 'post', |
| 167 | datatype: 'json', |
| 168 | data: $(plugin.form).serialize() + '&action=' + action + '&nonce=' + NestedPages.jsData.nonce + '&syncmenu=' + NestedPages.jsData.syncmenu + '&post_type=' + NestedPages.jsData.posttype, |
| 169 | success: function(data){ |
| 170 | if (data.status === 'error'){ |
| 171 | plugin.toggleLoading(false); |
| 172 | $(plugin.form).find(NestedPages.selectors.quickEditErrorDiv).text(data.message).show(); |
| 173 | return; |
| 174 | } |
| 175 | if ( addedit === true ){ // Redirect to Edit Screen |
| 176 | var link = data.new_pages[0].edit_link; |
| 177 | link = link.replace(/&/g, '&'); |
| 178 | window.location.replace(link); |
| 179 | return; |
| 180 | } |
| 181 | plugin.toggleLoading(false); |
| 182 | plugin.posts = data.new_pages; |
| 183 | plugin.addPosts(); |
| 184 | }, |
| 185 | error: function(data){ |
| 186 | console.log(data); |
| 187 | plugin.toggleLoading(false); |
| 188 | $(plugin.form).find(NestedPages.selectors.quickEditErrorDiv).text('The form could not be saved at this time.').show(); |
| 189 | } |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | // Add the new posts |
| 194 | plugin.addPosts = function() |
| 195 | { |
| 196 | // Before/After ID if applicable |
| 197 | var before = $(plugin.form).find('.page_before_id').val(); |
| 198 | before = ( before !== '' ) ? before : false; |
| 199 | var after = $(plugin.form).find('.page_after_id').val(); |
| 200 | after = ( after !== '' ) ? after : false; |
| 201 | |
| 202 | var parent_li = $(plugin.form).parent('.new-child').parent('.page-row'); |
| 203 | |
| 204 | // If parent li doesn't have a child ol, add one |
| 205 | if ( $(parent_li).children('ol').length === 0 && !before && !after ){ |
| 206 | $(parent_li).append('<ol class="nplist"></ol>'); |
| 207 | } |
| 208 | |
| 209 | if ( $(plugin.form).hasClass('in-modal') ){ |
| 210 | var appendto = $('.nplist.sortable li.page-row:first'); |
| 211 | } else { |
| 212 | var appendto = $(parent_li).children('ol'); |
| 213 | } |
| 214 | |
| 215 | for (i = 0; i < plugin.posts.length; i++){ |
| 216 | plugin.appendRows(appendto, plugin.posts[i], before, after); |
| 217 | } |
| 218 | |
| 219 | // Show the child page list and reset submenu toggles |
| 220 | if ( !before && !after ){ |
| 221 | $(appendto).show(); |
| 222 | } |
| 223 | |
| 224 | plugin.formatter.updateSubMenuToggle(); |
| 225 | plugin.formatter.setNestedMargins(); |
| 226 | plugin.cancelNewPage(); |
| 227 | $(document).trigger('close-modal-manual'); |
| 228 | } |
| 229 | |
| 230 | // Append new post rows to the nested view |
| 231 | plugin.appendRows = function(appendto, post, before, after) |
| 232 | { |
| 233 | var html = '<li id="menuItem_' + post.id + '" class="page-row'; |
| 234 | if ( post.status === 'publish' ) html += ' published'; |
| 235 | html += '">'; |
| 236 | |
| 237 | if ( NestedPages.jsData.hierarchical ){ |
| 238 | html += '<div class="row">'; |
| 239 | html += '<div class="child-toggle"></div>'; |
| 240 | } else { |
| 241 | html += '<div class="row non-hierarchical">'; |
| 242 | html += '<div class="non-hierarchical-spacer"></div>'; |
| 243 | } |
| 244 | |
| 245 | html += '<div class="row-inner">'; |
| 246 | // Submenu |
| 247 | html += '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="np-icon-sub-menu"><path fill="none" d="M0 0h24v24H0V0z"/><path d="M19 15l-6 6-1.42-1.42L15.17 16H4V4h2v10h9.17l-3.59-3.58L13 9l6 6z" class="arrow" /></svg>'; |
| 248 | // Handle |
| 249 | html += '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="handle np-icon-menu"><path d="M0 0h24v24H0z" fill="none" /><path d="M3 18h18v-2H3v2zm0-5h18v-2H3v2zm0-7v2h18V6H3z" class="bars" /></svg>'; |
| 250 | html += '<a href="' + post.edit_link + '" class="page-link page-title">'; |
| 251 | html += '<span class="title">' + post.title + '</span>'; |
| 252 | |
| 253 | // Status |
| 254 | if ( post.status !== 'Publish' ){ |
| 255 | html += '<span class="status">(' + post.status + ')</span>'; |
| 256 | } else { |
| 257 | html += '<span class="status"></span>'; |
| 258 | } |
| 259 | |
| 260 | // Nav Status |
| 261 | html += '<span class="nav-status">'; |
| 262 | if ( post.np_nav_status === 'hide' ){ |
| 263 | html += ' ' + nestedpages.hidden; |
| 264 | } |
| 265 | html += '</span>'; |
| 266 | |
| 267 | html += '<span class="edit-indicator">Edit</span>'; |
| 268 | html += '</a>'; |
| 269 | |
| 270 | // Non-Hierarchical Data |
| 271 | if ( !NestedPages.jsData.hierarchical ){ |
| 272 | html += '<div class="np-post-columns">'; |
| 273 | html += '<ul class="np-post-info">'; |
| 274 | html += '<li><span class="np-author-display">' + post.author_formatted + '</span></li>'; |
| 275 | html += '<li>' + post.date_formatted + '</li>'; |
| 276 | html += '</ul>'; |
| 277 | html += '</div>'; |
| 278 | } |
| 279 | |
| 280 | // Yoast |
| 281 | if ( $('.nplist').first().hasClass('has-yoast') ) { |
| 282 | html += '<span class="np-seo-indicator na"></span>'; |
| 283 | } |
| 284 | |
| 285 | // Action Buttons |
| 286 | html += '<div class="action-buttons">'; |
| 287 | html += '<div class="nestedpages-dropdown" data-dropdown><a href="#" class="np-btn has-icon toggle" data-dropdown-toggle><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M6 10c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm12 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm-6 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/></svg></a><ul class="nestedpages-dropdown-content" data-dropdown-content>'; |
| 288 | // Add Link |
| 289 | html += '<li><a href="#" class="open-redirect-modal" data-parentid="' + post.id + '"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>' + nestedpages.add_link + '</a></li>'; |
| 290 | // Add Child |
| 291 | html += '<li><a href="#" class="add-new-child" data-id="' + post.id + '" data-parentname="' + post.title + '"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M3 21h18v-2H3v2zM3 8v8l4-4-4-4zm8 9h10v-2H11v2zM3 3v2h18V3H3zm8 6h10V7H11v2zm0 4h10v-2H11v2z"/><path d="M0 0h24v24H0z" fill="none"/></svg>' + nestedpages.add_child_short + '</a></li>'; |
| 292 | html += '</ul></div>'; |
| 293 | |
| 294 | // Quick Edit (data attrs) |
| 295 | html += '<a href="#" class="np-btn np-quick-edit" data-id="' + post.id + '" data-template="' + post.page_template + '" data-title="' + post.title + '" data-slug="' + post.slug + '" data-commentstatus="closed" data-status="' + post.status.toLowerCase() + '" data-np-status="show" data-navstatus="show" data-author="' + post.author + '" data-template="' + post.template + '" data-month="' + post.month + '" data-day="' + post.day + '" data-year="' + post.year + '" data-hour="' + post.hour + '" data-minute="' + post.minute + '" data-datepicker="' + post.datepicker + '" data-time="' + post.time + '" data-formattedtime="' + post.formattedtime + '" data-ampm="' + post.ampm + '">' + nestedpages.quick_edit + '</a>'; |
| 296 | |
| 297 | html += '<a href="' + post.view_link + '" class="np-btn" target="_blank">' + nestedpages.view + '</a>'; |
| 298 | |
| 299 | // Trash |
| 300 | html += '<a href="' + post.delete_link + '" class="np-btn np-btn-trash"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="np-icon-remove"><path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z" class="icon"/><path d="M0 0h24v24H0z" fill="none"/></svg></a>'; |
| 301 | html += '</div><!-- .action-buttons -->'; |
| 302 | |
| 303 | html += '</div><!-- .row-inner --></div><!-- .row -->'; |
| 304 | html += '</li>'; |
| 305 | |
| 306 | if ( before ){ |
| 307 | var row = plugin.findRowById(before); |
| 308 | $(html).insertBefore(row); |
| 309 | return; |
| 310 | } |
| 311 | if ( after ){ |
| 312 | var row = plugin.findRowById(after); |
| 313 | $(html).insertAfter(row); |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | $(appendto).append(html); |
| 318 | } |
| 319 | |
| 320 | // Find the row for inserting before/after |
| 321 | plugin.findRowById = function(id) |
| 322 | { |
| 323 | var row = $(NestedPages.selectors.rows + '#menuItem_' + id); |
| 324 | return row; |
| 325 | } |
| 326 | |
| 327 | // Toggle the "Add & Edit" & "Add" buttons depending on row count |
| 328 | plugin.toggleAddEditButton = function(form) |
| 329 | { |
| 330 | var titleCount = $(form).find('.np_title').length; |
| 331 | if ( titleCount < 1 ){ |
| 332 | $(NestedPages.selectors.newPageSubmitButton).hide(); |
| 333 | return; |
| 334 | } |
| 335 | $(NestedPages.selectors.newPageSubmitButton).show(); |
| 336 | if ( titleCount > 1 ){ |
| 337 | $(NestedPages.selectors.newPageSubmitButton + '.add-edit').hide() |
| 338 | return; |
| 339 | } |
| 340 | $(NestedPages.selectors.newPageSubmitButton + '.add-edit').show() |
| 341 | } |
| 342 | |
| 343 | // Toggle the form loading state |
| 344 | plugin.toggleLoading = function(loading) |
| 345 | { |
| 346 | if ( loading ){ |
| 347 | $(NestedPages.selectors.quickEditErrorDiv).hide(); |
| 348 | $(NestedPages.selectors.newPageSubmitButton).attr('disabled', 'disabled'); |
| 349 | $(NestedPages.selectors.quickEditLoadingIndicator).show(); |
| 350 | return; |
| 351 | } |
| 352 | $(NestedPages.selectors.newPageSubmitButton).attr('disabled', false); |
| 353 | $(NestedPages.selectors.quickEditLoadingIndicator).hide(); |
| 354 | } |
| 355 | |
| 356 | return plugin.bindEvents(); |
| 357 | } |