This repository has been archived by the owner on Apr 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
simple-page-ordering.dev.js
executable file
·125 lines (111 loc) · 3.45 KB
/
simple-page-ordering.dev.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function update_simple_ordering_callback(response) {
if ( 'children' === response ) {
window.location.reload();
return;
}
var changes = jQuery.parseJSON( response );
var new_pos = changes.new_pos;
for ( var key in new_pos ) {
if ( 'next' === key ) {
continue;
}
var inline_key = document.getElementById('inline_' + key);
if ( null !== inline_key && new_pos.hasOwnProperty(key) ) {
var dom_menu_order = inline_key.querySelector('.menu_order');
if ( undefined !== new_pos[key]['menu_order'] ) {
if ( null !== dom_menu_order ) {
dom_menu_order.innerHTML = new_pos[key]['menu_order'];
}
var dom_post_parent = inline_key.querySelector('.post_parent');
if ( null !== dom_post_parent ) {
dom_post_parent.innerHTML = new_pos[key]['post_parent'];
}
var post_title = null;
var dom_post_title = inline_key.querySelector('.post_title');
if ( null !== dom_post_title ) {
post_title = dom_post_title.innerHTML;
}
var dashes = 0;
while ( dashes < new_pos[key]['depth'] ) {
post_title = '— ' + post_title;
dashes++;
}
var dom_row_title = inline_key.parentNode.querySelector('.row-title');
if ( null !== dom_row_title && null !== post_title ) {
dom_row_title.innerHTML = post_title;
}
} else if ( null !== dom_menu_order ) {
dom_menu_order.innerHTML = new_pos[key];
}
}
}
if ( changes.next ) {
jQuery.post( ajaxurl, {
action: 'simple_page_ordering',
id: changes.next['id'],
previd: changes.next['previd'],
nextid: changes.next['nextid'],
start: changes.next['start'],
excluded: changes.next['excluded']
}, update_simple_ordering_callback );
} else {
jQuery('.spo-updating-row').removeClass('spo-updating-row');
sortable_post_table.removeClass('spo-updating').sortable('enable');
}
}
var sortable_post_table = jQuery(".wp-list-table tbody");
sortable_post_table.sortable({
items: '> tr',
cursor: 'move',
axis: 'y',
containment: 'table.widefat',
cancel: '.inline-edit-row',
distance: 2,
opacity: .8,
tolerance: 'pointer',
start: function(e, ui){
if ( typeof(inlineEditPost) !== 'undefined' ) {
inlineEditPost.revert();
}
ui.placeholder.height(ui.item.height());
},
helper: function(e, ui) {
var children = ui.children();
for ( var i=0; i<children.length; i++ ) {
var selector = jQuery(children[i]);
selector.width( selector.width() );
};
return ui;
},
stop: function(e, ui) {
// remove fixed widths
ui.item.children().css('width','');
},
update: function(e, ui) {
sortable_post_table.sortable('disable').addClass('spo-updating');
ui.item.addClass('spo-updating-row');
var postid = ui.item[0].id.substr(5); // post id
var prevpostid = false;
var prevpost = ui.item.prev();
if ( prevpost.length > 0 ) {
prevpostid = prevpost.attr('id').substr(5);
}
var nextpostid = false;
var nextpost = ui.item.next();
if ( nextpost.length > 0 ) {
nextpostid = nextpost.attr('id').substr(5);
}
// go do the sorting stuff via ajax
jQuery.post( ajaxurl, { action: 'simple_page_ordering', id: postid, previd: prevpostid, nextid: nextpostid }, update_simple_ordering_callback );
// fix cell colors
var table_rows = document.querySelectorAll('tr.iedit'),
table_row_count = table_rows.length;
while( table_row_count-- ) {
if ( 0 === table_row_count%2 ) {
jQuery(table_rows[table_row_count]).addClass('alternate');
} else {
jQuery(table_rows[table_row_count]).removeClass('alternate');
}
}
}
});