      1 /**
      2  * Hilo
      3  * Copyright 2015 alibaba.com
      4  * Licensed under the MIT License
      5  */
      6
      7 /**
      8  * <iframe src='../../../examples/Button.html?noHeader' width = '320' height = '170' scrolling='no'></iframe>
      9  * <br/>
     10  * demo:
     11  * <pre>
     12  * var btn = new Hilo.Button({
     13  *     image: buttonImage,
     14  *     upState: {rect:[0, 0, 64, 64]},
     15  *     overState: {rect:[64, 0, 64, 64]},
     16  *     downState: {rect:[128, 0, 64, 64]},
     17  *     disabledState: {rect:[192, 0, 64, 64]}
     18  * });
     19  * </pre>
     20  * @class Button class is a simple button class, contains four kinds of state: 'up', 'over', 'down', 'disabled'
     21  * @augments View
     22  * @param {Object} properties create object properties. Contains all writable properties. Also contains:
     23  * <ul>
     24  * <li><b>image</b> - the image element that button image is in</li>
     25  * </ul>
     26  * @module hilo/view/Button
     27  * @requires hilo/core/Hilo
     28  * @requires hilo/core/Class
     29  * @requires hilo/view/View
     30  * @requires hilo/view/Drawable
     31  * @requires hilo/util/util
     32  * @property {Object} upState The property of button 'up' state or collections of its drawable properties.
     33  * @property {Object} overState The property of button 'over' state or collections of its drawable properties.
     34  * @property {Object} downState The property of button 'down' state or collections of its drawable properties.
     35  * @property {Object} disabledState The property of button 'disabled' state or collections of its drawable properties.
     36  * @property {String} state the state name of button, could be one of Button.UP|OVER|DOWN|DISABLED, readonly!
     37  * @property {Boolean} enabled Is button enabled. default value is true, readonly!
     38  * @property {Boolean} useHandCursor If true, cursor over the button will become a pointer cursor, default value is true.
     39  */
     40  var Button = Class.create(/** @lends Button.prototype */{
     41     Extends: View,
     42     constructor: function(properties){
     43         properties = properties || {};
     44         this.id = this.id || properties.id || Hilo.getUid("Button");
     45         Button.superclass.constructor.call(this, properties);
     46
     47         this.drawable = new Drawable(properties);
     48         this.setState(Button.UP);
     49     },
     50
     51     upState: null,
     52     overState: null,
     53     downState: null,
     54     disabledState: null,
     55
     56     state: null,
     57     enabled: true,
     58     useHandCursor: true,
     59
     60     /**
     61      * Set whether the button is enabled.
     62      * @param {Boolean} enabled Show whether the button is enabled.
     63      * @returns {Button} Return the button itself.
     64      */
     65     setEnabled: function(enabled){
     66         if(this.enabled != enabled){
     67             if(!enabled){
     68                 this.setState(Button.DISABLED);
     69             }else{
     70                 this.setState(Button.UP);
     71             }
     72         }
     73         return this;
     74     },
     75
     76     /**
     77      * Set the state of the button. Invoke inside the Button and may not be used.
     78      * @param {String} state New state of the button.
     79      * @returns {Button} Return the button itself.
     80      */
     81     setState: function(state){
     82         if(this.state !== state){
     83             this.state = state;
     84             this.pointerEnabled = this.enabled = state !== Button.DISABLED;
     85
     86             var stateObj;
     87             switch(state){
     88                 case Button.UP:
     89                     stateObj = this.upState;
     90                     break;
     91                 case Button.OVER:
     92                     stateObj = this.overState;
     93                     break;
     94                 case Button.DOWN:
     95                     stateObj = this.downState;
     96                     break;
     97                 case Button.DISABLED:
     98                     stateObj = this.disabledState;
     99                     break;
    100             }
    101
    102             if(stateObj){
    103                 this.drawable.init(stateObj);
    104                 util.copy(this, stateObj, true);
    105             }
    106         }
    107
    108         return this;
    109     },
    110
    111     /**
    112      * overwrite
    113      * @private
    114      */
    115     fire: function(type, detail){
    116         if(!this.enabled) return;
    117
    118         var evtType = typeof type === 'string' ? type : type.type;
    119         switch(evtType){
    120             case 'mousedown':
    121             case 'touchstart':
    122             case 'touchmove':
    123                 this.setState(Button.DOWN);
    124                 break;
    125             case "mouseover":
    126                 this.setState(Button.OVER);
    127                 break;
    128             case 'mouseup':
    129                 if(this.overState) this.setState(Button.OVER);
    130                 else if(this.upState) this.setState(Button.UP);
    131                 break;
    132             case 'touchend':
    133             case 'touchout':
    134             case 'mouseout':
    135                 this.setState(Button.UP);
    136                 break;
    137         }
    138
    139         return Button.superclass.fire.call(this, type, detail);
    140     },
    141
    142     Statics: /** @lends Button */ {
    143         /**
    144          * Statics value of Button's 'up' state.
    145          * @type String
    146          */
    147         UP: 'up',
    148         /**
    149          * Statics value of Button's 'over' state.
    150          * @type String
    151          */
    152         OVER: 'over',
    153         /**
    154          * Statics value of Button's 'down' state.
    155          * @type String
    156          */
    157         DOWN: 'down',
    158         /**
    159          * Statics value of Button's 'disabled' state.
    160          * @type String
    161          */
    162         DISABLED: 'disabled'
    163     }
    164  });