r3350 committed - button: update to markup and classes

r3350 committed - button: update to markup and classes

Revision: 3350
Author: joern.zaefferer
Date: Thu Oct 1 12:56:55 2009
Log: button: update to markup and classes
http://code.google.com/p/jquery-ui/source/detail?r=3350
Added:
/branches/dev/external/jquery.metadata.js
Modified:
/branches/dev/tests/visual/button/default.html
/branches/dev/ui/jquery.ui.button.js
=======================================
--- /dev/null
+++ /branches/dev/external/jquery.metadata.js    Thu Oct 1 12:56:55 2009
@@ -0,0 +1,122 @@
+/*
+ * Metadata - jQuery plugin for parsing metadata from elements
+ *
+ * Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul
McLanahan
+ *
+ * Dual licensed under the MIT and GPL licenses:
+ * http://www.opensource.org/licenses/mit-license.php
+ * http://www.gnu.org/licenses/gpl.html
+ *
+ * Revision: $Id: jquery.metadata.js 4187 2007-12-16 17:15:27Z
joern.zaefferer $
+ *
+ */
+
+/**
+ * Sets the type of metadata to use. Metadata is encoded in JSON, and each
property
+ * in the JSON will become a property of the element itself.
+ *
+ * There are three supported types of metadata storage:
+ *
+ * attr: Inside an attribute. The name parameter indicates *which*
attribute.
+ *
+ * class: Inside the class attribute, wrapped in curly braces: { }
+ *
+ * elem: Inside a child element (e.g. a script tag). The
+ * name parameter indicates *which* element.
+ *
+ * The metadata for an element is loaded the first time the element is
accessed via jQuery.
+ *
+ * As a result, you can define the metadata type, use $(expr) to load the
metadata into the elements
+ * matched by expr, then redefine the metadata type and run another
$(expr) for other elements.
+ *
+ * @name $.metadata.setType
+ *
+ * @example <p id="one" class="some_class {item_id: 1,
item_label: 'Label'}">This is a p
+ * @before $.metadata.setType("class")
+ * @after $("#one").metadata().item_id == 1;
$("#one").metadata().item_label == "Label"
+ * @desc Reads metadata from the class attribute
+ *
+ * @example <p id="one" class="some_class" data="{item_id: 1,
item_label: 'Label'}">This is a p
+ * @before $.metadata.setType("attr", "data")
+ * @after $("#one").metadata().item_id == 1;
$("#one").metadata().item_label == "Label"
+ * @desc Reads metadata from a "data" attribute
+ *
+ * @example <p id="one" class="some_class"><script>{item_id: 1,
item_label: 'Label'}</script>This is a p
+ * @before $.metadata.setType("elem", "script")
+ * @after $("#one").metadata().item_id == 1;
$("#one").metadata().item_label == "Label"
+ * @desc Reads metadata from a nested script element
+ *
+ * @param String type The encoding type
+ * @param String name The name of the attribute to be used to get metadata
(optional)
+ * @cat Plugins/Metadata
+ * @descr Sets the type of encoding to be used when loading metadata for
the first time
+ * @type undefined
+ * @see metadata()
+ */
+
+(function($) {
+
+$.extend({
+    metadata : {
+        defaults : {
+            type: 'class',
+            name: 'metadata',
+            cre: /({.*})/,
+            single: 'metadata'
+        },
+        setType: function( type, name ){
+            this.defaults.type = type;
+            this.defaults.name = name;
+        },
+        get: function( elem, opts ){
+            var settings = $.extend({},this.defaults,opts);
+            // check for empty string in single property
+            if ( !settings.single.length ) settings.single = 'metadata';
+
+            var data = $.data(elem, settings.single);
+            // returned cached data if it already exists
+            if ( data ) return data;
+
+            data = "{}";
+
+            if ( settings.type == "class" ) {
+                var m = settings.cre.exec( elem.className );
+                if ( m )
+                    data = m[1];
+            } else if ( settings.type == "elem" ) {
+                if( !elem.getElementsByTagName )
+                    return undefined;
+                var e = elem.getElementsByTagName(settings.name);
+                if ( e.length )
+                    data = $.trim(e[0].innerHTML);
+            } else if ( elem.getAttribute != undefined ) {
+                var attr = elem.getAttribute( settings.name );
+                if ( attr )
+                    data = attr;
+            }
+
+            if ( data.indexOf( '{' ) <0 )
+            data = "{" + data + "}";
+
+            data = eval("(" + data + ")");
+
+            $.data( elem, settings.single, data );
+            return data;
+        }
+    }
+});
+
+/**
+ * Returns the metadata object for the first member of the jQuery object.
+ *
+ * @name metadata
+ * @descr Returns element's metadata object
+ * @param Object opts An object contianing settings to override the
defaults
+ * @type jQuery
+ * @cat Plugins/Metadata
+ */
+$.fn.metadata = function( opts ){
+    return $.metadata.get( this[0], opts );
+};
+
+})(jQuery);
=======================================
--- /branches/dev/tests/visual/button/default.html    Wed Sep 30 14:25:32 2009
+++ /branches/dev/tests/visual/button/default.html    Thu Oct 1 12:56:55 2009
@@ -5,6 +5,7 @@
    <link rel="stylesheet" href="../visual.css" type="text/css" />
    <link rel="stylesheet" href="../../../themes/base/ui.all.css"
