spud.admin.SplitPane = (function(){ function SplitPane(selector){ var container = this.container = $(selector); this.rightPane = container.find('.split-pane-right'); this.leftPane = container.find('.split-pane-left'); this.contentArea = container.find('.split-pane-right-content'); this._listUrl = window.location.pathname; container.on('click', '.split-pane-item', this, clickedItem); container.on('click', '.pagination a', this, clickedPageLink); container.on('submit', '.split-pane-search-form', this, submittedSearchForm); container.on('click', '.split-pane-search-clear-btn', this, clickedClearSearch); container.find('.split-pane-search-clear-btn').hide(); this.triggerFirstItem(); } // Event Handlers ///////////////// var clickedItem = function(e){ var splitPane = e.data; item = $(this); splitPane.setActiveItem(item); url = item.attr('data-url'); title = item.attr('data-title') || ''; $.ajax(url, { dataType:'html', success:function(html){ splitPane.setContentAreaHtml(html); splitPane.setToolbarTitle(title); } }); }; var clickedPageLink = function(e){ e.preventDefault(); var splitPane = e.data; var url = $(this).attr('href'); splitPane.refreshList(url, function(){ splitPane.triggerFirstItem(); }); }; var clickedClearSearch = function(e){ e.preventDefault(); var splitPane = e.data; $('.split-pane-search-form input').val(''); var btn = $(this); btn.hide(); splitPane.refreshList(btn.attr('href')); }; var submittedSearchForm = function(e){ e.preventDefault(); var splitPane = e.data; splitPane.container.find('.split-pane-search-clear-btn').show(); var form = $(this); var url = form.attr('action') + '?' + form.serialize(); splitPane.refreshList(url); }; // Instance Methods /////////////////// SplitPane.prototype.refreshList = function(url, callback){ if(typeof(url) != 'undefined'){ this._listUrl = url; } var activeId = this.getActiveItem().attr('data-id'); var self = this; $.ajax(this._listUrl, { success:function(html){ self.setLeftSideHtml(html); var originalItem = self.leftPane.find('[data-id='+activeId+']'); if(originalItem.length){ self.setActiveItem(originalItem); } if(typeof(callback) == 'function'){ callback.call(self); } } }); }; SplitPane.prototype.setActiveItem = function(item){ this.deselectActiveItem(); item.addClass('split-pane-item-selected'); }; SplitPane.prototype.getActiveItem = function(){ return this.container.find('.split-pane-item-selected'); }; SplitPane.prototype.deselectActiveItem = function(){ this.getActiveItem().removeClass('split-pane-item-selected'); }; SplitPane.prototype.triggerFirstItem = function(){ this.container.find('.split-pane-item').first().click(); }; SplitPane.prototype.setToolbarTitle = function(title){ if(typeof(title) == 'undefined'){ title = ''; } this.container.find('.split-pane-toolbar-title').text(title); }; SplitPane.prototype.setContentAreaHtml = function(html){ this.contentArea.html(html); this.rightPane.scrollTop(0); }; SplitPane.prototype.getContentAreaHtml = function(){ return this.contentArea.html(); }; SplitPane.prototype.removeCurrentlySelectedItem = function(){ var activeItem = this.getActiveItem(); var sibling = activeItem.next(); if(!sibling){ sibling = activeItem.prev(); } this.getActiveItem().remove(); if(sibling){ sibling.click(); } else{ this.setContentAreaHtml(''); } }; SplitPane.prototype.setLeftSideHtml = function(html){ this.leftPane.html(html); }; SplitPane.prototype.find = function(selector){ return this.contentArea.find(selector); }; return SplitPane; })();