Browse files

JS: $.kcPostFinder()

  • Loading branch information...
1 parent 4743558 commit e6e599c5dcd02d22c751d555ac87bf0bde1face6 Dzikri Aziz committed Jul 13, 2012
Showing with 134 additions and 77 deletions.
  1. +133 −76 kc-essentials-inc/scripts/widgets.dev.js
  2. +1 −1 kc-essentials-inc/scripts/widgets.js
View
209 kc-essentials-inc/scripts/widgets.dev.js
@@ -1,100 +1,157 @@
-(function($) {
-
- $(document).ready(function($) {
- // Deps
- $('.widgets-sortables .hasdep').kcFormDep();
- $('.widgets-sortables').ajaxSuccess(function() {
- $('.hasdep', this).kcFormDep();
- });
-
- // Delete tax/meta query row
- $('.kcw-control-block .rm').live('click', function(e) {
+(function($, document) {
+ var
+ func = 'kcPostFinder',
+ active = false,
+ $_doc = $(document),
+ selectors = ['.kc-find-post'],
+ $_box, $_input, $_response, $_submit, $_close,
+ getSelectors = function() {
+ return selectors.join( ', ');
+ }
+ activate = function() {
+ $_input = $('#find-posts-input');
+ $_response = $('#find-posts-response');
+ $_submit = $('#find-posts-submit');
+ $_close = $('#find-posts-close');
+
+ // Insert
+ $_submit.on('click.kcPostFinder', function(e) {
e.preventDefault();
- var $el = $(this),
- $item = $el.parent(),
- $block = $item.parent(),
- $next = $item.next('.row');
-
- $item.slideUp(function() {
- if ( !$item.siblings('.row').length ) {
- $item.find('input[type="text"]').val('');
- $item.find('input[type="checkbox"]').prop('checked', false);
- $item.find('.hasdep').trigger('change');
- } else {
- $item.remove();
- if ( $next.length )
- $block.kcReorder( $el.attr('rel'), true );
- }
- });
- });
-
+ // Be nice!
+ if ( !$_box.data('kcTarget') )
+ return;
- // Add tax/meta query row
- $('.kcw-control-block .add').live('click', function(e) {
- e.preventDefault();
+ var $selected = $_response.find('input:checked');
+ if ( !$selected.length )
+ return false;
- var $el = $(this),
- $item = $el.parent().prev('.row');
+ var $target = $_box.data('kcTarget'),
+ current = $target.val(),
+ current = current === '' ? [] : current.split(','),
+ newID = $selected.val();
- if ( $item.is(':hidden') ) {
- $item.slideDown();
+ if ( $target.is('.unique') ) {
+ $target.val( newID );
}
- else {
- $nu = $item.clone(true).hide();
- $item.after( $nu );
- $nu.slideDown()
- .kcReorder( $el.attr('rel'), false )
- .find('.hasdep').kcFormDep();
+ else if ( $.inArray(newID, current) < 0 ) {
+ current.push(newID);
+ $target.val( current.join(',') );
}
});
- });
+ // Double click on the radios
+ $_doc.on('dblclick.kcPostFinder', 'input[name="found_post_id"]', function() {
+ $_submit.trigger('click.kcPostFinder');
+ });
- // Find posts
- var $findBox = $('#find-posts'),
- $found = $('#find-posts-response'),
- $findBoxSubmit = $('#find-posts-submit');
+ // Close
+ $_doc.on('click.kcPostFinder', '#find-posts-close', function() {
+ $_input.val('');
+ $_box.removeData('kcTarget');
+ });
- // Open
- $('input.kc-find-post').live('dblclick', function() {
- $findBox.data('kcTarget', $(this));
+ active = true;
+ },
+ deactivate = function() {
+ unbind();
+ $_submit.off('click.kcPostFinder');
+ $_doc.off('dblclick.kcPostFinder');
+ $_doc.off('click.kcPostFinder');
+ $_box = $_input = $_response = $_submit = $_close = null;
+ active = false;
+ },
+ action = function(e) {
+ $_box.data('kcTarget', $(this));
findPosts.open();
+ },
+ bind = function() {
+ $_doc.on( 'dblclick.kcPostFinder', getSelectors(), action );
+ },
+ unbind = function() {
+ $_doc.off( 'dblclick.kcPostFinder', getSelectors(), action );
+ },
+ publicMethod = $[func] = function( sel ) {
+ var $this = this;
+
+ if ( active ) {
+ if ( !sel )
+ return;
+
+ unbind();
+ }
+ else {
+ $_box = $('#find-posts');
+ if ( !$_box.length )
+ return;
+
+ activate();
+ }
+
+ if ( sel )
+ selectors = selectors.concat( sel.split(',') );
+
+ bind();
+
+ return $this;
+ };
+
+ publicMethod.destroy = function() {
+ deactivate();
+ };
+}(jQuery, document));
+
+
+(function($) {
+ var $doc = $(document);
+
+ // Deps
+ $('.widgets-sortables .hasdep').kcFormDep();
+ $('.widgets-sortables').ajaxSuccess(function() {
+ $('.hasdep', this).kcFormDep();
});
- // Insert
- $findBoxSubmit.click(function(e) {
+ // Delete tax/meta query row
+ $('.kcw-control-block .rm').live('click', function(e) {
e.preventDefault();
- // Be nice!
- if ( !$findBox.data('kcTarget') )
- return;
+ var $el = $(this),
+ $item = $el.parent(),
+ $block = $item.parent(),
+ $next = $item.next('.row');
+
+ $item.slideUp(function() {
+ if ( !$item.siblings('.row').length ) {
+ $item.find('input[type="text"]').val('');
+ $item.find('input[type="checkbox"]').prop('checked', false);
+ $item.find('.hasdep').trigger('change');
+ } else {
+ $item.remove();
+ if ( $next.length )
+ $block.kcReorder( $el.attr('rel'), true );
+ }
+ });
+ });
+
- var $selected = $found.find('input:checked');
- if ( !$selected.length )
- return false;
+ // Add tax/meta query row
+ $('.kcw-control-block .add').live('click', function(e) {
+ e.preventDefault();
- var $target = $findBox.data('kcTarget'),
- current = $target.val(),
- current = current === '' ? [] : current.split(','),
- newID = $selected.val();
+ var $el = $(this),
+ $item = $el.parent().prev('.row');
- if ( $target.is('.unique') ) {
- $target.val( newID );
+ if ( $item.is(':hidden') ) {
+ $item.slideDown();
}
- else if ( $.inArray(newID, current) < 0 ) {
- current.push(newID);
- $target.val( current.join(',') );
+ else {
+ $nu = $item.clone(true).hide();
+ $item.after( $nu );
+ $nu.slideDown()
+ .kcReorder( $el.attr('rel'), false )
+ .find('.hasdep').kcFormDep();
}
});
- // Double click on the radios
- $('input[name="found_post_id"]', $findBox).live('dblclick', function() {
- $findBoxSubmit.trigger('click');
- });
-
- // Close
- $( '#find-posts-close' ).click(function() {
- $findBox.removeData('kcTarget');
- });
+ $.kcPostFinder();
})(jQuery);
View
2 kc-essentials-inc/scripts/widgets.js
@@ -1 +1 @@
-(function(d){d(document).ready(function(e){e(".widgets-sortables .hasdep").kcFormDep();e(".widgets-sortables").ajaxSuccess(function(){e(".hasdep",this).kcFormDep()});e(".kcw-control-block .rm").live("click",function(j){j.preventDefault();var h=e(this),g=h.parent(),i=g.parent(),f=g.next(".row");g.slideUp(function(){if(!g.siblings(".row").length){g.find('input[type="text"]').val("");g.find('input[type="checkbox"]').prop("checked",false);g.find(".hasdep").trigger("change")}else{g.remove();if(f.length){i.kcReorder(h.attr("rel"),true)}}})});e(".kcw-control-block .add").live("click",function(h){h.preventDefault();var g=e(this),f=g.parent().prev(".row");if(f.is(":hidden")){f.slideDown()}else{$nu=f.clone(true).hide();f.after($nu);$nu.slideDown().kcReorder(g.attr("rel"),false).find(".hasdep").kcFormDep()}})});var c=d("#find-posts"),b=d("#find-posts-response"),a=d("#find-posts-submit");d("input.kc-find-post").live("dblclick",function(){c.data("kcTarget",d(this));findPosts.open()});a.click(function(j){j.preventDefault();if(!c.data("kcTarget")){return}var h=b.find("input:checked");if(!h.length){return false}var f=c.data("kcTarget"),i=f.val(),i=i===""?[]:i.split(","),g=h.val();if(f.is(".unique")){f.val(g)}else{if(d.inArray(g,i)<0){i.push(g);f.val(i.join(","))}}});d('input[name="found_post_id"]',c).live("dblclick",function(){a.trigger("click")});d("#find-posts-close").click(function(){c.removeData("kcTarget")})})(jQuery);
+(function(e,i){var a="kcPostFinder",c=false,l=e(i),k=[".kc-find-post"],g,f,b,j,h,d=function(){return k.join(", ")};activate=function(){f=e("#find-posts-input");b=e("#find-posts-response");j=e("#find-posts-submit");h=e("#find-posts-close");j.on("click.kcPostFinder",function(q){q.preventDefault();if(!g.data("kcTarget")){return}var o=b.find("input:checked");if(!o.length){return false}var m=g.data("kcTarget"),p=m.val(),p=p===""?[]:p.split(","),n=o.val();if(m.is(".unique")){m.val(n)}else{if(e.inArray(n,p)<0){p.push(n);m.val(p.join(","))}}});l.on("dblclick.kcPostFinder",'input[name="found_post_id"]',function(){j.trigger("click.kcPostFinder")});l.on("click.kcPostFinder","#find-posts-close",function(){f.val("");g.removeData("kcTarget")});c=true},deactivate=function(){unbind();j.off("click.kcPostFinder");l.off("dblclick.kcPostFinder");l.off("click.kcPostFinder");g=f=b=j=h=null;c=false},action=function(m){g.data("kcTarget",e(this));findPosts.open()},bind=function(){l.on("dblclick.kcPostFinder",d(),action)},unbind=function(){l.off("dblclick.kcPostFinder",d(),action)},publicMethod=e[a]=function(m){var n=this;if(c){if(!m){return}unbind()}else{g=e("#find-posts");if(!g.length){return}activate()}if(m){k=k.concat(m.split(","))}bind();return n};publicMethod.destroy=function(){deactivate()}}(jQuery,document));(function(a){var b=a(document);a(".widgets-sortables .hasdep").kcFormDep();a(".widgets-sortables").ajaxSuccess(function(){a(".hasdep",this).kcFormDep()});a(".kcw-control-block .rm").live("click",function(h){h.preventDefault();var f=a(this),d=f.parent(),g=d.parent(),c=d.next(".row");d.slideUp(function(){if(!d.siblings(".row").length){d.find('input[type="text"]').val("");d.find('input[type="checkbox"]').prop("checked",false);d.find(".hasdep").trigger("change")}else{d.remove();if(c.length){g.kcReorder(f.attr("rel"),true)}}})});a(".kcw-control-block .add").live("click",function(f){f.preventDefault();var d=a(this),c=d.parent().prev(".row");if(c.is(":hidden")){c.slideDown()}else{$nu=c.clone(true).hide();c.after($nu);$nu.slideDown().kcReorder(d.attr("rel"),false).find(".hasdep").kcFormDep()}});a.kcPostFinder()})(jQuery);

0 comments on commit e6e599c

Please sign in to comment.