'+text+'
').appendTo(notice_area).slideUp(0).slideDown(); }, hideNotice = function(){ var notes = notice_area.children(); if(notes.length){ notes.slideUp().promise().done(function(){notes.remove()}); } }, selectionHandler = function(event){ event.preventDefault(); var method = 'single', selected = all_items.filter('.selected'), $this = $(this), last, clicked; if ( event.shiftKey ) { method = 'between'; } else if ( event.ctrlKey || event.metaKey ) { method = 'toggle'; } //check if there is anything selected if(method === 'single'){ all_items.not($this).removeClass('selected'); $this.toggleClass('selected'); } else{ if(selected.length && method === 'between'){ last = indexOfItem(selected.eq(selected.length - 1)); clicked = indexOfItem($this); if(clicked !== last){ if(clicked < last){ $this.nextUntil(last).andSelf().addClass('selected'); } else{ selected.eq(selected.length - 1).nextUntil($this).add($this).addClass('selected'); } } } else{ $this.toggleClass('selected'); } } //toggle delete button if(all_items.filter('.selected').length){ switchConditionalControls(); } else{ switchConditionalControls(false); } }, switchConditionalControls = function(switcher){ switcher = typeof switcher === 'undefined'? true : switcher; remove_button.prop({ disabled: !switcher }); bulk_taging.find('input').prop({ disabled: !switcher }); if(switcher){ bulk_taging.removeClass('disabled'); } else{ bulk_taging.addClass('disabled'); } }, removeSelected = function(){ var selected = all_items.filter('.selected'), number = selected.length; if(number){ selected.each(function(){ var t = $(this); removeItem(t, true); }); //remove HTML selected.fadeOut(250,function(){ selected.remove(); }); updateTextarea(); showNotice('Removed '+number+' items'); } //disable button switchConditionalControls(false); }, itemTags = function(args){ //get params args = $.extend({ filter_container : undefined, index : undefined, single : true }, args ); var filter_container = args.filter_container, index = args.index, singleItem = args.single, breakTags = function (string_tags) { if (typeof string_tags !== 'undefined') { var tags = string_tags.split(','), temp_arr = []; for (var i = 0; i < tags.length; i++) { if (tags[i].length) { temp_arr.push($.trim(tags[i])); } } return temp_arr; } return []; }, uniqueArray = function (array) { var n = [array[0]]; for (var i = 1; i < array.length; i++) { if (array.indexOf(array[i]) == i) n.push(array[i]); } return n; }, difference = function (a1, a2) { var result = []; for (var i = 0; i < a1.length; i++) { if (a2.indexOf(a1[i]) === -1) { result.push(a1[i]); } } return result; }, collectAllExistingAndCurrentTags = function(){ var temp_array = []; for (var i = 0, end = items_JSON.length; i < end; i++) { temp_array[i] = items_JSON[i][items_JSON[i].type + '_tags']; all_tags = all_tags.concat(breakTags(temp_array[i])); } //unique list if (all_tags.length) { all_tags = uniqueArray(all_tags); } if(singleItem){ //get filters for current item current_tags = breakTags(temp_array[index]); } }, updateCurrentTags = function(){ var temp_html = ''; for (var i = 0; i < current_tags.length; i++) { temp_html += ' ' + current_tags[i] + ''; } current_tags_container.empty().append(temp_html); }, updateAvailableTags = function() { available_tags = difference(all_tags, current_tags); }, updateTagsTextarea = function(){ tags_textarea.val(current_tags.join(', ')); }, addTag = function(tag){ current_tags.push(tag); updateTagsTextarea(); updateCurrentTags(); updateAvailableTags(); }, removeTag = function(tag){ var where_it_is = current_tags.indexOf(tag); current_tags.splice(where_it_is, 1); updateTagsTextarea(); updateCurrentTags(); updateAvailableTags(); }, inputTagsHandler = function (e) { //don't look for "enter" when autocomplete is open if ( $(this).autocomplete("instance").menu.active ){ return; } if (e.keyCode == 13) { e.preventDefault(); e.stopPropagation(); add_tag_button.click(); return false; } }, buttonTagsHandler = function(){ var value = add_tag_input.val(), tags = breakTags(value), temp; if(singleItem){ for (var i = 0; i < tags.length; i++) { temp = $.trim(tags[i]); if (temp.length && current_tags.indexOf(temp) < 0) { addTag(temp); } } //reset tag input add_tag_input.val(''); } else{ //get selected itesm var selected = all_items.filter('.selected'), number = selected.length; if(tags.length === 0){ showNotice('No tags added.'); } else if(number) { //removes "," that was at end of last value //tags = tags.join(', '); //add tags for each item selected.each(function () { var index = indexOfItem($(this)), item = items_JSON[index], type = item.type, old_tags = breakTags(item[type + '_tags']), new_item_part = []; //combine old and new tags new_item_part[type + '_tags'] = uniqueArray(old_tags.concat(tags)).join(', '); //add new tags to item $.extend(item, new_item_part); }); //update things updateTextarea(); updateAvailableTags(); showNotice('Updated ' + number + ' items with tags: ' + tags.join(', ')); //reset tag input add_tag_input.val(''); } //user didn't select anything else{ showNotice('No items selected.'); } } }, removeTagsHandler = function(e){ e.preventDefault(); removeTag($(this).nextAll('em').text()); }, split = function( val ){ return val.split( /,\s*/ ); }, extractLast = function ( term ){ return split( term ).pop(); }, bindEvents = function(){ if(singleItem){ current_tags_container.on('click', 'a', removeTagsHandler); $body.on('a13_before_item_save', buttonTagsHandler); $body.on('a13_close_fieldset', unbindEvents); } add_tag_button.on('click', buttonTagsHandler); add_tag_input.on('keydown', inputTagsHandler); add_tag_input.autocomplete({ autoFocus: true, minLength: 0, delay: 50, source: function( request, response ) { // delegate back to autocomplete, but extract the last term response( $.ui.autocomplete.filter( available_tags, extractLast( request.term ) ) ); }, focus: function() { // prevent value insert on focus - good for multi-tagging at one go return false; }, select: function( event, ui ) { var terms = split( this.value ); // remove the current input terms.pop(); // add the selected item terms.push( ui.item.value ); // add placeholder to get the comma-and-space at the end terms.push( "" ); this.value = terms.join( ", " ); return false; } }); }, unbindEvents = function(){ if(singleItem) { current_tags_container.off('click', 'a', removeTagsHandler); $body.off('a13_before_item_save', buttonTagsHandler); $body.off('a13_close_fieldset', unbindEvents); } add_tag_button.off('click', buttonTagsHandler); add_tag_input.off('keydown', inputTagsHandler); add_tag_input.autocomplete( "destroy" ); current_tags = []; }, tags_textarea = filter_container.find('textarea'), current_tags_container = filter_container.find('div.current-tags'), add_tag_button = filter_container.find('input.tagadd'), add_tag_input = filter_container.find('input.newtag'), available_tags = [], all_tags = [], current_tags = []; //collect all available tags collectAllExistingAndCurrentTags(); //remove currently used filters from list of available filters updateAvailableTags(); if(singleItem){ //print controls updateCurrentTags(); } bindEvents(); }, //not implemented yet /*workingStatusOn = function(){ }, workingStatusOff = function(){ },*/ setColumns = function() { var prev = columns, width = sort_area.width(); if ( width ) { columns = Math.min( Math.round( width / ideal_column_width ), 12 ) || 1; if ( ! prev || prev !== columns ) { sort_area.attr( 'data-columns', columns ); } } }; collectDefaults(); updateAllItems(); setColumns(); //bind actions our_skt_meta .on('click', 'span.add-link-media', {}, addItem) .on('click', 'input.a13_mu_save', {}, updateItem) .on('click', 'input.a13_mu_cancel', {}, closeFieldset); //actions on single gallery item sort_area .on('click', 'span.mu-item-remove', {}, removeAction) .on('click', 'span.mu-item-edit', {}, editAction) .on('click keydown', item_selector, selectionHandler) .sortable({ handle: 'div.mu-item-drag', items: item_selector, placeholder : 'sort-placeholder attachment', start: itemsSortStart, update: itemsSortUpdate }) .disableSelection(); //remove many items remove_button.on('click', removeSelected); //prepare bulk tag input itemTags({ filter_container : bulk_taging, single : false }); $(window).resize(debounce(setColumns, 250)); //enable multi upload mu_button.click(muUploadFile); //hide notice on click notice_area.click(hideNotice); } }, compareDependency : function(dependency, possible_switches){ var parent = dependency[0], operator = dependency[1], value = dependency[2], parent_value = possible_switches.filter( '[name="'+G.input_prefix+parent+'"]' ); //check if such input can be found if(parent_value.length){ //radio input? special work to do! if(parent_value.is('[type="radio"]')){ parent_value = parent_value.filter(':checked').val(); } //classic else{ parent_value = parent_value.val() } } else{ return false; } //check operators if(operator === '='){ return value === parent_value; } else if(operator === '!='){ return value !== parent_value; } //for all other operators return false; }, checkDependencies : function(){ var input = $(this); if(input.is('.not-to-collect')){ return; } var name = input.attr('name'), id = name.substr(G.input_prefix.length), meta_areas = $('div.sktwb-metas'), possible_switches = meta_areas.find('input[type="radio"], select, input[type="hidden"]'), show_it; if(typeof G.list_of_dependent[id] !== 'undefined'){ //we check what fields are affected by changing current input $.each(G.list_of_dependent[id], function(){ //looking for requirements for this fields var dependency = G.list_of_requirements[this] || '', field = meta_areas.find('[name="'+ G.input_prefix+this+'"]').closest('div.input-parent'); if(dependency.length){ show_it = true; //we have more then one required condition if(Array.isArray(dependency[0])){ for(var i = 0; i < dependency.length; i++ ){ if(!BARTER_ADMIN.metaActions.compareDependency(dependency[i], possible_switches)){ //some dependency were not met show_it = false; break; } } } //we have only one required condition else { if(!BARTER_ADMIN.metaActions.compareDependency(dependency, possible_switches)){ //dependency were not met show_it = false; } } //show or hide if(show_it){ field.show(); } else{ field.hide(); } } }); } } }, settingsAction : function(){ var a13_before_page_save = function(){ //check if row shortcode was cloned - in case it has active nava settings - change timestamp to avoid problems var content_ifr = $('#content_ifr'), content = content_ifr.length ? content_ifr.contents().find('#tinymce').html() : $('#content').val(), matches1, G = SKTParams; if( typeof( content ) === 'undefined' ){ return true; } matches1 = content.match(/a13_nava_id="[^"]+"/g); //now search for anchor duplicates if( matches1 && matches1.length>0 ){ var sorted_arr1 = matches1.sort(); var duplicates1 = ''; for (var i = 0; i < matches1.length - 1; i++) { if (sorted_arr1[i + 1] == sorted_arr1[i] && sorted_arr1[i] != 'a13_nava_id="-1"' ) { duplicates1 = duplicates1 + G['nava'][sorted_arr1[i].match(/[0-9 -()+]+$/)[0].toString().replace('"','').replace('"','')] + ', '; } } if ( duplicates1 != '' ) { duplicates1 = duplicates1.substring(0,(duplicates1.length - 2)); alert(G['messages']['duplicate_nava_anchors']+' '+duplicates1); return false; } } return true; }; BARTER_ADMIN.ratingNotice(); BARTER_ADMIN.proofingNotice(); BARTER_ADMIN.adminNotices(); //save options button - back to current fieldset after reload $('input[name="theme_updated"]').click(function(){ var I = $(this), fieldset = I.parents('div.postbox').eq(0).attr('id'), form = I.parents('form').eq(0); form.attr('action', '#'+fieldset); //insert anchor }); //NAVA manipulations //deleting nava from vc_row shortcode edit panel $('#vc_ui-panel-edit-element').on('click','.a13_delete_selected_nava', function(){ var post = $(this).closest('.edit_form_line').find('.a13_nava_id :selected'); var id = post.val(); if(confirm(G.messages.confirm_delete_nava)){ $.ajax({ type: 'post', url: SKTParams.ajaxurl, data: { action: 'barter_nava_delete_post', id: id }, success: function( result ) { if( result == 'success' ) { post.remove(); } } }) } return false; } ) //adding new nava from vc_row shortcode edit panel .on('keyup','.a13_new_nava_id', function(e){ if( e.keyCode != 13){ return; } var name = jQuery(this).val(); if( name == '' ){ return; } $.ajax({ type: 'post', url: SKTParams.ajaxurl, data: { action: 'barter_nava_add_post', title: name }, success: function( data ) { $('.a13_nava_id').append('').val(data.new_post_ID); $('.a13_new_nava_id').val(''); G['nava'][data.new_post_ID] = data.new_post_title; } }); return false; } ); //checking if there is no duplicate use of navigation anchors $body.on('click','#publish', a13_before_page_save ); }, ratingNotice : function(){ var rating_notice = $('div.rating-notice'); if(rating_notice.length){ var later = rating_notice.find('a[href="#remind-later"]'), disable = rating_notice.find('a[href="#disable-rating"]'), links = later.add(disable); links.on('click', function(e){ e.preventDefault(); rating_notice.hide().remove(); $.ajax({ type: "POST", url: ajaxurl, data: { action : 'barter_rating_notice_action', //called in backend what : $(this).attr('href').substr(1)//no hash }, success: function(reply) { //console.log(reply); }, dataType: 'html' }); }); } }, proofingNotice : function(){ var proofing_notice = $('div#proofing-notice'); if(proofing_notice.length){ $('#hide-proof-notice').on('click', function(e){ e.preventDefault(); proofing_notice.hide().remove(); $.ajax({ type: "POST", url: ajaxurl, data: { action : 'barter_proofing_notice_action' //called in backend }, success: function(reply) { //console.log(reply); }, dataType: 'html' }); }); } }, adminNotices : function(){ var notices = $('div.a13fe-admin-notice'); if(notices.length){ notices.on('click', '.notice-dismiss', function(e){ e.preventDefault(); var notice = $(this).parents('div.notice').eq(0); notice.hide().remove(); $.ajax({ type: "POST", url: G.ajaxurl, data: { action : 'barter_disable_ajax_notice', //called in backend notice_id : notice.data('notice_id') }, success: function(reply) { //console.log(reply); }, dataType: 'html' }); }); } } }; var BARTER_ADMIN = window.BARTER_ADMIN; //start ADMIN $(document).ready(BARTER_ADMIN.onReady); })(jQuery);