type="text/css">
    <script type="text/javascript" src="../../../jquery-1.3.2.js"></script>
+    <script type="text/javascript"
src="../../../external/jquery.metadata.js"></script>
    <script type="text/javascript"
src="../../../ui/jquery.ui.core.js"></script>
    <script type="text/javascript"
src="../../../ui/jquery.ui.button.js"></script>
    <script type="text/javascript">
@@ -44,9 +45,10 @@
    </div>
    <div>
        With icon
-        <button><span class="ui-icon ui-icon-locked"></span> Button with icon on
the left</button>
-        <button>Button with icon on the right<span class="ui-icon
ui-icon-scissors"></span></button>
-        <button>Button with icon <span class="ui-icon
ui-icon-circle-close"></span> inbetween.</button>
+        <button class="{button:{icons:{primary:'locked'},text:false}}">Button
with icon only</button>
+        <button class="{button:{icons:{primary:'locked'}}}">Button with icon on
the left</button>
+        <button
class="{button:{icons:{primary:'locked',secondary:'scissors'}}}">Button
with two icons</button>
+        <button
class="{button:{icons:{primary:'locked',secondary:'scissors'},text:false}}">Button
with two icons</button>
    </div>
</div>
@@ -62,9 +64,9 @@
<div id="toolbar" class="ui-widget-header ui-corner-all
ui-helper-clearfix">
    <span id="ops1">
-        <button><span class="ui-icon ui-icon-folder-open"></span></button>
-        <button><span class="ui-icon ui-icon-disk"></span></button>
-        <button><span class="ui-icon ui-icon-trash"></span></button>
+        <button
class="{button:{icons:{primary:'folder-open'},text:false}}">Open</button>
+        <button
class="{button:{icons:{primary:'disk'},text:false}}">Save</button>
+        <button
class="{button:{icons:{primary:'trash'},text:false}}">Delete</button>
    </span>
    <span id="format">
        <input type="checkbox" id="check1" /><label for="check1">B</label>
@@ -72,8 +74,8 @@
        <input type="checkbox" id="check3" /><label for="check3">U</label>
    </span>
    <span id="ops2">
-        <button><span class="ui-icon ui-icon-print"></span></button>
-        <button><span class="ui-icon ui-icon-mail-closed"></span></button>
+        <button
class="{button:{icons:{primary:'print'},text:false}}">Print...</button>
+        <button class="{button:{icons:{primary:'mail-closed'},text:false}}">Mail
to...</button>
    </span>
    <span id="mode">
        <input type="radio" id="mode1" name="radio" /><label
for="mode1">Edit</label>
=======================================
--- /branches/dev/ui/jquery.ui.button.js    Wed Sep 30 14:25:32 2009
+++ /branches/dev/ui/jquery.ui.button.js    Thu Oct 1 12:56:55 2009
@@ -17,6 +17,29 @@
$.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
ui-icon-" + icons.primary + "'></span>");
+            }
+            if (icons.secondary) {
+                this.element.append("<span class='ui-button-icon-secondary ui-icon
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");
+        }
        this.element
            .addClass("ui-button ui-widget ui-state-default ui-corner-all")
            .bind("mouseenter.button", function() {
@@ -45,13 +68,18 @@
    },
    destroy: function() {
+        this.element.html(this.element.find(".ui-button-text").html());
        this.element
-            .removeClass("ui-button ui-widget ui-state-default ui-corner-all
ui-state-hover ui-state-focus")
+            .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);
    }
});
+$.ui.button.defaults = {
+    text: true
+}
+
// TODO merge with button-widget
$.widget("ui.toggleButton", {
    _init: function() {
@@ -161,7 +189,7 @@
        if (this.radio) {
            this.radio.radioButton("destroy");
        }
-        this.buttons.button("destroy");
+        this.buttons.button("destroy").removeClass("ui-corner-left
ui-corner-right");
        $.widget.prototype.destroy.call(this);
    }
});