/* Copyright (c) 2008 Blythe Dunham Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* LazyLoadContentMgr searches the DOM for elements with className "lazyLoadContent". Please copy this file lazyLoadContent.js to your public/javascripts directory. LazyLoadContentMgr is dependent on the prototype libraries. */ var LazyLoadContentMgr = Class.create(); LazyLoadContentMgr.prototype = { parentElement: null, // Parent containing the tab links and tab wrappes lazyloaders: null, // divs with lazyLoad info bulkLoadList: null, // list of items to load in bulk serializedParams: null, /* Create a manager After constructing, use findAndAttach() to connect it to HTML elements. */ initialize: function(parentElementName) { this.parentElement = $(parentElementName || document.body); return(true); }, findAndAttach: function() { // Store the tab selectors and wrappers this.lazyloaders = this.parentElement.select('.lazyLoadContent'); this.bulkLoadList = new Hash(); for (var i=0; i < this.lazyloaders.length; i++) { //evaluate directly lazy load content var evalScript = lazyLoadMgr.lazyloaders[i].getAttribute('lazyloadeval'); if (evalScript != null){ eval(evalScript); // otherwise compile data and make one ajax call } else { var params = this.lazyloaders[i].getAttribute('params'); var hashKey = this.lazyloaders[i].getAttribute('url'); var paramHash = $H(params ? eval("(" + params + ")") : null); // Get the current data stored in the hash if ((curVal = this.bulkLoadList.get(hashKey)) == null) { curVal = ''; } // Add the new parameters. We send an array of hashes to parameter lazy_load curVal += this.serializeBulkParams(curVal, paramHash); // set the hash this.bulkLoadList.set(hashKey, curVal); } } // send each hash as an array if (this.bulkLoadList.keys().length > 0) { this.bulkLoadList.each(this.remote_bulk_update.bind(this)); this.bulkLoadList = null; } }, remote_bulk_update:function(params){ new Ajax.Request(params.first(), { parameters: params.last() }); }, //lazy load is interpreted by ruby as an array of hashes. //each hash contains data indicating which div to update and input for updating it // // link_to "TEST", :action => :test, :arr => [{{:first => '1', 'second' => 2}, {:first => 'first'}] serializeBulkParams:function(serParams, params){ params.each(function(valPair){ serParams += serParams.length == 0 ? '?' : '&'; serParams += 'lazy_load[][' + valPair.first()+ ']=' + valPair.last(); }); return(serParams); } }