(function($){ /* * Relationship * * static model for this field * * @type event * @date 1/06/13 * */ acf.fields.relationship = { $el : null, $input : null, $left : null, $right : null, o : {}, timeout : null, set : function( o ){ // merge in new option $.extend( this, o ); // find elements this.$input = this.$el.children('input[type="hidden"]'); this.$left = this.$el.find('.relationship_left'), this.$right = this.$el.find('.relationship_right'); // get options this.o = acf.helpers.get_atts( this.$el ); // return this for chaining return this; }, init : function(){ // reference var _this = this; // is clone field? if( acf.helpers.is_clone_field(this.$input) ) { return; } // set height of right column this.$right.find('.relationship_list').height( this.$left.height() -2 ); // right sortable this.$right.find('.relationship_list').sortable({ axis : 'y', items : '> li', forceHelperSize : true, forcePlaceholderSize : true, scroll : true, update : function(){ _this.$input.trigger('change'); } }); // load more var $el = this.$el; this.$left.find('.relationship_list').scrollTop( 0 ).on('scroll', function(e){ // validate if( $el.hasClass('loading') || $el.hasClass('no-results') ) { return; } // Scrolled to bottom if( $(this).scrollTop() + $(this).innerHeight() >= $(this).get(0).scrollHeight ) { var paged = parseInt( $el.attr('data-paged') ); // update paged $el.attr('data-paged', (paged + 1) ); // fetch _this.set({ $el : $el }).fetch(); } }); // ajax fetch values for left side this.fetch(); }, fetch : function(){ // reference var _this = this, $el = this.$el; // add loading class, stops scroll loading $el.addClass('loading'); // get results $.ajax({ url : acf.o.ajaxurl, type : 'post', dataType : 'json', data : $.extend({ action : 'acf/fields/relationship/query_posts', post_id : acf.o.post_id, nonce : acf.o.nonce }, this.o ), success : function( json ){ // render _this.set({ $el : $el }).render( json ); } }); }, render : function( json ){ // reference var _this = this; // update classes this.$el.removeClass('no-results').removeClass('loading'); // new search? if( this.o.paged == 1 ) { this.$el.find('.relationship_left li:not(.load-more)').remove(); } // no results? if( ! json || ! json.html ) { this.$el.addClass('no-results'); return; } // append new results this.$el.find('.relationship_left .load-more').before( json.html ); // next page? if( ! json.next_page_exists ) { this.$el.addClass('no-results'); } // apply .hide to left li's this.$left.find('a').each(function(){ var id = $(this).attr('data-post_id'); if( _this.$right.find('a[data-post_id="' + id + '"]').exists() ) { $(this).parent().addClass('hide'); } }); }, add : function( $a ){ // vars var id = $a.attr('data-post_id'), title = $a.html(); // max posts if( this.$right.find('a').length >= this.o.max ) { alert( acf.l10n.relationship.max.replace('{max}', this.o.max) ); return false; } // can be added? if( $a.parent().hasClass('hide') ) { return false; } // hide $a.parent().addClass('hide'); // template var html = [ '