From ef68436ac04da078ffdcacd7e1f785473a303d45 Mon Sep 17 00:00:00 2001 From: Giulio Cesare Solaroli Date: Sun, 02 Oct 2011 23:56:18 +0000 Subject: First version of the newly restructured repository --- (limited to 'frontend/beta/js/YUI-extensions/widgets/Toolbar.js') diff --git a/frontend/beta/js/YUI-extensions/widgets/Toolbar.js b/frontend/beta/js/YUI-extensions/widgets/Toolbar.js new file mode 100644 index 0000000..7c14753 --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/widgets/Toolbar.js @@ -0,0 +1,296 @@ +/** + * @class YAHOO.ext.Toolbar + * Basic Toolbar used by the Grid to create the paging toolbar. This class is reusable but functionality + * is limited. Look for more functionality in a future version. + * @constructor + * @param {String/HTMLElement/Element} container + * @param {Array} buttons (optional) array of button configs or elements to add + */ + YAHOO.ext.Toolbar = function(container, buttons){ + this.el = getEl(container, true); + var div = document.createElement('div'); + div.className = 'ytoolbar'; + var tb = document.createElement('table'); + tb.border = 0; + tb.cellPadding = 0; + tb.cellSpacing = 0; + div.appendChild(tb); + var tbody = document.createElement('tbody'); + tb.appendChild(tbody); + var tr = document.createElement('tr'); + tbody.appendChild(tr); + this.el.dom.appendChild(div); + this.tr = tr; + if(buttons){ + this.add.apply(this, buttons); + } +}; + +YAHOO.ext.Toolbar.prototype = { + /** + * Adds element(s) to the toolbar - this function takes a variable number of + * arguments of mixed type and adds them to the toolbar... + * + * @param {Mixed} arg If arg is a ToolbarButton, it is added. If arg is a string, it is wrapped + * in a ytb-text element and added unless the text is "separator" in which case a separator + * is added. Otherwise, it is assumed the element is an HTMLElement and it is added directly. + */ + add : function(){ + for(var i = 0; i < arguments.length; i++){ + var el = arguments[i]; + var td = document.createElement('td'); + this.tr.appendChild(td); + if(el instanceof YAHOO.ext.ToolbarButton){ + el.init(td); + }else if(el instanceof Array){ + this.addButton(el); + }else if(typeof el == 'string'){ + var span = document.createElement('span'); + if(el == 'separator'){ + span.className = 'ytb-sep'; + }else{ + span.innerHTML = el; + span.className = 'ytb-text'; + } + td.appendChild(span); + }else if(typeof el == 'object' && el.nodeType){ // must be element? + td.appendChild(el); + }else if(typeof el == 'object'){ // must be button config? + this.addButton(el); + } + } + }, + + /** + * Returns the element for this toolbar + * @return {YAHOO.ext.Element} + */ + getEl : function(){ + return this.el; + }, + + /** + * Adds a separator + */ + addSeparator : function(){ + var td = document.createElement('td'); + this.tr.appendChild(td); + var span = document.createElement('span'); + span.className = 'ytb-sep'; + td.appendChild(span); + }, + + /** + * Add a button (or buttons), see {@link YAHOO.ext.ToolbarButton} for more info on the config + * @param {Object/Array} config A button config or array of configs + * @return {YAHOO.ext.ToolbarButton/Array} + */ + addButton : function(config){ + if(config instanceof Array){ + var buttons = []; + for(var i = 0, len = config.length; i < len; i++) { + buttons.push(this.addButton(config[i])); + } + return buttons; + } + var b = config; + if(!(config instanceof YAHOO.ext.ToolbarButton)){ + b = new YAHOO.ext.ToolbarButton(config); + } + this.add(b); + return b; + }, + + /** + * Adds text to the toolbar + * @param {String} text The text to add + * @return {HTMLElement} The span element created which you can use to update the text. + */ + addText : function(text){ + var td = document.createElement('td'); + this.tr.appendChild(td); + var span = document.createElement('span'); + span.className = 'ytb-text'; + span.innerHTML = text; + td.appendChild(span); + return span; + }, + + /** + * Inserts a button (or buttons) at the specified index + * @param {Number} index The index where the buttons are to be inserted + * @param {Object/Array} config A button config or array of configs + * @return {YAHOO.ext.ToolbarButton/Array} + */ + insertButton : function(index, config){ + if(config instanceof Array){ + var buttons = []; + for(var i = 0, len = config.length; i < len; i++) { + buttons.push(this.insertButton(index + i, config[i])); + } + return buttons; + } + var b = new YAHOO.ext.ToolbarButton(config); + var td = document.createElement('td'); + var nextSibling = this.tr.childNodes[index]; + if (nextSibling) + this.tr.insertBefore(td, nextSibling); + else + this.tr.appendChild(td); + b.init(td); + return b; + } +}; + +/** + * @class YAHOO.ext.ToolbarButton + * A toolbar button. The config has the following options: + *