mypackage_wrt/WRTKit/UI/FormButton.js
changeset 42 20be4dd42b12
equal deleted inserted replaced
41:315255cd1aef 42:20be4dd42b12
       
     1 /*
       
     2 © Copyright 2008 Nokia Corporation. All rights reserved.
       
     3 
       
     4 IMPORTANT:  The Nokia software ("WRTKit and Example Widget files") is supplied to you by Nokia
       
     5 Corporation (“Nokia”) in consideration of your agreement to the following terms. Your use, installation
       
     6 and/or redistribution of the WRTKit and Example Widget files constitutes acceptance of these terms. If
       
     7 you do not agree with these terms, please do not use, install, or redistribute the WRTKit and Example
       
     8 Widget files.
       
     9 
       
    10 In consideration of your agreement to abide by the following terms, and subject to these terms, Nokia
       
    11 grants you a personal, non-exclusive license, under Nokia’s copyrights in the WRTKit and Example
       
    12 Widget files, to use, reproduce, and redistribute the WRTKit and Example files, in text form (for HTML,
       
    13 CSS, or JavaScript files) or binary form (for associated images), for the sole purpose of creating S60
       
    14 Widgets.
       
    15 
       
    16 If you redistribute the WRTKit and Example files, you must retain this entire notice in all such
       
    17 redistributions of the WRTKit and Example files.
       
    18 
       
    19 You may not use the name, trademarks, service marks or logos of Nokia to endorse or promote products
       
    20 that include the WRTKit and Example files without the prior written explicit agreement with Nokia.
       
    21 Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by
       
    22 Nokia herein, including but not limited to any patent rights that may be infringed by your products that
       
    23 incorporate the WRTKit and Example files or by other works in which the WRTKit and Example files
       
    24 may be incorporated.
       
    25 
       
    26 The WRTKit and Example files are provided on an "AS IS" basis.  NOKIA MAKES NO
       
    27 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
       
    28 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
       
    29 PARTICULAR PURPOSE, REGARDING THE EXAMPLES OR ITS USE AND OPERATION
       
    30 ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
       
    31 
       
    32 IN NO EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
       
    33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    35 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR
       
    36 DISTRIBUTION OF THE EXAMPLES, HOWEVER CAUSED AND WHETHER UNDER THEORY
       
    37 OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE,
       
    38 EVEN IF NOKIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    39 
       
    40 */
       
    41 
       
    42 ///////////////////////////////////////////////////////////////////////////////
       
    43 // The FormButton class implements a button control for use in form-style UIs.
       
    44 
       
    45 // Constructor.
       
    46 function FormButton(id, text) {
       
    47     if (id != UI_NO_INIT_ID) {
       
    48         this.init(id, text);
       
    49     }
       
    50 }
       
    51 
       
    52 // FormButton inherits from ActionControl.
       
    53 FormButton.prototype = new ActionControl(UI_NO_INIT_ID);
       
    54 
       
    55 // Button table element.
       
    56 FormButton.prototype.tableElement = null;
       
    57 
       
    58 // Button table row element.
       
    59 FormButton.prototype.tableRowElement = null;
       
    60 
       
    61 // Button table left cell element.
       
    62 FormButton.prototype.tableLeftCellElement = null;
       
    63 
       
    64 // Button table center cell element.
       
    65 FormButton.prototype.tableCenterCellElement = null;
       
    66 
       
    67 // Button text element.
       
    68 FormButton.prototype.textElement = null;
       
    69 
       
    70 // Button table right cell element.
       
    71 FormButton.prototype.tableRightCellElement = null;
       
    72 
       
    73 // Initializer - called from constructor.
       
    74 FormButton.prototype.init = function(id, text) {
       
    75     uiLogger.debug("FormButton.init(" + id + ", " + text + ")");
       
    76     
       
    77     // call superclass initializer
       
    78     ActionControl.prototype.init.call(this, id, null);
       
    79     
       
    80     // remove caption element
       
    81     this.assemblyElement.removeChild(this.captionElement);
       
    82     
       
    83     // construct the button
       
    84     this.buttonElement = document.createElement("div");
       
    85     this.tableElement = document.createElement("table");
       
    86     this.tableRowElement = document.createElement("tr");
       
    87     this.tableLeftCellElement = document.createElement("td");
       
    88     this.tableCenterCellElement = document.createElement("td");
       
    89     this.linkElement = document.createElement("a");
       
    90     this.linkElement.href = "JavaScript:void(0)";
       
    91     this.textElement = document.createElement("span");
       
    92     this.tableRightCellElement = document.createElement("td");
       
    93     this.tableElement.appendChild(this.tableRowElement);
       
    94     this.tableRowElement.appendChild(this.tableLeftCellElement);
       
    95     this.tableRowElement.appendChild(this.tableCenterCellElement);
       
    96     this.tableCenterCellElement.appendChild(this.linkElement);
       
    97     this.linkElement.appendChild(this.textElement);
       
    98     this.tableRowElement.appendChild(this.tableRightCellElement);
       
    99     this.buttonElement.appendChild(this.tableElement);
       
   100     this.controlElement.appendChild(this.buttonElement);
       
   101     
       
   102     // set the text
       
   103     this.setText(text);
       
   104     
       
   105     // bind event listeners
       
   106     this.bindActionControlListeners();
       
   107     
       
   108     // update the style
       
   109     this.updateStyleFromState();
       
   110 }
       
   111 
       
   112 // Sets the enabled state.
       
   113 FormButton.prototype.setEnabled = function(enabled) {
       
   114     uiLogger.debug("FormButton.setEnabled(" + enabled + ")");
       
   115     
       
   116     // bail out early if there is no change in state
       
   117     if (this.enabled == enabled) {
       
   118         return;
       
   119     }
       
   120     
       
   121     // set the enabled state
       
   122     this.enabled = enabled;
       
   123     
       
   124     if (this.enabled) {
       
   125         // diabled -> enabled
       
   126         this.tableCenterCellElement.removeChild(this.textElement);
       
   127         this.tableCenterCellElement.appendChild(this.linkElement);
       
   128         this.linkElement.appendChild(this.textElement);
       
   129     } else {
       
   130         // enabled -> diabled
       
   131         this.linkElement.removeChild(this.textElement);
       
   132         this.tableCenterCellElement.removeChild(this.linkElement);
       
   133         this.tableCenterCellElement.appendChild(this.textElement);
       
   134     }
       
   135     
       
   136     // update the style
       
   137     this.updateStyleFromState();
       
   138 }
       
   139 
       
   140 // Returns the button text.
       
   141 FormButton.prototype.getText = function() {
       
   142     return this.textElement.innerHTML;
       
   143 }
       
   144 
       
   145 // Sets the button text.
       
   146 FormButton.prototype.setText = function(text) {
       
   147     uiLogger.debug("FormButton.setText(" + text + ")");
       
   148     this.textElement.innerHTML = (text == null) ? "" : text;;
       
   149 }
       
   150 
       
   151 // Updates the style of the control to reflects the state of the control.
       
   152 FormButton.prototype.updateStyleFromState = function() {
       
   153     uiLogger.debug("FormButton.updateStyleFromState()");
       
   154     
       
   155     // determine the state name
       
   156     var stateName = this.getStyleStateName();
       
   157     
       
   158     // set root element class name
       
   159     this.setClassName(this.rootElement, "Control");
       
   160     
       
   161     // set the control assembly class names
       
   162     this.setClassName(this.assemblyElement, "ControlAssembly ControlAssemblyNormal");
       
   163     
       
   164     // control element
       
   165     this.setClassName(this.controlElement, "ControlElement FormButtonControlElement");
       
   166     
       
   167     // set the button table class names
       
   168     this.setClassName(this.buttonElement, "FormButton");
       
   169     this.setClassName(this.tableElement, "FormButtonTable");
       
   170     this.setClassName(this.tableRowElement, "FormButtonRow");
       
   171     this.setClassName(this.tableLeftCellElement, "FormButtonLeftCell FormButtonLeftCell" + stateName);
       
   172     this.setClassName(this.tableCenterCellElement, "FormButtonCenterCell FormButtonLeftCell" + stateName);
       
   173     this.setClassName(this.tableRightCellElement, "FormButtonRightCell FormButtonLeftCell" + stateName);
       
   174     
       
   175     // set the button text class name
       
   176     this.setClassName(this.textElement, "FormButtonText FormButtonText" + stateName);
       
   177 }