r3354 committed - Button: Refactored label and icon creation. Added support to change op...
Revision: 3354
Author: scott.gonzalez
Date: Thu Oct 1 17:10:51 2009
Log: Button: Refactored label and icon creation. Added support to change
options after init.
http://code.google.com/p/jquery-ui/source/detail?r=3354
Modified:
/branches/dev/ui/jquery.ui.button.js
=======================================
--- /branches/dev/ui/jquery.ui.button.js Thu Oct 1 13:24:18 2009
+++ /branches/dev/ui/jquery.ui.button.js Thu Oct 1 17:10:51 2009
@@ -17,29 +17,11 @@
$.widget("ui.button", {
_init: function() {
var self = this;
-
$("<span/>").addClass("ui-button-text").html(this.element.html()).appendTo(this.element.empty());
- if (this.options.icons) {
- var icons = this.options.icons;
- if (icons.primary && icons.secondary) {
- this.element.addClass("ui-button-text-icons");
- } else {
- this.element.addClass("ui-button-text-icon");
- }
- if (icons.primary) {
- this.element.prepend("<span class='ui-button-icon-primary ui-icon " +
icons.primary + "'></span>");
- }
- if (icons.secondary) {
- this.element.append("<span class='ui-button-icon-secondary ui-icon " +
icons.secondary + "'></span>");
- }
- if (!this.options.text) {
- this.element.addClass(icons.primary &&
icons.secondary ? "ui-button-icons-only" : "ui-button-icon-only").removeClass("ui-button-text-icons
ui-button-text-icon");
- if (!this.element.attr("tooltip")) {
- this.element.attr("tooltip",
this.element.find(".ui-button-text").text());
- }
- }
- } else {
- this.element.addClass("ui-button-text-only");
- }
+
+ if (this.options.label === null) {
+ this.options.label = this.element.html();
+ }
+
this.element
.addClass("ui-button ui-widget ui-state-default ui-corner-all")
.bind("mouseenter.button", function() {
@@ -65,6 +47,8 @@
if (self.options.disabled) { return; }
$(this).removeClass("ui-state-active");
});
+
+ this._resetButton();
},
destroy: function() {
@@ -73,12 +57,52 @@
.removeClass("ui-button ui-widget ui-state-default ui-corner-all
ui-state-hover ui-state-focus ui-button-icons-only ui-button-icon-only
ui-button-text-icons ui-button-text-icon")
.unbind(".button");
$.widget.prototype.destroy.call(this);
+ },
+
+ _setData: function(key, value) {
+ $.widget.prototype._setData.apply(this, arguments);
+ this._resetButton();
+ },
+
+ _resetButton: function() {
+ $("<span></span>")
+ .addClass("ui-button-text")
+ .html(this.options.label)
+ .appendTo(this.element.empty());
+
+ var icons = this.options.icons,
+ multipleIcons = icons.primary && icons.secondary;
+ if (icons.primary || icons.secondary) {
+ this.element.addClass("ui-button-text-icon" +
+ (multipleIcons ? "s" : ""));
+ if (icons.primary) {
+ this.element.prepend("<span class='ui-button-icon-primary ui-icon " +
icons.primary + "'></span>");
+ }
+ if (icons.secondary) {
+ this.element.append("<span class='ui-button-icon-secondary ui-icon " +
icons.secondary + "'></span>");
+ }
+ if (!this.options.text) {
+ this.element
+
.addClass(multipleIcons ? "ui-button-icons-only" : "ui-button-icon-only")
+ .removeClass("ui-button-text-icons ui-button-text-icon");
+ if (!this.element.attr("tooltip")) {
+ this.element.attr("tooltip",
this.element.find(".ui-button-text").text());
+ }
+ }
+ } else {
+ this.element.addClass("ui-button-text-only");
+ }
}
});
$.ui.button.defaults = {
- text: true
-}
+ text: true,
+ label: null,
+ icons: {
+ primary: null,
+ secondary: null
+ }
+};
// TODO merge with button-widget
$.widget("ui.toggleButton", {