codemirror
1 year ago
datatables
1 year ago
admin-menu-organizer.js
1 year ago
admin-page.js
1 year ago
content-order-sort.js
1 year ago
custom-admin-menu.js
1 year ago
external-permalinks.js
1 year ago
gutenberg-content-duplication.js
1 year ago
hide-admin-notices.js
1 year ago
jBox.all.min.js
1 year ago
jquery.jsticky.mod.js
1 year ago
jquery.jsticky.mod.min.js
1 year ago
jquery.mjs.nestedSortable.js
1 year ago
jquery.ui.touch-punch.min.js
1 year ago
js.cookie.min.js
1 year ago
list-tables-content-order.js
1 year ago
media-replace.js
1 year ago
multiple-user-roles.js
1 year ago
select2.full.min.js
1 year ago
toggle-hidden-menu.js
1 year ago
content-order-sort.js
143 lines
| 1 | (function( $ ) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | $(document).ready( function() { |
| 5 | var itemList = $('#item-list'), // Container of item list |
| 6 | maxLevel = 6, |
| 7 | sort_started = {}, // For data related to the dragged element when dragging started |
| 8 | sort_finished = {}; // For data related to the dragged element when dragging has finished |
| 9 | |
| 10 | // console.log(contentOrderSort); // Data passed from PHP via wp_localize_script |
| 11 | if (contentOrderSort.hierarchical == 'false') { |
| 12 | maxLevel = 1; |
| 13 | } |
| 14 | |
| 15 | // Make item list into nested sortable |
| 16 | // Ref: https://api.jqueryui.com/sortable/ |
| 17 | // Ref: https://github.com/ilikenwf/nestedSortable |
| 18 | itemList.nestedSortable({ |
| 19 | // Disable nesting if set to true |
| 20 | protectRoot: true, |
| 21 | // Forces the placeholder to have a size. |
| 22 | forcePlaceholderSize: true, |
| 23 | // Restricts sort start click to the specified element. |
| 24 | // Allows for a helper element to be used for dragging display. |
| 25 | // If set to "clone", then the element will be cloned and the clone will be dragged. |
| 26 | helper: 'clone', |
| 27 | listType: 'ul', |
| 28 | items: 'li', |
| 29 | toleranceElement: '> div', // Direct children of the li element |
| 30 | handle: 'div', // The same <div> for toleranceElement is set as the drag handle |
| 31 | // Specifies which mode to use for testing whether the item being moved is hovering over another item. |
| 32 | // If set to 'pointr', is when the mouse pointer overlaps the other item. |
| 33 | tolerance: 'pointer', |
| 34 | // The maximum depth of nested items the list can accept. |
| 35 | maxLevels: maxLevel, |
| 36 | // Defines the opacity of the helper while sorting. |
| 37 | opacity: 0.6, |
| 38 | placeholder: 'ui-sortable-placeholder', |
| 39 | // Whether the sortable items should revert to their new positions using a smooth animation. |
| 40 | // If set as a number, it's in miliseconds |
| 41 | revert: 250, |
| 42 | // How far right or left (in pixels) the item has to travel |
| 43 | // in order to be nested or to be sent outside its current list. Default: 20 |
| 44 | tabSize: 25, |
| 45 | // This event is triggered when sorting starts. |
| 46 | start: function (event, ui) { |
| 47 | sort_started.item = ui.item; // The jQuery object representing the current dragged element. |
| 48 | sort_started.prev = ui.item.prev(':not(".ui-sortable-placeholder")'); |
| 49 | sort_started.next = ui.item.next(':not(".ui-sortable-placeholder")'); |
| 50 | }, |
| 51 | // This event is triggered when the user stopped sorting and the DOM position has changed. |
| 52 | update: function (event, ui) { |
| 53 | // Elements of the "Updating order..." notice |
| 54 | var updateNotice = $('#updating-order-notice'), // Wrapper |
| 55 | spinner = $('#spinner-img'), // Spinner |
| 56 | updateSuccess = $('.updating-order-notice .dashicons.dashicons-saved'); // Check mark |
| 57 | |
| 58 | ui.item.find('div.row-content:first').append(updateNotice); |
| 59 | |
| 60 | // Reset the state of the "Updating order..." indicator |
| 61 | $(spinner).show(); |
| 62 | $(updateSuccess).hide(); |
| 63 | $(updateNotice).css('background-color','#eee').fadeIn(); |
| 64 | |
| 65 | // Get the end items where the item was placed |
| 66 | sort_finished.item = ui.item; // The jQuery object representing the current dragged element. |
| 67 | sort_finished.prev = ui.item.prev(':not(".ui-sortable-placeholder")'); |
| 68 | sort_finished.next = ui.item.next(':not(".ui-sortable-placeholder")'); |
| 69 | |
| 70 | var list_offset = parseInt(sort_finished.item.index()); |
| 71 | sort_finished.item.attr('data-menu-order', list_offset); |
| 72 | |
| 73 | // Get attributes |
| 74 | var attributes = {}; |
| 75 | $.each(sort_finished.item[0].attributes, function () { |
| 76 | attributes[this.name] = this.value; |
| 77 | }); |
| 78 | // console.log('attributes: ' + cleanStringify(attributes)); |
| 79 | |
| 80 | // Data for ajax call |
| 81 | var dataArgs = { |
| 82 | action: contentOrderSort.action, // from wp_localize_script |
| 83 | item_parent: 0, // We only deal with top-level items, not child items |
| 84 | start: 0, // Start processing menu_order update in DB from item with menu_order defined here |
| 85 | nonce: contentOrderSort.nonce, |
| 86 | post_id: sort_finished.item.attr('data-id'), |
| 87 | menu_order: sort_finished.item.attr('data-menu-order'), |
| 88 | excluded_items: {}, |
| 89 | post_type: sort_started.item.attr('data-post-type'), |
| 90 | attributes: attributes, |
| 91 | }; |
| 92 | // console.log('dataArgs: ' + cleanStringify(dataArgs)); |
| 93 | |
| 94 | // AJAX call to update menu_order for items in the list |
| 95 | $.ajax({ |
| 96 | type: "POST", |
| 97 | url: ajaxurl, |
| 98 | data: dataArgs, |
| 99 | success: function(response) { |
| 100 | // console.log(response); |
| 101 | // Update the state of the "Updating order..." indicator |
| 102 | $(spinner).hide(); |
| 103 | $(updateSuccess).show(); |
| 104 | $(updateNotice).css('background-color','#cce5cc').delay(1000).fadeOut(); |
| 105 | }, |
| 106 | error: function(errorThrown) { |
| 107 | console.log(errorThrown); |
| 108 | } |
| 109 | }); |
| 110 | } |
| 111 | }); |
| 112 | |
| 113 | |
| 114 | }); |
| 115 | |
| 116 | // Convert object to simpler string for console.log. Ref: https://stackoverflow.com/a/48845206 |
| 117 | // function cleanStringify(object) { |
| 118 | // if (object && typeof object === 'object') { |
| 119 | // object = copyWithoutCircularReferences([object], object); |
| 120 | // } |
| 121 | // return JSON.stringify(object); |
| 122 | |
| 123 | // function copyWithoutCircularReferences(references, object) { |
| 124 | // var cleanObject = {}; |
| 125 | // Object.keys(object).forEach(function(key) { |
| 126 | // var value = object[key]; |
| 127 | // if (value && typeof value === 'object') { |
| 128 | // if (references.indexOf(value) < 0) { |
| 129 | // references.push(value); |
| 130 | // cleanObject[key] = copyWithoutCircularReferences(references, value); |
| 131 | // references.pop(); |
| 132 | // } else { |
| 133 | // cleanObject[key] = '###_Circular_###'; |
| 134 | // } |
| 135 | // } else if (typeof value !== 'function') { |
| 136 | // cleanObject[key] = value; |
| 137 | // } |
| 138 | // }); |
| 139 | // return cleanObject; |
| 140 | // } |
| 141 | // } |
| 142 | |
| 143 | })( jQuery ); |