Skip to content

Instantly share code, notes, and snippets.

@Satyam
Created December 19, 2011 18:18
Show Gist options
  • Save Satyam/1498249 to your computer and use it in GitHub Desktop.
Save Satyam/1498249 to your computer and use it in GitHub Desktop.
Patch for WidgetButtons
YUI.add('widget-buttons-patch', function (Y) {
"use strict";
var WB = Y.WidgetButtons,
WBp = WB.prototype,
BUTTON = 'button',
getCN = Y.ClassNameManager.getClassName,
DISABLED = getCN(BUTTON, 'disabled'),
DEFAULT = getCN(BUTTON, 'default');
// propertySetters handle all properties for a button
// except for type, section and action.
// From the original it handles classNames.
// I added disabled and isDefault
WB.propertySetters = {
classNames: function (node, value) {
Y.Array.each(Y.Array(value), node.addClass, node);
},
disabled: function(node, value) {
if (value) {
node.addClass(DISABLED);
} else {
node.removeClass(DISABLED);
}
// Since they are not really button elements, the disable attribute makes little sense
// node.set('disabled',!!value);
},
isDefault: function(node, value, button) {
node.addClass(DEFAULT);
if (value) {
this._defaultButton = {node:node,button:button};
}
}
};
WBp._createButtons = function () {
var header = Y.WidgetStdMod.HEADER,
footer = Y.WidgetStdMod.FOOTER,
templates = WB.TEMPLATES,
defaultButtons = WB.DEFAULT_BUTTONS,
hdBtnNode = this._hdBtnNode,
ftBtnNode = this._ftBtnNode,
buttonsArray = this._buttonsArray,
propSetters = WB.propertySetters;
Y.each(this.get('buttons'), function (button) {
var template, node;
// Make sure we actually have some Object.
if ( ! Y.Lang.isObject(button)) {return;}
// Check to see if the type property is defined,
// and if a button corresponds to that type.
if (button.type && defaultButtons[button.type]) {
// Mix the template definitions over the given ones, without overwriting them
Y.mix(button, defaultButtons[button.type]);
}
button.href = button.href || '#';
// Allow any properties to be substituted into the template,
// after all, if they are not in the template, they don't hurt
template = Y.Lang.sub(templates.defaultTemplate, button);
// Create Y.Node instance of button.
node = Y.Node.create(template);
// Allow propertySetters to handle any properties
// including classNames which is now handled by a property setter.
Y.each(button, function (value, name) {
if (propSetters[name]) {
propSetters[name].call(this, node, value, button);
}
},this);
// Push the Node onto the Array of all the buttons.
buttonsArray.push({node: node, cb: button.action});
// Append button to wrapper Node.
switch (button.section) {
case header:
hdBtnNode.appendChild(node);
break;
case footer:
ftBtnNode.appendChild(node);
break;
default:
Y.log('One of the buttons did not have the specified sections property, and was not attached to the appropriate section.', 'warn', 'WidgetButtons');
}
},this);
return true;
};
WBp._afterEnterKey = function (ev) {
var db = this._defaultButton;
if (
db &&
!db.node.hasClass(DISABLED) &&
ev.target.get('tagName').toLowerCase() !== 'textarea'
) {
db.button.action.call(this);
}
};
WBp._afterButtonClick = function(ev, cb){
// so it does not respond to clicks on disabled buttons
if (!ev.currentTarget.hasClass(DISABLED)) {
cb.call(this);
}
};
WBp._bindUIButtons = function () {
var handles = [];
Y.each(this._buttonsArray, function(btn) {
handles.push(btn.node.after('click', this._afterButtonClick , this, btn.cb));
},this);
handles.push(this.get('boundingBox').after('key', this._afterEnterKey, 'enter',this));
this._uiHandlesButtons = handles;
this.after('buttonsChange', this._afterButtonsChange);
};
// Y.mix(Y.Panel, WB, true, undefined, 2);
Y.Base.mix(Y.Panel, [WB])
}, '3.5.0pr1' ,{
requires:['panel','base-build']
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment