/** * @summary ACTUS Xfields administration events. * * @since 0.1.0 * * */ (function( $ ){ ACTUS.XF.EVENTS = { fieldsets : function( mode ){ mode = mode || 'on'; $('body').off('click','.actus-xf-add-fieldset'); $('body').off('click','.actus-xfieldset-bar'); $('body').off('click','.axf-delete-fieldset'); if ( mode.toLowerCase() == 'on' ) { // FIELDSET ADD $('body').on('click','.actus-xf-add-fieldset', ACTUS.XF.ACTIONS.fieldsetAdd ); // FIELDSET EDIT $('body').on('click','.actus-xfieldset-bar', ACTUS.XF.ACTIONS.fieldsetEdit ); // FIELDSET DELETE $('body').on('click', '.axf-delete-fieldset', ACTUS.XF.ACTIONS.fieldsetDelete ); } }, fieldsetEdit : function( mode ){ mode = mode || 'on'; $('body').off('click','#new-and_target'); $('body').off('click','#new-or_target'); $('body').off('click','#new-delete_target'); $('body').off('change','.actus-xfieldset-edit-title'); $('body').off('click','.actus-xfieldset-edit-close'); $('body').off('click','.actus-xfieldset-options-button'); $('body').off('click','.actus-xfieldset-options .axf-switch p'); if ( mode.toLowerCase() == 'on' ) { // CLOSE FIELDSET EDIT $('body').on('click','.actus-xfieldset-edit-close', ACTUS.XF.ACTIONS.fieldsetEditClose ); // FIELDSET EDIT TITLE $('body').on('change', '.actus-xfieldset-edit-title', ACTUS.XF.ACTIONS.fieldsetEditTitle ); // FIELDSET OPTIONS CLICK $('body').on('click','.actus-xfieldset-options-button', ACTUS.XF.RENDER.fieldsetOptions ); // FIELDSET OPTIONS SWITCH CLICK $('body').on('click','.actus-xfieldset-options .axf-switch p ', ACTUS.XF.ACTIONS.fieldsetOptionsSwitch ); // EDIT TARGET CHANGE $('body').on('AXF-CHANGE',function(e, name, value){ //console.log( name +": "+ value ); targetID = currentDropdown.parent().attr('id'); targetIdx = currentDropdown.parent().data('idx'); targetType = currentDropdown.parent().find('#new-target-type').data('value'); title = value.split(',')[1]; value = value.split(',')[0]; currentDropdown.attr('data-value', value); // TYPE if ( name == 'target-type' ) { ACTUS.XF.FS.targets[targetIdx].type = value; ACTUS.XF.FS.targets[targetIdx].type_title = title; var values = ACTUS.XF.defaults.criteria[value].join(','); var titles = values; if( value == 'admin screen' ) { $( '#'+targetID+' div[name="target-condition"]').addClass('disabled'); values = []; $.each( ACTUS.XF.adminPageGroups, function(key){ values.push(key); }) values = values.join(','); } args = { name : 'target-criteria', values : values, titles : titles, plchld : 'criteria' } newCriteria = ACTUS.XF.CREATE.dropdown( args ); $( '#' + targetID + ' .axf-field[name="target-criteria"]' ).remove(); $( '#' + targetID + ' .axf-field[name="target-type"]' ) .after( newCriteria ); $('#'+targetID+' div[name="target-condition"]').addClass('disabled'); $('#'+targetID+' div[name="target-value"]' ).addClass('disabled'); if ( value == 'admin screen' ) { //$('#'+targetID+' div[name="target-criteria"]').addClass('disabled'); } } // CRITERIA if ( name == 'target-criteria' ) { ACTUS.XF.FS.targets[targetIdx].criteria = value; ACTUS.XF.FS.targets[targetIdx].criteria_title = title; $( '#'+targetID+' div[name="target-condition"]').removeClass('disabled').find('.value').html('='); ACTUS.XF.FS.targets[targetIdx].condition = '='; // admin screen if ( ACTUS.XF.FS.targets[targetIdx].type == 'admin screen' ) { var values = ''; var titles = ''; var idx = 0; $.each( ACTUS.XF.adminPages[value], function(name,rec){ if ( idx > 0 ) { titles += ','; values += ','; } titles += name; values += rec; idx++; }) if ( values == "" ) { ACTUS.XF.FS.targets[targetIdx].value = value; } else { args = { name : 'target-value', values : values, titles : titles, plchld : value } newValueField = ACTUS.XF.CREATE.dropdown( args ); $( '#' + targetID + ' .axf-field[name="target-value"]' ).remove(); $( '#'+targetID+' div[name="and_target"]' ) .before( newValueField ); } } // if ( value == 'type' || value == "category" || value == "template" || value == "tag" ) { if ( value == 'type' ) data = 'postTypes'; if ( value == 'category' ) data = 'postCategories'; if ( value == 'tag' ) data = 'postTags'; if ( value == 'template' ) data = 'pageTemplates'; if ( value == 'template' && targetType == 'post' ) data = 'postTemplates'; var values = ''; var titles = ''; var idx = 0; $.each( ACTUS.XF[data], function(name,rec){ if ( idx > 0 ) { titles += ','; values += ','; } if ( value == 'category' || value == "tag" ) { titles += rec.name; values += rec.term_id; } else { titles += name; values += rec; } idx++; }) args = { name : 'target-value', values : values, titles : titles, plchld : value } newValueField = ACTUS.XF.CREATE.dropdown( args ); $( '#' + targetID + ' .axf-field[name="target-value"]' ).remove(); $( '#'+targetID+' div[name="and_target"]' ) .before( newValueField ); } if ( value == 'id' || value == "search" || value == "parent" ) { plchld = 'post ID'; if ( type == 'page' ) plchld = 'page ID'; if ( value == "search" || value == "parent" ) { $( '#'+targetID+' div[name="target-condition"]').addClass('disabled'); plchld = 'search'; } type =$( '#'+targetID+' div[name="target-type"]').attr('data-value'); args = { type : type, name : 'target-value', plchld : plchld } newValueField = ACTUS.XF.CREATE.dynamicSearch( args ); $( '#' + targetID + ' .axf-field[name="target-value"]' ).remove(); $( '#'+targetID+' div[name="and_target"]' ) .before( newValueField ); } } // CONDITION if ( name == 'target-condition' ) { ACTUS.XF.FS.targets[targetIdx].condition = value; ACTUS.XF.FS.targets[targetIdx].condition_title = title; } // VALUE if ( name == 'target-value' ) { ACTUS.XF.FS.targets[targetIdx].value = value; ACTUS.XF.FS.targets[targetIdx].value_title = title; } }) // add AND TARGET $('body').on('click', '#new-and_target', ACTUS.XF.ACTIONS.targetAddAND ); // add OR TARGET $('body').on('click', '#new-or_target', ACTUS.XF.ACTIONS.targetAddOR ); // DELETE TARGET $('body').on('click', '#new-delete_target', ACTUS.XF.ACTIONS.targetDelete ); } }, fieldsEdit : function( mode ){ mode = mode || 'on'; $('body').off('click','.actus-xfieldset-edit-fields .axf-field'); $('body').off('click','.axf-delete-xfield'); $('body').off('change','.axf-option input'); $('body').off('click','.axf-width p'); $('body').off('click','.axf-size p'); $('body').off('click','.axf-label-pos-toggle p'); $('body').off('click','.axf-option-multi-box .axf-add'); $('body').off('click','.axf-option-multi-box p img'); if ( mode.toLowerCase() == 'on' ) { // SELECT FIELD $('body').on('click','.actus-xfieldset-edit-fields .axf-field',function(e){ e.stopPropagation(); ACTUS.XF.FID = $(this).attr( 'id' ); ACTUS.XF.F = ACTUS.XF.FS.xfields[ ACTUS.XF.FID ]; name = $(this).attr( 'name' ); ACTUS.XF.RENDER.options(); }) // DELETE FIELD $('body').on('click','.axf-delete-xfield',function(e){ if ( ACTUS.XF.F.type == 'group' || ACTUS.XF.F.type == 'multi' ) { $.each( ACTUS.XF.FS.xfields, function(){ if ( this.parent_group == ACTUS.XF.F.id ) delete ACTUS.XF.FS.xfields[this.id]; }) } ACTUS.XF.fieldnames .splice($.inArray(ACTUS.XF.F.name, ACTUS.XF.fieldnames),1); delete ACTUS.XF.FS.xfields[ACTUS.XF.F.id]; delete ACTUS.XF.F; $( '.actus-xfields-options' ).removeClass( 'open' ); ACTUS.XF.RENDER.XFields( ACTUS.XF.FSID ); }) // OPTION CLICK $('body').on('click','.axf-option input',function(e){ val = $(this).val(); name = $(this).attr( 'name' ); if ( name == 'name' ) { oldName = val; } }) // OPTION CHANGE $('body').on('change','.axf-option input',function(e){ name = $(this).attr( 'name' ); val = $(this).val(); $('body').trigger('AXF-OPTION-CHANGE',[ name, val]); }) $('body').on('AXF-OPTION-CHANGE', function(e,name,val){ if ( name == 'dropdown_default' ) name = 'default'; if ( name == 'values' || name == 'titles' ) return; if ( name == 'name' ) { ACTUS.TOOLS.rename( val, ACTUS.XF.fieldnames, function( newName ){ ACTUS.XF.fieldnames .splice(ACTUS.XF.fieldnames.indexOf(oldName),1); ACTUS.XF.fieldnames.push( newName ); ACTUS.XF.F.name = newName; $('.axf-option input[name="name"]').val( newName ); ACTUS.XF.RENDER.XFields( ACTUS.XF.FSID ); }) } ACTUS.XF.F[ name ] = val; ACTUS.XF.RENDER.XFields( ACTUS.XF.FSID ); }) // OPTION WIDTH CHANGE $('body').on('click','.axf-width p',function(e){ $('.axf-width p').removeClass('ON'); $(this).addClass('ON'); widthClass = $(this).attr('alt'); ACTUS.XF.F.width = widthClass; ACTUS.XF.RENDER.XFields( ACTUS.XF.FSID ); }) // OPTION SIZE CHANGE $('body').on('click','.axf-size p',function(e){ $('.axf-size p').removeClass('ON'); $(this).addClass('ON'); sizeClass = $(this).attr('alt'); ACTUS.XF.F.size = sizeClass; ACTUS.XF.RENDER.XFields( ACTUS.XF.FSID ); }) // OPTION LABEL POSITION CHANGE $('body').on('click','.axf-label-pos-toggle p',function(e){ $('.axf-label-pos-toggle p').removeClass('ON'); $(this).addClass('ON'); labelPos = $(this).attr('alt'); ACTUS.XF.F.label_pos = labelPos; ACTUS.XF.RENDER.XFields( ACTUS.XF.FSID ); }) // DROPDOWN VALUE/TITLE ADD $('body').on('click','.axf-option-multi-box .axf-add',function(e){ val = $.trim( $(this).siblings('input').val() ); name = $(this).siblings('input').attr('name'); if ( val == '' ) return; $(this).siblings('input').val(''); ACTUS.XF.F[ name ] = ACTUS.XF.F[ name ] || []; ACTUS.XF.F[ name ].push( $.trim( val ) ); $( '.axf-option-' + name + '-box > p' ).remove(); $.each(ACTUS.XF.F[ name ], function(i,v){ valueH = '
' + (i+1) + '' + v + ACTUS.XF.images.icon_x_img +'
'; $( '.axf-option-' + name + '-box input' ).before( valueH ); }) }) // DROPDOWN VALUE/TITLE DELETE $('body').on('click','.axf-option-multi-box p img',function(e){ idx = parseInt($(this).siblings('span').text()) - 1; $(this).siblings('span').remove(); val = $(this).parent().text(); name = $(this).parent().siblings('input').attr('name'); ACTUS.XF.F[ name ].splice($.inArray(val, ACTUS.XF.F[ name ]), 1); $( '.axf-option-' + name + '-box > p' ).remove(); $.each(ACTUS.XF.F[ name ], function(i,v){ valueH = '' + (i+1) + '' + v + ACTUS.XF.images.icon_x_img +'
'; $( '.axf-option-' + name + '-box input' ).before( valueH ); }) }) // OPTION REC WIDTH CHANGE $('body').on('click','.axf-recwidth p',function(e){ $('.axf-recwidth p').removeClass('ON'); $(this).addClass('ON'); widthClass = $(this).attr('alt'); ACTUS.XF.F.recwidth = widthClass; ACTUS.XF.RENDER.XFields( ACTUS.XF.FSID ); }) } } } // INIT EVENTS if ( actusXFparams.options.current_screen.id == 'actus_page_actus-xfields' ) ACTUS.XF.EVENTS.fieldsets(); $(document).ready(function() { $('' + post.ID+': '+post.post_title+'
'; }) listL = currentField.parent().offset().left; listT = parseInt( currentField.parent().offset().top + currentField.parent().height() ); listW = parseInt( currentField.width() ); listL = currentField.offset().left; $('#axf-search-list').html( listH ); if ( (listT + $('#axf-search-list').height() + 16) > ( $(window).height() + $(window).scrollTop()) ) { listT = currentField.parent().offset().top - $('#axf-search-list').height() - currentField.parent().height() + 10; } $( '#axf-search-list' ) .css({ left: listL+'px', top: listT+'px', width: listW+'px' }) .slideDown(150); } else { $('#axf-search-list').html( '' ).slideUp(100); } }); } else { $('#axf-search-list').html( '' ).slideUp(100); } }) $('body').on('click','#axf-search-list p',function(e){ e.stopPropagation(); currentSearchValue = $(this).text(); if ( criteria == 'id' ) currentSearchValue = $(this).data('id') + ',' + $(this).data('id'); $('#axf-search-list').slideUp(100); currentField.data('value',currentSearchValue); currentField.find('input').val( currentSearchValue ); $('body').trigger('AXF-CHANGE',[ currentFieldName, currentSearchValue]); }) // DROPDOWN $('body').off('click','.axf-dropdown'); $('body').off('click','#axf-dropdown-list p'); $('body').on('click','.axf-dropdown',function(e){ if ( $(this).hasClass('disabled') ) return; if ( $(this).hasClass('AXF-drag') ) return; e.stopPropagation(); currentDropdown = $(this); currentDropdownName = $(this).attr('name'); //$('#axf-dropdown-list').slideUp(100); $('#axf-search-list').html( '' ).slideUp(100); titles = $(this).find('.value').data('titles'); values = $(this).find('.value').data('values'); if (typeof(values) === 'undefined' ) values = ''; if (typeof(titles) === 'undefined' ) titles = values; titles = titles.toString().split(','); values = values.toString().split(','); listH = ''; $.each(titles,function(i,label){ listH += ''+label+'
'; }) listT = parseInt( currentDropdown.offset().top + currentDropdown.height() ); listW = parseInt( currentDropdown.width() ) - 16; $('#axf-dropdown-list').html( listH ); //if ( listW > 320 ) listW = 336; listL = currentDropdown.offset().left + ( currentDropdown.width() - (listW+8) ); if ( (listT + $('#axf-dropdown-list').height() + 16) > ( $(window).height() + $(window).scrollTop()) ) { listT = currentDropdown.offset().top - $('#axf-dropdown-list').height() - currentDropdown.height() + 10; } $( '#axf-dropdown-list' ) .css({ left: listL+'px', top: listT+'px', width: listW+'px' }) .slideDown(150); $( '#axf-dropdown-list' ).removeClass( 'txtL' ); if ( $( this ).hasClass( 'dropL' ) ) { $( '#axf-dropdown-list' ).addClass( 'txtL' ); } }) $('body').on('click','#axf-dropdown-list p',function(e){ e.stopPropagation(); currentDropdownValue = $(this).attr('alt'); currentDropdownLabel = $(this).text(); $('#axf-dropdown-list').slideUp(100); currentDropdown.data('value',currentDropdownValue); currentDropdown.find('.value').text( currentDropdownLabel ); currentDropdown.find('input').val( currentDropdownLabel ); //currentDropdownValue += ',' + currentDropdownLabel; if ( currentDropdown.closest('.actus-xfieldset-edit').length ) $('body').trigger('AXF-CHANGE',[ currentDropdownName, currentDropdownValue]); if ( currentDropdown.closest('.actus-xfields-options').length ) $('body').trigger('AXF-OPTION-CHANGE',[ currentDropdownName, currentDropdownValue]); }) var orig_send_to_editor = window.send_to_editor; // UPLOAD $('body').on('click', '.actus-upload-button', function(e){ var button = $(this); var currentInput = $(this).siblings('input'); var currentImage = $(this).siblings('img'); var _orig_send_attachment = wp.media.editor.send.attachment; var send_attachment_bkp = wp.media.editor.send.attachment; var _custom_media = true; wp.media.editor.send.attachment = function(props, attachment) { if ( _custom_media ) { currentInput.val( attachment.url ); currentImage.attr( 'src', attachment.url ); } else { return _orig_send_attachment.apply( this, [props, attachment] ); }; } wp.media.editor.open(button); return false; }) $('body').on('click', '.actus-upload-buttonX', function(e){ tb_show('actus', 'media-upload.php?type=image&TB_iframe=1'); currentInput = $(this).siblings('input'); currentInputName = currentInput.attr('name'); currentImage = $(this).siblings('img'); window.send_to_editor = function( html ) { imgurl = $(html).attr( 'src' ); currentInput.val( imgurl ); currentImage.attr( 'src', imgurl ); tb_remove(); window.send_to_editor = orig_send_to_editor; } return false; }); // HELP $('body').on('mouseover', '.axf-help-button', function(e){ ACTUS.XF.FID = $(this).closest('.axf-field').attr( 'id' ); ACTUS.XF.FSID = $(this).closest('.axf-fieldset').data( 'id' ); ACTUS.XF.FS = ACTUS.XF.fieldsets[ ACTUS.XF.FSID ]; ACTUS.XF.F = ACTUS.XF.FS.xfields[ ACTUS.XF.FID ]; helpH = '