media.js
31.5 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
/**
* Displays the Tree View in the Media Library, and handles
* its functionality, such as creating/editing/deleting Terms,
* drag and drop categorization etc.
*
* @since 1.0.0
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
var mediaLibraryOrganizerTreeViewGridSelectedAttachments,
mediaLibraryOrganizerTreeViewGridModified;
/**
* Enables the Context Menu on the Tree View
*
* @since 1.1.1
*/
function mediaLibraryOrganizerTreeViewContextMenuInit() {
( function( $ ) {
$( '#media-library-organizer-tree-view-list' ).contextmenu(
{
delegate: '.cat-item',
menu: media_library_organizer_tree_view.context_menu,
select: function( event, ui ) {
// Get selected Term ID and Name.
var term_id = mediaLibraryOrganizerTreeViewGetTermIDFromElement( ui.target.parent() ),
term_name = mediaLibraryOrganizerTreeViewGetTermNameFromElement( ui.target );
switch ( ui.cmd ) {
case 'create_term':
mediaLibraryOrganizerTreeViewAddCategory( term_id );
break;
case 'edit_term':
mediaLibraryOrganizerTreeViewEditCategory( term_id, term_name );
break;
case 'delete_term':
mediaLibraryOrganizerTreeViewDeleteCategory( term_id, term_name );
break;
default:
// Fire the mlo:grid:tree-view:context-menu:{ui.cmd} event that Addons can hook into and listen.
let atts = {
'term_id': term_id,
'term_name': term_name
}
wp.media.events.trigger(
'mlo:grid:tree-view:context-menu:' + ui.cmd,
{
media_library_organizer_tree_view,
atts
}
);
break;
}
}
}
);
} )( jQuery );
}
/**
* Add a Category
*
* @since 1.1.1
*
* @param int term_id Term ID.
*/
function mediaLibraryOrganizerTreeViewAddCategory( term_id ) {
( function( $ ) {
// Get Name.
var new_term_name = prompt( media_library_organizer_tree_view.actions.create_term.prompt );
if ( ! new_term_name || ! new_term_name.length ) {
return;
}
// Build args.
var args = {
'action': media_library_organizer_tree_view.actions.create_term.action,
'nonce': media_library_organizer_tree_view.actions.create_term.nonce,
'taxonomy_name': media_library_organizer_tree_view.taxonomy.name,
'term_name': new_term_name,
'term_parent_id': term_id
};
args[ media_library_organizer_tree_view.taxonomy.name ] = media_library_organizer_tree_view.selected_term;
// Send request.
$.post(
media_library_organizer_tree_view.ajaxurl,
args,
function( response ) {
// Bail if an error occured.
if ( ! response.success ) {
alert( response.data );
return;
}
// Build attributes to send to wp.media.events.
var atts = response.data;
atts.selected_term = media_library_organizer_tree_view.selected_term;
atts.media_view = media_library_organizer_tree_view.media_view;
// Fire the mlo:grid:tree-view:added:term event that Addons can hook into and listen.
wp.media.events.trigger( 'mlo:grid:tree-view:added:term', atts );
// Reload Tree View.
mediaLibraryOrganizerTreeViewGet( atts.taxonomy.name, atts.selected_term );
}
);
} )( jQuery );
}
/**
* Edits an existing Category.
*
* @since 1.1.1
*
* @param int term_id Existing Category ID.
* @param string term_name Existing Category Name.
*/
function mediaLibraryOrganizerTreeViewEditCategory( term_id, term_name ) {
( function( $ ) {
// Bail if no Term ID specified.
if ( ! term_id ) {
alert( media_library_organizer_tree_view.actions.edit_term.no_selection );
return;
}
// Get Name.
var new_term_name = prompt( media_library_organizer_tree_view.actions.edit_term.prompt, term_name );
if ( ! new_term_name || ! new_term_name.length ) {
return;
}
// Build args.
var args = {
'action': media_library_organizer_tree_view.actions.edit_term.action,
'nonce': media_library_organizer_tree_view.actions.edit_term.nonce,
'taxonomy_name': media_library_organizer_tree_view.taxonomy.name,
'term_id': term_id,
'term_name': new_term_name,
};
args[ media_library_organizer_tree_view.taxonomy.name ] = media_library_organizer_tree_view.selected_term;
// Send request.
$.post(
media_library_organizer_tree_view.ajaxurl,
args,
function( response ) {
// Bail if an error occured.
if ( ! response.success ) {
alert( response.data );
return;
}
// Build attributes to send to wp.media.events.
var atts = response.data;
atts.selected_term = media_library_organizer_tree_view.selected_term;
atts.media_view = media_library_organizer_tree_view.media_view;
// Fire the mlo:grid:tree-view:edited:term event that Addons can hook into and listen.
wp.media.events.trigger( 'mlo:grid:tree-view:edited:term', atts );
// Reload Tree View.
mediaLibraryOrganizerTreeViewGet( atts.taxonomy.name, atts.selected_term );
}
);
} )( jQuery );
}
/**
* Deletes an existing Category
*
* @since 1.1.1
*
* @param int term_id Existing Category ID.
* @param string term_name Existing Category Name.
*/
function mediaLibraryOrganizerTreeViewDeleteCategory( term_id, term_name ) {
( function( $ ) {
// Bail if no Term ID specified.
if ( ! term_id ) {
alert( media_library_organizer_tree_view.actions.delete_term.no_selection );
return;
}
// Confirm Deletion.
var result = confirm( media_library_organizer_tree_view.actions.delete_term.prompt + ' ' + term_name );
if ( ! result ) {
return;
}
// Build args.
var args = {
'action': media_library_organizer_tree_view.actions.delete_term.action,
'nonce': media_library_organizer_tree_view.actions.delete_term.nonce,
'taxonomy_name': media_library_organizer_tree_view.taxonomy.name,
'term_id': term_id
};
args[ media_library_organizer_tree_view.taxonomy.name ] = media_library_organizer_tree_view.selected_term;
// Send request.
$.post(
media_library_organizer_tree_view.ajaxurl,
args,
function( response ) {
// Bail if an error occured.
if ( ! response.success ) {
alert( response.data );
return;
}
// Build attributes to send to wp.media.events.
var atts = response.data;
atts.selected_term = media_library_organizer_tree_view.selected_term;
atts.media_view = media_library_organizer_tree_view.media_view;
// Fire the mlo:grid:tree-view:edited:term event that Addons can hook into and listen.
wp.media.events.trigger( 'mlo:grid:tree-view:deleted:term', atts );
// Reload Tree View.
mediaLibraryOrganizerTreeViewGet( atts.taxonomy.name, atts.selected_term );
}
);
} )( jQuery );
}
/**
* Assign Attachment(s) to the given Category.
*
* @since 1.1.1
*
* @param array attachment_ids Attachment IDs.
* @param int term_id Category ID to assign Attachment(s) to.
*/
function mediaLibraryOrganizerTreeViewAssignAttachmentsToCategory( attachment_ids, term_id ) {
( function( $ ) {
// Bail if no Attachment IDs or Term ID.
if ( ! attachment_ids ) {
return;
}
if ( ! term_id ) {
return;
}
$.post(
media_library_organizer_tree_view.ajaxurl,
{
'action': media_library_organizer_tree_view.actions.categorize_attachments.action,
'nonce': media_library_organizer_tree_view.actions.categorize_attachments.nonce,
'taxonomy_name': media_library_organizer_tree_view.taxonomy.name,
'attachment_ids': attachment_ids,
'term_id': term_id
},
function( response ) {
// Bail if an error occured.
if ( ! response.success ) {
wpzinc_notification_show_error_message( response.data );
return;
}
// Show notification.
wpzinc_notification_show_success_message( media_library_organizer_tree_view.labels.categorized_attachments.replace( '%s', response.data.attachments.length ) );
// Build attributes to send to wp.media.events.
var atts = response.data;
atts.selected_term = media_library_organizer_tree_view.selected_term;
atts.media_view = media_library_organizer_tree_view.media_view;
// Fire the mlo:grid:tree-view:assigned:attachments:term event that Addons can hook into and listen.
wp.media.events.trigger( 'mlo:grid:tree-view:assigned:attachments:term', atts );
}
);
} )( jQuery );
}
/**
* Enables or disables contextual buttons for editing and deleting Categories.
*
* @since 1.1.1
*/
function mediaLibraryOrganizerTreeViewContextualButtons() {
( function( $ ) {
if ( $( '#media-library-organizer-tree-view-list .current-cat' ).length ) {
// Enable.
$( 'button.media-library-organizer-tree-view-edit' ).prop( 'disabled', false );
$( 'button.media-library-organizer-tree-view-delete' ).prop( 'disabled', false );
} else {
// Disable.
$( 'button.media-library-organizer-tree-view-edit' ).prop( 'disabled', true );
$( 'button.media-library-organizer-tree-view-delete' ).prop( 'disabled', true );
}
} )( jQuery );
}
/**
* Fetch the Tree View HTML, injecting it into the container
*
* @since 1.1.1
*
* @param string taxonomy_name Taxonomy Name.
* @param int current_term The current Term ID or Slug that is selected.
*/
function mediaLibraryOrganizerTreeViewGet( taxonomy_name, current_term ) {
( function( $ ) {
$.post(
media_library_organizer_tree_view.ajaxurl,
{
'action': media_library_organizer_tree_view.actions.get_tree_view.action,
'nonce': media_library_organizer_tree_view.actions.get_tree_view.nonce,
'taxonomy_name': taxonomy_name,
'current_term': current_term
},
function( response ) {
if ( ! response.success ) {
return false;
}
// Destroy JSTree.
mediaLibraryOrganizerTreeViewDestroyJsTree();
// Inject Tree View into DOM.
$( '#media-library-organizer-tree-view-list' ).html( response.data );
// Init JSTree.
mediaLibraryOrganizerTreeViewInitJsTree();
// Enable or Disable Rename and Delete when a Category is selected.
mediaLibraryOrganizerTreeViewContextualButtons();
// Rebind Droppable.
mediaLibraryOrganizerTreeViewInitDroppable();
// Fire the mlo:grid:tree-view:loaded event that Addons can hook into and listen.
wp.media.events.trigger( 'mlo:grid:tree-view:loaded' );
}
);
} )( jQuery );
}
/**
* Initialize the Tree View JSTree.
*
* @since 1.2.7
*/
function mediaLibraryOrganizerTreeViewInitJsTree() {
( function( $ ) {
if ( $( '.media-library-organizer-tree-view-enabled' ).length ) {
// If a subcategory was selected, open all .current-cat-ancestor list items,
// so the user can see the subcategory.
$( 'li.current-cat-ancestor', $( '.media-library-organizer-tree-view-enabled' ) ).each(
function() {
$( this ).addClass( 'jstree-open' );
}
);
// Init JSTree.
$( '.media-library-organizer-tree-view-enabled' ).jstree()
.bind(
'select_node.jstree',
function( e, data ) {
document.location.href = data.node.a_attr.href;
}
)
.bind(
'open_node.jstree',
function( e, data ) {
// Re-init droppable targets as new categories are displayed in the Tree View.
mediaLibraryOrganizerTreeViewInitDroppable();
}
);
}
} )( jQuery );
}
/**
* Destroys the Tree View JSTree
*
* @since 1.2.7
*/
function mediaLibraryOrganizerTreeViewDestroyJsTree() {
( function( $ ) {
if ( $( '.media-library-organizer-tree-view-enabled' ).length ) {
$( '.media-library-organizer-tree-view-enabled' ).jstree( 'destroy' );
}
} )( jQuery );
}
/**
* Initialize the Tree View Draggable on the List View
*
* @since 1.1.1
*/
function mediaLibraryOrganizerTreeViewListInitDraggable() {
( function( $ ) {
$( 'td.title.column-title strong.has-media-icon, td.tree-view-move span.dashicons-move' ).draggable(
{
appendTo: 'body', // Ensure dragging div is above all other elements.
revert: true,
cursorAt: {
top: 10,
left: 10
},
helper: function() {
var attachment_id = $( this ).closest( 'tr' ).attr( 'id' ).split( '-' )[1],
attachment_ids = [ attachment_id ];
// See if any Media Library items' checkboxes have been checked.
// If so, include them.
if ( $( 'table.media tbody input:checked' ).length > 0 ) {
// Get Attachment IDs.
$( 'table.media tbody input:checked' ).each(
function() {
// Skip if this Attachment is the one we're dragging, to avoid duplicates.
if ( $( this ).val() == attachment_id ) {
return;
}
attachment_ids.push( $( this ).val() );
}
);
}
// Define label.
var label = '';
if ( attachment_ids.length > 1 ) {
label = media_library_organizer_tree_view.labels.categorize_attachments.replace( '%s', attachment_ids.length );
} else {
label = media_library_organizer_tree_view.labels.categorize_attachment;
}
return $( '<div id="media-library-organizer-tree-view-draggable" data-attachment-ids="' + attachment_ids.join( ',' ) + '">' + label + '</div>' );
}
}
);
} )( jQuery );
}
/**
* Initialize the Tree View Draggable on the Grid View
*
* @since 1.1.1
*/
function mediaLibraryOrganizerTreeViewGridInitDraggable() {
( function( $ ) {
$( 'li.attachment' ).draggable(
{
appendTo: 'body', // Ensure dragging div is above all other elements.
revert: true,
cursorAt: {
top: 40,
left: 10
},
helper: function() {
var attachment_id = $( this ).data( 'id' ),
attachment_ids = [ attachment_id ];
// Add Bulk Selected Attachments, if defined.
if ( mediaLibraryOrganizerTreeViewGridSelectedAttachments.length > 0 ) {
var length = mediaLibraryOrganizerTreeViewGridSelectedAttachments.length;
for ( var i = 0; i < length; i++ ) {
// Skip if this attachment is already selected.
if ( mediaLibraryOrganizerTreeViewGridSelectedAttachments.models[ i ].id == attachment_id ) {
continue;
}
attachment_ids.push( mediaLibraryOrganizerTreeViewGridSelectedAttachments.models[ i ].id );
}
}
// Define label.
var label = '';
if ( attachment_ids.length > 1 ) {
label = media_library_organizer_tree_view.labels.categorize_attachments.replace( '%s', attachment_ids.length );
} else {
label = media_library_organizer_tree_view.labels.categorize_attachment;
}
return $( '<div id="media-library-organizer-tree-view-draggable" data-attachment-ids="' + attachment_ids.join( ',' ) + '">' + label + '</div>' );
}
}
);
} )( jQuery );
}
/**
* Initialize the Tree View Droppable.
*
* @since 1.1.1
*/
function mediaLibraryOrganizerTreeViewInitDroppable() {
( function( $ ) {
$( '#media-library-organizer-tree-view-list li.cat-item a, #media-library-organizer-tree-view-list li.cat-item-unassigned a' ).droppable(
{
hoverClass: 'media-library-organizer-tree-view-droppable-hover',
drop: function( event, ui ) {
// Get Attachment IDs from helper.
var attachment_ids = $( ui.helper ).data( 'attachment-ids' );
if ( attachment_ids.toString().search( ',' ) ) {
attachment_ids = attachment_ids.toString().split( ',' );
}
// Get Term ID we dropped the items on.
var term_id = mediaLibraryOrganizerTreeViewGetTermIDFromElement( $( event.target ).parent() );
// Assign Attachments to Category.
mediaLibraryOrganizerTreeViewAssignAttachmentsToCategory( attachment_ids, term_id );
}
}
);
} )( jQuery );
}
/**
* Extracts the Term ID from the given <li> Tree View element.
*
* @since 1.2.7
*
* @param DOMElement element The <li> element.
* @return mixed false | Term ID (-1 = unassigned, any other number = Term ID)
*/
function mediaLibraryOrganizerTreeViewGetTermIDFromElement( element ) {
// Bail if no CSS classes exist on the element.
if ( typeof element[0] === 'undefined' ) {
return false;
}
if ( typeof element[0].className === 'undefined' ) {
return false;
}
var css_classes = element[0].className.split( ' ' ),
length = css_classes.length;
for ( var i = 0; i < length; i++ ) {
// Skip if this isn't the class we're looking for.
if ( css_classes[ i ].search( 'cat-item-' ) == -1 ) {
continue;
}
// Extract number.
var term_id = css_classes[ i ].replace( 'cat-item-', '' );
// If the Term ID is 'unassigned', return -1.
if ( term_id == 'unassigned' ) {
return -1;
}
return term_id;
}
return false;
}
/**
* Extracts the Term Name from the given <a> Tree View element
*
* @since 1.3.1
*
* @param DOMElement element The <a> element.
* @return string Term Name.
*/
function mediaLibraryOrganizerTreeViewGetTermNameFromElement( element ) {
return jQuery( element ).contents().filter(
function() {
return this.nodeType == 3;
}
) [0].nodeValue.trim();
}
/**
* Draggable: Grid View: Reinitialize Drag and Drop Categorization when the Attachments Browser contents change
* e.g.
* - Filters are applied
* - Search is changed
* - Bulk Selection is activated, changed or deactivated
* - Attachments are dragged and dropped into the Tree View for categorization
*/
if ( media_library_organizer_tree_view.media_view == 'grid' ) {
// (Re)initialize Drag and Drop Categorization when the Attachments Browser contents change.
jQuery( document ).ready(
function( $ ) {
var mediaLibraryOrganizerTreeViewObserver = new MutationObserver( mediaLibraryOrganizerTreeViewGridInitDraggable );
mediaLibraryOrganizerTreeViewObserver.observe(
document.querySelector( '.attachments-browser ul.attachments' ),
{
childList: true
}
);
}
);
/**
* Fetch the selected attachments, storing them in a local var
*/
( function( $, _ ) {
// Called on load and when Bulk Select is cancelled.
_.extend(
wp.media.view.AttachmentFilters.prototype,
{
select: function() {
mediaLibraryOrganizerTreeViewGridSelectedAttachments = this.controller.state().get( 'selection' );
}
}
);
// Called when Bulk Select is used and an attachment is selected/deselected.
_.extend(
wp.media.controller.Library.prototype,
{
refreshContent: function() {
mediaLibraryOrganizerTreeViewGridSelectedAttachments = this.get( 'selection' );
},
}
);
} )( jQuery, _ );
}
jQuery( document ).ready(
function( $ ) {
// Media Library Screen.
if ( $( 'body' ).hasClass( 'upload-php' ) ) {
// Move tree view into the wrapper.
$( '.wrap' ).wrap( '<div class="media-library-organizer-tree-view"></div>' );
$( '.media-library-organizer-tree-view' ).prepend( $( '#media-library-organizer-tree-view' ) );
$( '#media-library-organizer-tree-view' ).show();
// Make Sidebar Sticky.
var mediaLibraryOrganizerTreeViewSidebar = new StickySidebar(
'#media-library-organizer-tree-view',
{
containerSelector: '.media-library-organizer-tree-view',
innerWrapperSelector: '.media-library-organizer-tree-view-inner',
}
);
// Setup right click context menu, if the User's role permits managing Categories.
if ( media_library_organizer_tree_view.context_menu != false ) {
mediaLibraryOrganizerTreeViewContextMenuInit();
}
// JSTree.
mediaLibraryOrganizerTreeViewInitJsTree();
// Draggable.
mediaLibraryOrganizerTreeViewListInitDraggable();
// Droppable.
mediaLibraryOrganizerTreeViewInitDroppable();
// Enable or Disable Rename and Delete when a Category is selected.
mediaLibraryOrganizerTreeViewContextualButtons();
// Add Category.
$( 'body' ).on(
'click',
'.media-library-organizer-tree-view-add',
function( e ) {
e.preventDefault();
// Get selected Term ID.
var term_id = mediaLibraryOrganizerTreeViewGetTermIDFromElement( $( '#media-library-organizer-tree-view-list .current-cat' ) );
// Add Category.
mediaLibraryOrganizerTreeViewAddCategory( term_id );
}
);
// Edit Category.
$( 'body' ).on(
'click',
'.media-library-organizer-tree-view-edit',
function( e ) {
e.preventDefault();
// Get selected Term ID and Name.
var term_id = mediaLibraryOrganizerTreeViewGetTermIDFromElement( $( '#media-library-organizer-tree-view-list .current-cat' ) ),
term_name = mediaLibraryOrganizerTreeViewGetTermNameFromElement( $( '#media-library-organizer-tree-view-list .current-cat a' ) );
// Edit Category.
mediaLibraryOrganizerTreeViewEditCategory( term_id, term_name );
}
);
// Delete Category.
$( 'body' ).on(
'click',
'.media-library-organizer-tree-view-delete',
function( e ) {
e.preventDefault();
// Get selected Term ID and Name.
var term_id = mediaLibraryOrganizerTreeViewGetTermIDFromElement( $( '#media-library-organizer-tree-view-list .current-cat' ) ),
term_name = mediaLibraryOrganizerTreeViewGetTermNameFromElement( $( '#media-library-organizer-tree-view-list .current-cat a' ) );
// Delete Category.
mediaLibraryOrganizerTreeViewDeleteCategory( term_id, term_name );
}
);
}
}
);
/**
* Tree View: When a Taxonomy Term is added, refresh the Taxonomy Dropdown Filter
*
* @since 1.3.3
*
* @param obj atts Attributes.
*/
wp.media.events.on(
'mlo:grid:tree-view:added:term',
function( atts ) {
( function( $ ) {
switch ( atts.media_view ) {
/**
* List View
*/
case 'list':
// Replace <select> Taxonomy dropdown to reflect changes.
mediaLibraryOrganizerListViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.dropdown_filter,
atts.selected_term
);
break;
/**
* Grid View
*/
case 'grid':
// Replace Taxonomy Filter to reflect changes, if we're not in Bulk Select mode
// (otherwise the Taxonomy Filters display between the 'Delete permanently' and 'Cancel' buttons).
if ( ! MediaLibraryOrganizerAttachmentsBrowser.controller.isModeActive( 'select' ) ) {
mediaLibraryOrganizerGridViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.terms,
atts.taxonomy.labels.all_items,
media_library_organizer_media.labels.unassigned
);
}
break;
}
} )( jQuery );
}
);
/**
* Tree View: When a Taxonomy Term is edited, refresh the Taxonomy Dropdown Filter
*
* @since 1.3.3
*
* @param obj atts Attributes.
*/
wp.media.events.on(
'mlo:grid:tree-view:edited:term',
function( atts ) {
( function( $ ) {
// Update this Taxonomy for any Attachments in the Media Library View that are assigned to it.
switch ( atts.media_view ) {
/**
* List View
*/
case 'list':
// Replace <select> Taxonomy dropdown to reflect changes.
mediaLibraryOrganizerListViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.dropdown_filter,
atts.selected_term
);
// Iterate through all Terms listed in the WP_List_Table for each Attachment,
// replacing the old Term with the New Term.
mediaLibraryOrganizerListViewUpdateAttachmentTerms(
atts.taxonomy.name,
atts.old_term,
atts.term
);
break;
/**
* Grid View
*/
case 'grid':
// Replace Taxonomy Filter to reflect changes, if we're not in Bulk Select mode
// (otherwise the Taxonomy Filters display between the 'Delete permanently' and 'Cancel' buttons).
if ( ! MediaLibraryOrganizerAttachmentsBrowser.controller.isModeActive( 'select' ) ) {
mediaLibraryOrganizerGridViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.terms,
atts.taxonomy.labels.all_items,
media_library_organizer_media.labels.unassigned
);
}
// Refresh the Library to reflect the changed Term Name.
if ( typeof wp.media.frame.library !== 'undefined' ) {
wp.media.frame.library.props.set( {ignore: (+ new Date())} );
} else {
wp.media.frame.content.get().collection.props.set( {ignore: (+ new Date())} );
wp.media.frame.content.get().options.selection.reset();
}
break;
}
} )( jQuery );
}
);
/**
* Tree View: When a Taxonomy Term is deleted, refresh the Taxonomy Dropdown Filter
*
* @since 1.3.3
*
* @param obj atts Attributes.
*/
wp.media.events.on(
'mlo:grid:tree-view:deleted:term',
function( atts ) {
( function( $ ) {
// Remove this Term from any Attachments in the Media Library View.
switch ( atts.media_view ) {
/**
* List View
*/
case 'list':
// If we're viewing the Term we just deleted, reset the view.
if ( atts.selected_term == atts.term.slug ) {
window.location.href = 'upload.php?mode=list';
return;
}
// Replace <select> Taxonomy dropdown to reflect changes.
mediaLibraryOrganizerListViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.dropdown_filter,
atts.selected_term
);
// Iterate through all Terms listed in the WP_List_Table for each Attachment, remov the Term.
mediaLibraryOrganizerListViewUpdateAttachmentTerms(
atts.taxonomy.name,
atts.term,
false
);
break;
/**
* Grid View.
*/
case 'grid':
// If we're viewing the Category we just deleted, reset the view.
if ( atts.selected_term == atts.term.slug ) {
window.location.href = 'upload.php?mode=grid';
return;
}
// Replace Taxonomy Filter to reflect changes, if we're not in Bulk Select mode
// (otherwise the Taxonomy Filters display between the 'Delete permanently' and 'Cancel' buttons).
if ( ! MediaLibraryOrganizerAttachmentsBrowser.controller.isModeActive( 'select' ) ) {
mediaLibraryOrganizerGridViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.terms,
atts.taxonomy.labels.all_items,
media_library_organizer_media.labels.unassigned
);
}
// Refresh the Library to reflect the deleted Term.
if ( typeof wp.media.frame.library !== 'undefined' ) {
wp.media.frame.library.props.set( {ignore: (+ new Date())} );
} else {
wp.media.frame.content.get().collection.props.set( {ignore: (+ new Date())} );
wp.media.frame.content.get().options.selection.reset();
}
break;
}
} )( jQuery );
}
);
/**
* List or Grid View: When attachment(s) are dragged and dropped onto a Category in the Tree View:
* -
* -
*
* @since 1.3.3
*
* @param obj atts Attributes.
*/
wp.media.events.on(
'mlo:grid:tree-view:assigned:attachments:term',
function( atts ) {
( function( $ ) {
switch ( atts.media_view ) {
/**
* List View
*/
case 'list':
// Replace <select> Taxonomy dropdown to reflect changes.
mediaLibraryOrganizerListViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.dropdown_filter,
media_library_organizer_tree_view.selected_term
);
// For each Attachment, build Term Links.
for ( let attachment in atts.attachments ) {
var terms = [],
length = atts.attachments[ attachment ].terms.length;
for ( j = 0; j < length; j++ ) {
terms.push( '<a href="upload.php?taxonomy=' + atts.attachments[ attachment ].terms[ j ].taxonomy + '&term=' + atts.attachments[ attachment ].terms[ j ].slug + '">' + atts.attachments[ attachment ].terms[ j ].name + '</a>' );
}
// Set HTML in Terms column of this Attachment's row.
$( 'tr#post-' + atts.attachments[ attachment ].id + ' td.taxonomy-' + atts.taxonomy.name ).html( terms.join( ', ' ) );
}
break;
/**
* Grid View
*/
case 'grid':
// Cancel Bulk Select mode if active and was just used to categorize multiple Attachments.
if ( MediaLibraryOrganizerAttachmentsBrowser.controller.isModeActive( 'select' ) ) {
MediaLibraryOrganizerAttachmentsBrowser.controller.deactivateMode( 'select' ).activateMode( 'edit' );
}
// Replace Taxonomy Filter to reflect changes.
mediaLibraryOrganizerGridViewReplaceTaxonomyFilter(
atts.taxonomy.name,
atts.terms,
atts.taxonomy.labels.all_items,
media_library_organizer_media.labels.unassigned
);
// Refresh Grid View.
mediaLibraryOrganizerGridViewRefresh();
break;
}
// Don't reload Tree View if the Term that was added isn't for the Taxonomy displayed in the Tree View.
if ( atts.taxonomy.name != media_library_organizer_tree_view.taxonomy.name ) {
return;
}
// Reload Tree View.
mediaLibraryOrganizerTreeViewGet( atts.taxonomy.name, atts.selected_term );
} )( jQuery );
}
);
/**
* Grid View: When an attachment is edited in the Grid View, and has a Taxonomy Term added to it using the
* inline 'Add New' option, reload the Tree View to reflect the new Taxonomy Term
*
* @since 1.3.3
*
* @param obj atts Attributes.
*/
wp.media.events.on(
'mlo:grid:edit-attachment:added:term',
function( atts ) {
// Don't reload Tree View if the Term that was added isn't for the Taxonomy displayed in the Tree View.
if ( atts.taxonomy.name != media_library_organizer_tree_view.taxonomy.name ) {
return;
}
// Reload Tree View.
mediaLibraryOrganizerTreeViewGet( atts.taxonomy.name, media_library_organizer_tree_view.selected_term );
}
);
/**
* Grid View: When Taxonomy Term(s) are assigned or unassigned to an Attachment in the Grid View
* reload the Tree View for the Taxonomy where changes were made.
*
* @since 1.3.3
*
* @param obj atts Attributes.
*/
wp.media.events.on(
'mlo:grid:edit-attachment:edited',
function( atts ) {
( function( $ ) {
// Bail if no Taxonomy Terms were changed in the Attachment.
if ( ! atts.taxonomy_term_changed ) {
return;
}
// Reload Tree View.
mediaLibraryOrganizerTreeViewGet( media_library_organizer_tree_view.taxonomy.name, media_library_organizer_tree_view.selected_term );
} )( jQuery );
}
);
/**
* Grid View: When the Grid View's Taxonomy Filter's value is changed, reflect the change
* of selected Category in the Tree View.
*
* @since 1.2.2
*
* @param obj atts Filter Attributes.
* slug: term-slug.
*/
wp.media.events.on(
'mlo:grid:filter:change:term',
function( atts ) {
// Don't reload Tree View if the Term that was changed isn't the Taxonomy displayed in the Tree View.
if ( atts.taxonomy_name != media_library_organizer_tree_view.taxonomy.name ) {
return;
}
// Update the selected term.
media_library_organizer_tree_view.selected_term = atts.slug;
// Reload Tree View.
mediaLibraryOrganizerTreeViewGet( atts.taxonomy_name, atts.slug );
}
);
/**
* Grid View: When an attachment completes successful upload to the Grid View, reload the Tree View
* to show the updated Category counts
*
* @since 1.3.1
*
* @param obj attachment Uploaded Attachment.
*/
wp.media.events.on(
'mlo:grid:attachment:upload:success',
function( attachment ) {
mediaLibraryOrganizerTreeViewGet( media_library_organizer_tree_view.taxonomy.name, media_library_organizer_tree_view.selected_term );
}
);
/**
* Grid View: When an attachment is deleted in the Grid View > Edit Attachment modal,
* refresh the Tree View to get the new Term Counts.
*
* @since 1.3.3
*/
wp.media.events.on(
'mlo:grid:edit-attachment:deleted',
function( atts ) {
mediaLibraryOrganizerTreeViewGet( media_library_organizer_tree_view.taxonomy.name, media_library_organizer_tree_view.selected_term );
}
);
/**
* Grid View: When Bulk Actions complete on Attachments in the Grid View,
* refresh the Tree View to get the new Term counts
*
* @since 1.2.3
*/
wp.media.events.on(
'mlo:grid:attachments:bulk_actions:done',
function() {
mediaLibraryOrganizerTreeViewGet( media_library_organizer_tree_view.taxonomy.name, media_library_organizer_tree_view.selected_term );
}
);