jQuery(function($){ $.fn.wpjam_each = function(cb){ return this.each((i, el) => cb($(el), i)); }; $.fn.wpjam_on = function(name, selector, callback, options){ let cb = !_.isFunction(callback) && $.fn[callback] ? (e, ...args) => $(e.currentTarget)[callback](e, ...args) : callback; let {type, wait=300, ...data} = options || {}; if(type == 'throttle'){ cb = _.throttle(cb, wait); }else if(type == 'debounce' || ['submit', 'change', 'click'].includes(name)){ cb = _.debounce(cb, wait, true); } return this.on(name+'.wpjam', (!selector || selector === 'body') ? null : selector, data, cb); }; $.fn.wpjam_init = function(){ let filter = this.is('body') ? ':not(form *)' : ''; let rules = $.fn.wpjam_init.rules = $.fn.wpjam_init.rules || [ [null, '[data-indeterminate]', $el => $el.prop('indeterminate', true).removeAttr('data-indeterminate')] ]; filter && _.each(Object.keys($.fn), handle => { if(handle.startsWith('wpjam_') && _.every(rules, (r) => r[0] !== handle)){ let {selector, events} = $.fn[handle]; rules.push([handle, selector]); _.each(events, event => $('body').wpjam_on(event.name, event.selector || selector, handle, event)); } }); _.each(rules, ([handle, sel, cb])=> sel && this.find(sel.split(',').map(s => s.trim()+filter).join(',')).wpjam_each($el => cb ? cb($el) : $el[handle]())); filter && this.find('form').wpjam_each($el => $el.data('initialized') || $el.data('initialized', true).wpjam_init()); return this; }; $.fn.wpjam_highlight = function(type){ if(type){ this.attr('data-highlight', type); return type == 'delete' ? this : this.hide().fadeIn(1000); } return this.removeAttr('data-highlight'); }; $.fn.wpjam_remove = function(cb){ return this.fadeOut(400, ()=> this.remove() && cb && cb()); }; $.fn.wpjam_control = function(){ let $field = !this.data('type') && this.is(':checkbox, :radio') ? this.closest('.wpjam-choice') : []; return $field[0] ? $field : this; }; $.fn.wpjam_field = function(){ let {type, key, value, button_text: btn, multiple: mu, ...data} = this.data(); let id = this.attr('id'); let name = this.attr('name')+(mu ? '[]' : ''); let $wrap; if(type == 'color'){ $wrap = this.attr('type', 'text').addClass('expandable').val(value).wpColorPicker().closest('.wp-picker-container'); $wrap.prepend($('