[Kimchi-devel] [PATCH] UI Add widget button to new kimchi UI
Aline Manera
alinefm at linux.vnet.ibm.com
Tue Jan 27 12:41:38 UTC 2015
Some comments:
- Add a sample for tests
- Use 4 spaces instead of tab for indentation
- Does this widget will be a button or a radio box or a check box or an
input box?
I am not sure about how much code is common among them but maybe would
be good to split the code in 4 different widgets.
On 21/01/2015 06:27, Wen Wang wrote:
> Signed-off-by: Wen Wang <wenwang at linux.vnet.ibm.com>
> ---
> ui/css/theme-default/button-flat.css | 135 +++++++++++
> ui/js/widgets/button-flat.js | 419 +++++++++++++++++++++++++++++++++++
> 2 files changed, 554 insertions(+)
> create mode 100755 ui/css/theme-default/button-flat.css
> create mode 100755 ui/js/widgets/button-flat.js
>
> diff --git a/ui/css/theme-default/button-flat.css b/ui/css/theme-default/button-flat.css
> new file mode 100755
> index 0000000..082c412
> --- /dev/null
> +++ b/ui/css/theme-default/button-flat.css
> @@ -0,0 +1,135 @@
> +/*
> + * Project Kimchi
> + *
> + * Copyright IBM, Corp. 2015
> + *
> + * Licensed under the Apache License, Version 2.0 (the 'License');
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an 'AS IS' BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +.ui-button {
> + display: inline-block;
> + position: relative;
> + color: #FFFFFF;
> + padding: 0;
> + line-height: normal;
> + margin-right: .1em;
> + cursor: pointer;
> + vertical-align: middle;
> + text-align: center;
> + border-radius: 3px;
> + border: 1px none;
> + background: #4e4d4f;
> + font-weight: normal;
> + overflow: visible; /* removes extra width in IE */
> +}
> +.ui-button:hover{
> + background: #000000;
> + font-weight: normal;
> +}
> +.ui-button:active {
> + background: #4e4d4f;
> +}
> +.ui-button,
> +.ui-button:link,
> +.ui-button:visited,
> +.ui-button:hover,
> +.ui-button:active {
> + text-decoration: none;
> +}
> +
> +/* to make room for the icon, a width needs to be set here */
> +.ui-button-icon-only {
> + width: 2.2em;
> +}
> +/* button elements seem to need a little more width */
> +button.ui-button-icon-only {
> + width: 2.4em;
> +}
> +.ui-button-icons-only {
> + width: 3.4em;
> +}
> +button.ui-button-icons-only {
> + width: 3.7em;
> +}
> +
> +/* button text element */
> +.ui-button .ui-button-text {
> + display: block;
> + line-height: normal;
> +}
> +.ui-button-text-only .ui-button-text {
> + padding: .4em 1em;
> +}
> +.ui-button-icon-only .ui-button-text,
> +.ui-button-icons-only .ui-button-text {
> + padding: .4em;
> + text-indent: -9999999px;
> +}
> +.ui-button-text-icon-primary .ui-button-text,
> +.ui-button-text-icons .ui-button-text {
> + padding: .4em 1em .4em 2.1em;
> +}
> +.ui-button-text-icon-secondary .ui-button-text,
> +.ui-button-text-icons .ui-button-text {
> + padding: .4em 2.1em .4em 1em;
> +}
> +.ui-button-text-icons .ui-button-text {
> + padding-left: 2.1em;
> + padding-right: 2.1em;
> +}
> +/* no icon support for input elements, provide padding by default */
> +input.ui-button {
> + padding: .4em 1em;
> +}
> +
> +/* button icon element(s) */
> +.ui-button-icon-only .ui-icon,
> +.ui-button-text-icon-primary .ui-icon,
> +.ui-button-text-icon-secondary .ui-icon,
> +.ui-button-text-icons .ui-icon,
> +.ui-button-icons-only .ui-icon {
> + position: absolute;
> + top: 50%;
> + margin-top: -8px;
> +}
> +.ui-button-icon-only .ui-icon {
> + left: 50%;
> + margin-left: -8px;
> +}
> +.ui-button-text-icon-primary .ui-button-icon-primary,
> +.ui-button-text-icons .ui-button-icon-primary,
> +.ui-button-icons-only .ui-button-icon-primary {
> + left: .5em;
> +}
> +.ui-button-text-icon-secondary .ui-button-icon-secondary,
> +.ui-button-text-icons .ui-button-icon-secondary,
> +.ui-button-icons-only .ui-button-icon-secondary {
> + right: .5em;
> +}
> +
> +/* button sets */
> +.ui-buttonset {
> + margin-right: 7px;
> +}
> +.ui-buttonset .ui-button {
> + margin-left: 0;
> + margin-right: -.3em;
> +}
> +
> +/* workarounds */
> +/* reset extra padding in Firefox, see h5bp.com/l */
> +input.ui-button::-moz-focus-inner,
> +button.ui-button::-moz-focus-inner {
> + border: 0;
> + padding: 0;
> +}
> diff --git a/ui/js/widgets/button-flat.js b/ui/js/widgets/button-flat.js
> new file mode 100755
> index 0000000..38f8056
> --- /dev/null
> +++ b/ui/js/widgets/button-flat.js
> @@ -0,0 +1,419 @@
> +/*
> + * Project Kimchi
> + *
> + * Copyright IBM, Corp. 2015
> + *
> + * Licensed under the Apache License, Version 2.0 (the 'License');
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing, software
> + * distributed under the License is distributed on an 'AS IS' BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> + * See the License for the specific language governing permissions and
> + * limitations under the License.
> + */
> +
> +(function( factory ) {
> + if ( typeof define === "function" && define.amd ) {
> +
> + // AMD. Register as an anonymous module.
> + define([
> + "jquery",
> + "./core",
> + "./widget"
> + ], factory );
> + } else {
> +
> + // Browser globals
> + factory( jQuery );
> + }
> +}(function( $ ) {
> +
> +var lastActive,
> + baseClasses = "ui-button ui-widget",
> + typeClasses = "ui-button-icons-only ui-button-icon-only ui-button-text-icons ui-button-text-icon-primary ui-button-text-icon-secondary ui-button-text-only",
> + formResetHandler = function() {
> + var form = $( this );
> + setTimeout(function() {
> + form.find( ":ui-button" ).button( "refresh" );
> + }, 1 );
> + },
> + radioGroup = function( radio ) {
> + var name = radio.name,
> + form = radio.form,
> + radios = $( [] );
> + if ( name ) {
> + name = name.replace( /'/g, "\\'" );
> + if ( form ) {
> + radios = $( form ).find( "[name='" + name + "'][type=radio]" );
> + } else {
> + radios = $( "[name='" + name + "'][type=radio]", radio.ownerDocument )
> + .filter(function() {
> + return !this.form;
> + });
> + }
> + }
> + return radios;
> + };
> +
> +$.widget( "kimchi.buttonFlat", {
> + version: "@VERSION",
> + defaultElement: "<button>",
> + options: {
> + disabled: null,
> + text: true,
> + label: null,
> + icons: {
> + primary: null,
> + secondary: null
> + }
> + },
> + _create: function() {
> + this.element.closest( "form" )
> + .unbind( "reset" + this.eventNamespace )
> + .bind( "reset" + this.eventNamespace, formResetHandler );
> +
> + if ( typeof this.options.disabled !== "boolean" ) {
> + this.options.disabled = !!this.element.prop( "disabled" );
> + } else {
> + this.element.prop( "disabled", this.options.disabled );
> + }
> +
> + this._determineButtonType();
> + this.hasTitle = !!this.buttonElement.attr( "title" );
> +
> + var that = this,
> + options = this.options,
> + toggleButton = this.type === "checkbox" || this.type === "radio",
> + activeClass = !toggleButton ? "ui-state-active" : "";
> +
> + if ( options.label === null ) {
> + options.label = (this.type === "input" ? this.buttonElement.val() : this.buttonElement.html());
> + }
> +
> + this._hoverable( this.buttonElement );
> +
> + this.buttonElement
> + .addClass( baseClasses )
> + .attr( "role", "button" )
> + .bind( "mouseenter" + this.eventNamespace, function() {
> + if ( options.disabled ) {
> + return;
> + }
> + if ( this === lastActive ) {
> + $( this ).addClass( "ui-state-active" );
> + }
> + })
> + .bind( "mouseleave" + this.eventNamespace, function() {
> + if ( options.disabled ) {
> + return;
> + }
> + $( this ).removeClass( activeClass );
> + })
> + .bind( "click" + this.eventNamespace, function( event ) {
> + if ( options.disabled ) {
> + event.preventDefault();
> + event.stopImmediatePropagation();
> + }
> + });
> +
> + // Can't use _focusable() because the element that receives focus
> + // and the element that gets the ui-state-focus class are different
> + this._on({
> + focus: function() {
> + this.buttonElement.addClass( "ui-state-focus" );
> + },
> + blur: function() {
> + this.buttonElement.removeClass( "ui-state-focus" );
> + }
> + });
> +
> + if ( toggleButton ) {
> + this.element.bind( "change" + this.eventNamespace, function() {
> + that.refresh();
> + });
> + }
> +
> + if ( this.type === "checkbox" ) {
> + this.buttonElement.bind( "click" + this.eventNamespace, function() {
> + if ( options.disabled ) {
> + return false;
> + }
> + });
> + } else if ( this.type === "radio" ) {
> + this.buttonElement.bind( "click" + this.eventNamespace, function() {
> + if ( options.disabled ) {
> + return false;
> + }
> + $( this ).addClass( "ui-state-active" );
> + that.buttonElement.attr( "aria-pressed", "true" );
> +
> + var radio = that.element[ 0 ];
> + radioGroup( radio )
> + .not( radio )
> + .map(function() {
> + return $( this ).button( "widget" )[ 0 ];
> + })
> + .removeClass( "ui-state-active" )
> + .attr( "aria-pressed", "false" );
> + });
> + } else {
> + this.buttonElement
> + .bind( "mousedown" + this.eventNamespace, function() {
> + if ( options.disabled ) {
> + return false;
> + }
> + $( this ).addClass( "ui-state-active" );
> + lastActive = this;
> + that.document.one( "mouseup", function() {
> + lastActive = null;
> + });
> + })
> + .bind( "mouseup" + this.eventNamespace, function() {
> + if ( options.disabled ) {
> + return false;
> + }
> + $( this ).removeClass( "ui-state-active" );
> + })
> + .bind( "keydown" + this.eventNamespace, function(event) {
> + if ( options.disabled ) {
> + return false;
> + }
> + if ( event.keyCode === $.ui.keyCode.SPACE || event.keyCode === $.ui.keyCode.ENTER ) {
> + $( this ).addClass( "ui-state-active" );
> + }
> + })
> + // see #8559, we bind to blur here in case the button element loses
> + // focus between keydown and keyup, it would be left in an "active" state
> + .bind( "keyup" + this.eventNamespace + " blur" + this.eventNamespace, function() {
> + $( this ).removeClass( "ui-state-active" );
> + });
> +
> + if ( this.buttonElement.is("a") ) {
> + this.buttonElement.keyup(function(event) {
> + if ( event.keyCode === $.ui.keyCode.SPACE ) {
> + // TODO pass through original event correctly (just as 2nd argument doesn't work)
> + $( this ).click();
> + }
> + });
> + }
> + }
> +
> + this._setOption( "disabled", options.disabled );
> + this._resetButton();
> + },
> +
> + _determineButtonType: function() {
> + var ancestor, labelSelector, checked;
> +
> + if ( this.element.is("[type=checkbox]") ) {
> + this.type = "checkbox";
> + } else if ( this.element.is("[type=radio]") ) {
> + this.type = "radio";
> + } else if ( this.element.is("input") ) {
> + this.type = "input";
> + } else {
> + this.type = "button";
> + }
> +
> + if ( this.type === "checkbox" || this.type === "radio" ) {
> + // we don't search against the document in case the element
> + // is disconnected from the DOM
> + ancestor = this.element.parents().last();
> + labelSelector = "label[for='" + this.element.attr("id") + "']";
> + this.buttonElement = ancestor.find( labelSelector );
> + if ( !this.buttonElement.length ) {
> + ancestor = ancestor.length ? ancestor.siblings() : this.element.siblings();
> + this.buttonElement = ancestor.filter( labelSelector );
> + if ( !this.buttonElement.length ) {
> + this.buttonElement = ancestor.find( labelSelector );
> + }
> + }
> + this.element.addClass( "ui-helper-hidden-accessible" );
> +
> + checked = this.element.is( ":checked" );
> + if ( checked ) {
> + this.buttonElement.addClass( "ui-state-active" );
> + }
> + this.buttonElement.prop( "aria-pressed", checked );
> + } else {
> + this.buttonElement = this.element;
> + }
> + },
> +
> + widget: function() {
> + return this.buttonElement;
> + },
> +
> + _destroy: function() {
> + this.element
> + .removeClass( "ui-helper-hidden-accessible" );
> + this.buttonElement
> + .removeClass( baseClasses + " ui-state-active " + typeClasses )
> + .removeAttr( "role" )
> + .removeAttr( "aria-pressed" )
> + .html( this.buttonElement.find(".ui-button-text").html() );
> +
> + if ( !this.hasTitle ) {
> + this.buttonElement.removeAttr( "title" );
> + }
> + },
> +
> + _setOption: function( key, value ) {
> + this._super( key, value );
> + if ( key === "disabled" ) {
> + this.widget().toggleClass( "ui-state-disabled", !!value );
> + this.element.prop( "disabled", !!value );
> + if ( value ) {
> + if ( this.type === "checkbox" || this.type === "radio" ) {
> + this.buttonElement.removeClass( "ui-state-focus" );
> + } else {
> + this.buttonElement.removeClass( "ui-state-focus ui-state-active" );
> + }
> + }
> + return;
> + }
> + this._resetButton();
> + },
> +
> + refresh: function() {
> + //See #8237 & #8828
> + var isDisabled = this.element.is( "input, button" ) ? this.element.is( ":disabled" ) : this.element.hasClass( "ui-button-disabled" );
> +
> + if ( isDisabled !== this.options.disabled ) {
> + this._setOption( "disabled", isDisabled );
> + }
> + if ( this.type === "radio" ) {
> + radioGroup( this.element[0] ).each(function() {
> + if ( $( this ).is( ":checked" ) ) {
> + $( this ).button( "widget" )
> + .addClass( "ui-state-active" )
> + .attr( "aria-pressed", "true" );
> + } else {
> + $( this ).button( "widget" )
> + .removeClass( "ui-state-active" )
> + .attr( "aria-pressed", "false" );
> + }
> + });
> + } else if ( this.type === "checkbox" ) {
> + if ( this.element.is( ":checked" ) ) {
> + this.buttonElement
> + .addClass( "ui-state-active" )
> + .attr( "aria-pressed", "true" );
> + } else {
> + this.buttonElement
> + .removeClass( "ui-state-active" )
> + .attr( "aria-pressed", "false" );
> + }
> + }
> + },
> +
> + _resetButton: function() {
> + if ( this.type === "input" ) {
> + if ( this.options.label ) {
> + this.element.val( this.options.label );
> + }
> + return;
> + }
> + var buttonElement = this.buttonElement.removeClass( typeClasses ),
> + buttonText = $( "<span></span>", this.document[0] )
> + .addClass( "ui-button-text" )
> + .html( this.options.label )
> + .appendTo( buttonElement.empty() )
> + .text(),
> + icons = this.options.icons,
> + multipleIcons = icons.primary && icons.secondary,
> + buttonClasses = [];
> +
> + if ( icons.primary || icons.secondary ) {
> + if ( this.options.text ) {
> + buttonClasses.push( "ui-button-text-icon" + ( multipleIcons ? "s" : ( icons.primary ? "-primary" : "-secondary" ) ) );
> + }
> +
> + if ( icons.primary ) {
> + buttonElement.prepend( "<span class='ui-button-icon-primary ui-icon " + icons.primary + "'></span>" );
> + }
> +
> + if ( icons.secondary ) {
> + buttonElement.append( "<span class='ui-button-icon-secondary ui-icon " + icons.secondary + "'></span>" );
> + }
> +
> + if ( !this.options.text ) {
> + buttonClasses.push( multipleIcons ? "ui-button-icons-only" : "ui-button-icon-only" );
> +
> + if ( !this.hasTitle ) {
> + buttonElement.attr( "title", $.trim( buttonText ) );
> + }
> + }
> + } else {
> + buttonClasses.push( "ui-button-text-only" );
> + }
> + buttonElement.addClass( buttonClasses.join( " " ) );
> + }
> +});
> +
> +$.widget( "ui.buttonset", {
> + version: "@VERSION",
> + options: {
> + items: "button, input[type=button], input[type=submit], input[type=reset], input[type=checkbox], input[type=radio], a, :data(ui-button)"
> + },
> +
> + _create: function() {
> + this.element.addClass( "ui-buttonset" );
> + },
> +
> + _init: function() {
> + this.refresh();
> + },
> +
> + _setOption: function( key, value ) {
> + if ( key === "disabled" ) {
> + this.buttons.button( "option", key, value );
> + }
> +
> + this._super( key, value );
> + },
> +
> + refresh: function() {
> + var rtl = this.element.css( "direction" ) === "rtl",
> + allButtons = this.element.find( this.options.items ),
> + existingButtons = allButtons.filter( ":ui-button" );
> +
> + // Initialize new buttons
> + allButtons.not( ":ui-button" ).button();
> +
> + // Refresh existing buttons
> + existingButtons.button( "refresh" );
> +
> + this.buttons = allButtons
> + .map(function() {
> + return $( this ).button( "widget" )[ 0 ];
> + })
> + .removeClass( "ui-corner-all ui-corner-left ui-corner-right" )
> + .filter( ":first" )
> + .addClass( rtl ? "ui-corner-right" : "ui-corner-left" )
> + .end()
> + .filter( ":last" )
> + .addClass( rtl ? "ui-corner-left" : "ui-corner-right" )
> + .end()
> + .end();
> + },
> +
> + _destroy: function() {
> + this.element.removeClass( "ui-buttonset" );
> + this.buttons
> + .map(function() {
> + return $( this ).button( "widget" )[ 0 ];
> + })
> + .removeClass( "ui-corner-left ui-corner-right" )
> + .end()
> + .button( "destroy" );
> + }
> +});
> +
> +return $.ui.button;
> +
> +}));
More information about the Kimchi-devel
mailing list