From 0b0e10d01d7a3e66dac4cb7d5e1b2c5b7333eb92 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Sat, 21 Nov 2009 02:37:45 +0000 Subject: [PATCH] ENHANCEMENT Better integration between new form javascript and tree in CMS MINOR Various event and property renamings in CMSMain.js git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/cms/trunk@92635 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- javascript/LeftAndMain.js | 76 +++++++++++++++++++++++++++------- javascript/LeftAndMain_left.js | 10 ++++- 2 files changed, 69 insertions(+), 17 deletions(-) diff --git a/javascript/LeftAndMain.js b/javascript/LeftAndMain.js index 57d8213e..c3b43905 100644 --- a/javascript/LeftAndMain.js +++ b/javascript/LeftAndMain.js @@ -24,6 +24,20 @@ this._setupPinging(); this._setupButtons(); this._resizeChildren(); + + // artificially delay the resize event 200ms + // to avoid overlapping height changes in different onresize() methods + $(window).resize(function () { + var timerID = "timerLeftAndMainResize"; + if (window[timerID]) clearTimeout(window[timerID]); + window[timerID] = setTimeout(function() {self._resizeChildren();}, 200); + }); + + // trigger resize whenever new tabs are shown + // @todo This is called multiple times when tabs are loaded + this.find('.ss-tabset').bind('tabsshow', function() {self._resizeChildren();}); + + $('#Form_EditForm').bind('loadnewpage', function() {self._resizeChildren();}); this._super(); }, @@ -101,15 +115,15 @@ }, /** - * @param DOMElement button The pressed button (optiona) + * @param {DOMElement} button The pressed button (optional) */ ajaxSubmit: function(button) { // default to first button if none given - simulates browser behaviour if(!button) button = this.find(':submit:first'); - var $form = this; + var self = this; - this.trigger('beforeSubmit', [button]); + this.trigger('ajaxsubmit', {button: button}); // set button to "submitting" state $(button).addClass('loading'); @@ -119,12 +133,10 @@ // validate if required if(!this.validate()) { - this.trigger('validationError', [button]); - // TODO Automatically switch to the tab/position of the first error statusMessage("Validation failed.", "bad"); - if($('Form_EditForm_action_save') && $('Form_EditForm_action_save').stopLoading) $('Form_EditForm_action_save').stopLoading(); + $(button).removeClass('loading'); return false; } @@ -136,12 +148,10 @@ $.post( this.attr('action'), data, - function(result) { + function(response) { $(button).removeClass('loading'); - $form.trigger('afterSubmit', [result]); - - $form.loadNewPage(); + self._loadResponse(response); }, // @todo Currently all responses are assumed to be evaluated 'script' @@ -159,22 +169,56 @@ * @return {boolean} */ validate: function() { - this.trigger('beforeValidate'); var isValid = true; - this.trigger('afterValidate', [isValid]); + this.trigger('validate', {isValid: isValid}); return isValid; }, - loadNewPage: function(result) { + /** + * @param String url + * @param Function callback (Optional) + */ + load: function(url, callback) { + var self = this; + $.get( + url, + function(response) { + self._loadResponse(response); + if(callback) callback.apply(self, [response]); + }, + // @todo Currently all responses are assumed to be evaluated + 'script' + ); + }, + + /** + * Remove everying inside the
tag + * with a custom HTML fragment. Useful e.g. for deleting a page in the CMS. + * + * @param {String} removeText + */ + remove: function(removeHTML) { + + }, + + + /** + * @param {String} result Either HTML for straight insertion, or eval'ed JavaScript. + * If passed as HTML, it is assumed that everying inside the tag is replaced, + * but the old tag itself stays intact. + */ + _loadResponse: function(response) { // TinyMCE coupling if(typeof tinymce_removeAll != 'undefined') tinymce_removeAll(); + + var html = response; // Rewrite # links - result = result.replace(/(]+href *= *")#/g, '$1' + window.location.href.replace(/#.*$/,'') + '#'); + html = html.replace(/(]+href *= *")#/g, '$1' + window.location.href.replace(/#.*$/,'') + '#'); // Rewrite iframe links (for IE) - result = result.replace(/(]*src=")([^"]+)("[^>]*>)/g, '$1' + $('base').attr('href') + '$2$3'); + html = html.replace(/(]*src=")([^"]+)("[^>]*>)/g, '$1' + $('base').attr('href') + '$2$3'); // Prepare iframes for removal, otherwise we get loading bugs this.find('iframe').each(function() { @@ -182,7 +226,7 @@ this.remove(); }) - this.html(result); + this.html(html); if(this.hasClass('validationerror')) { statusMessage(ss.i18n._t('ModelAdmin.VALIDATIONERROR', 'Validation Error'), 'bad'); diff --git a/javascript/LeftAndMain_left.js b/javascript/LeftAndMain_left.js index 833fbd7a..69483d47 100755 --- a/javascript/LeftAndMain_left.js +++ b/javascript/LeftAndMain_left.js @@ -204,7 +204,15 @@ TreeNodeAPI.prototype = { }, getPageFromServer : function() { - $('Form_EditForm').getPageFromServer(this.id.replace('record-',''), this); + jQuery('#Form_EditForm').concrete('ss').load( + jQuery(this).find('a').attr('href'), + function(response) { + var pageID = jQuery(this).find(':input[name=ID]').val(); + if(pageID) { + jQuery('#sitetree')[0].setCurrentByIdx(pageID); + } + } + ); _AJAX_LOADING = false; }, ajaxExpansion : function() {