]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/tags-box.js
WordPress 4.2.1
[autoinstalls/wordpress.git] / wp-admin / js / tags-box.js
1 /* jshint curly: false, eqeqeq: false */
2 /* global ajaxurl */
3
4 var tagBox, array_unique_noempty;
5
6 ( function( $ ) {
7         // Return an array with any duplicate, whitespace or empty values removed
8         array_unique_noempty = function( array ) {
9                 var out = [];
10
11                 $.each( array, function( key, val ) {
12                         val = $.trim( val );
13
14                         if ( val && $.inArray( val, out ) === -1 ) {
15                                 out.push( val );
16                         }
17                 } );
18
19                 return out;
20         };
21
22         tagBox = {
23                 clean : function(tags) {
24                         var comma = window.tagsBoxL10n.tagDelimiter;
25                         if ( ',' !== comma )
26                                 tags = tags.replace(new RegExp(comma, 'g'), ',');
27                         tags = tags.replace(/\s*,\s*/g, ',').replace(/,+/g, ',').replace(/[,\s]+$/, '').replace(/^[,\s]+/, '');
28                         if ( ',' !== comma )
29                                 tags = tags.replace(/,/g, comma);
30                         return tags;
31                 },
32
33                 parseTags : function(el) {
34                         var id = el.id,
35                                 num = id.split('-check-num-')[1],
36                                 taxbox = $(el).closest('.tagsdiv'),
37                                 thetags = taxbox.find('.the-tags'),
38                                 comma = window.tagsBoxL10n.tagDelimiter,
39                                 current_tags = thetags.val().split( comma ),
40                                 new_tags = [];
41
42                         delete current_tags[num];
43
44                         $.each( current_tags, function( key, val ) {
45                                 val = $.trim( val );
46                                 if ( val ) {
47                                         new_tags.push( val );
48                                 }
49                         });
50
51                         thetags.val( this.clean( new_tags.join( comma ) ) );
52
53                         this.quickClicks( taxbox );
54                         return false;
55                 },
56
57                 quickClicks : function( el ) {
58                         var thetags = $('.the-tags', el),
59                                 tagchecklist = $('.tagchecklist', el),
60                                 id = $(el).attr('id'),
61                                 current_tags, disabled;
62
63                         if ( ! thetags.length )
64                                 return;
65
66                         disabled = thetags.prop('disabled');
67
68                         current_tags = thetags.val().split( window.tagsBoxL10n.tagDelimiter );
69                         tagchecklist.empty();
70
71                         $.each( current_tags, function( key, val ) {
72                                 var span, xbutton;
73
74                                 val = $.trim( val );
75
76                                 if ( ! val )
77                                         return;
78
79                                 // Create a new span, and ensure the text is properly escaped.
80                                 span = $('<span />').text( val );
81
82                                 // If tags editing isn't disabled, create the X button.
83                                 if ( ! disabled ) {
84                                         xbutton = $( '<a id="' + id + '-check-num-' + key + '" class="ntdelbutton" tabindex="0">X</a>' );
85
86                                         xbutton.on( 'click keypress', function( e ) {
87                                                 // Trigger function if pressed Enter - keyboard navigation
88                                                 if ( e.type === 'click' || e.keyCode === 13 ) {
89                                                         // When using keyboard, move focus back to the new tag field.
90                                                         if ( e.keyCode === 13 ) {
91                                                                 $( this ).closest( '.tagsdiv' ).find( 'input.newtag' ).focus();
92                                                         }
93
94                                                         tagBox.parseTags( this );
95                                                 }
96                                         });
97
98                                         span.prepend( '&nbsp;' ).prepend( xbutton );
99                                 }
100
101                                 // Append the span to the tag list.
102                                 tagchecklist.append( span );
103                         });
104                 },
105
106                 flushTags : function( el, a, f ) {
107                         var tagsval, newtags, text,
108                                 tags = $( '.the-tags', el ),
109                                 newtag = $( 'input.newtag', el ),
110                                 comma = window.tagsBoxL10n.tagDelimiter;
111
112                         a = a || false;
113
114                         text = a ? $(a).text() : newtag.val();
115
116                         if ( 'undefined' == typeof( text ) ) {
117                                 return false;
118                         }
119
120                         tagsval = tags.val();
121                         newtags = tagsval ? tagsval + comma + text : text;
122
123                         newtags = this.clean( newtags );
124                         newtags = array_unique_noempty( newtags.split( comma ) ).join( comma );
125                         tags.val( newtags );
126                         this.quickClicks( el );
127
128                         if ( ! a )
129                                 newtag.val('');
130                         if ( 'undefined' == typeof( f ) )
131                                 newtag.focus();
132
133                         return false;
134                 },
135
136                 get : function( id ) {
137                         var tax = id.substr( id.indexOf('-') + 1 );
138
139                         $.post( ajaxurl, { 'action': 'get-tagcloud', 'tax': tax }, function( r, stat ) {
140                                 if ( 0 === r || 'success' != stat ) {
141                                         return;
142                                 }
143
144                                 r = $( '<p id="tagcloud-' + tax + '" class="the-tagcloud">' + r + '</p>' );
145
146                                 $( 'a', r ).click( function() {
147                                         tagBox.flushTags( $( '#' + tax ), this );
148                                         return false;
149                                 });
150
151                                 $( '#' + id ).after( r );
152                         });
153                 },
154
155                 init : function() {
156                         var t = this, ajaxtag = $('div.ajaxtag');
157
158                         $('.tagsdiv').each( function() {
159                                 tagBox.quickClicks(this);
160                         });
161
162                         $('.tagadd', ajaxtag).click(function(){
163                                 t.flushTags( $(this).closest('.tagsdiv') );
164                         });
165
166                         $('input.newtag', ajaxtag).keyup(function(e){
167                                 if ( 13 == e.which ) {
168                                         tagBox.flushTags( $(this).closest('.tagsdiv') );
169                                         return false;
170                                 }
171                         }).keypress(function(e){
172                                 if ( 13 == e.which ) {
173                                         e.preventDefault();
174                                         return false;
175                                 }
176                         }).each( function() {
177                                 var tax = $(this).closest('div.tagsdiv').attr('id');
178                                 $(this).suggest(
179                                         ajaxurl + '?action=ajax-tag-search&tax=' + tax,
180                                         { delay: 500, minchars: 2, multiple: true, multipleSep: window.tagsBoxL10n.tagDelimiter + ' ' }
181                                 );
182                         });
183
184                         // save tags on post save/publish
185                         $('#post').submit(function(){
186                                 $('div.tagsdiv').each( function() {
187                                         tagBox.flushTags(this, false, 1);
188                                 });
189                         });
190
191                         // tag cloud
192                         $('.tagcloud-link').click(function(){
193                                 tagBox.get( $(this).attr('id') );
194                                 $(this).unbind().click(function(){
195                                         $(this).siblings('.the-tagcloud').toggle();
196                                         return false;
197                                 });
198                                 return false;
199                         });
200                 }
201         };
202 }( jQuery ));