1 /* jshint curly: false, eqeqeq: false */
4 var tagBox, array_unique_noempty;
7 // Return an array with any duplicate, whitespace or empty values removed
8 array_unique_noempty = function( array ) {
11 $.each( array, function( key, val ) {
14 if ( val && $.inArray( val, out ) === -1 ) {
23 clean : function(tags) {
24 var comma = window.tagsBoxL10n.tagDelimiter;
26 tags = tags.replace(new RegExp(comma, 'g'), ',');
27 tags = tags.replace(/\s*,\s*/g, ',').replace(/,+/g, ',').replace(/[,\s]+$/, '').replace(/^[,\s]+/, '');
29 tags = tags.replace(/,/g, comma);
33 parseTags : function(el) {
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 ),
42 delete current_tags[num];
44 $.each( current_tags, function( key, val ) {
51 thetags.val( this.clean( new_tags.join( comma ) ) );
53 this.quickClicks( taxbox );
57 quickClicks : function( el ) {
58 var thetags = $('.the-tags', el),
59 tagchecklist = $('.tagchecklist', el),
60 id = $(el).attr('id'),
61 current_tags, disabled;
63 if ( ! thetags.length )
66 disabled = thetags.prop('disabled');
68 current_tags = thetags.val().split( window.tagsBoxL10n.tagDelimiter );
71 $.each( current_tags, function( key, val ) {
79 // Create a new span, and ensure the text is properly escaped.
80 span = $('<span />').text( val );
82 // If tags editing isn't disabled, create the X button.
84 xbutton = $( '<a id="' + id + '-check-num-' + key + '" class="ntdelbutton" tabindex="0">X</a>' );
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();
94 tagBox.parseTags( this );
98 span.prepend( ' ' ).prepend( xbutton );
101 // Append the span to the tag list.
102 tagchecklist.append( span );
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;
114 text = a ? $(a).text() : newtag.val();
116 if ( 'undefined' == typeof( text ) ) {
120 tagsval = tags.val();
121 newtags = tagsval ? tagsval + comma + text : text;
123 newtags = this.clean( newtags );
124 newtags = array_unique_noempty( newtags.split( comma ) ).join( comma );
126 this.quickClicks( el );
130 if ( 'undefined' == typeof( f ) )
136 get : function( id ) {
137 var tax = id.substr( id.indexOf('-') + 1 );
139 $.post( ajaxurl, { 'action': 'get-tagcloud', 'tax': tax }, function( r, stat ) {
140 if ( 0 === r || 'success' != stat ) {
144 r = $( '<p id="tagcloud-' + tax + '" class="the-tagcloud">' + r + '</p>' );
146 $( 'a', r ).click( function() {
147 tagBox.flushTags( $( '#' + tax ), this );
151 $( '#' + id ).after( r );
156 var t = this, ajaxtag = $('div.ajaxtag');
158 $('.tagsdiv').each( function() {
159 tagBox.quickClicks(this);
162 $('.tagadd', ajaxtag).click(function(){
163 t.flushTags( $(this).closest('.tagsdiv') );
166 $('input.newtag', ajaxtag).keyup(function(e){
167 if ( 13 == e.which ) {
168 tagBox.flushTags( $(this).closest('.tagsdiv') );
171 }).keypress(function(e){
172 if ( 13 == e.which ) {
176 }).each( function() {
177 var tax = $(this).closest('div.tagsdiv').attr('id');
179 ajaxurl + '?action=ajax-tag-search&tax=' + tax,
180 { delay: 500, minchars: 2, multiple: true, multipleSep: window.tagsBoxL10n.tagDelimiter + ' ' }
184 // save tags on post save/publish
185 $('#post').submit(function(){
186 $('div.tagsdiv').each( function() {
187 tagBox.flushTags(this, false, 1);
192 $('.tagcloud-link').click(function(){
193 tagBox.get( $(this).attr('id') );
194 $(this).unbind().click(function(){
195 $(this).siblings('.the-tagcloud').toggle();