Wikipedia/WRTKit/UI/ImageLabel.js
changeset 20 918767a9c8d3
equal deleted inserted replaced
19:f3521a11d878 20:918767a9c8d3
       
     1 // ////////////////////////////////////////////////////////////////////////////
       
     2 // Symbian Foundation Example Code
       
     3 //
       
     4 // This software is in the public domain. No copyright is claimed, and you 
       
     5 // may use it for any purpose without license from the Symbian Foundation.
       
     6 // No warranty for any purpose is expressed or implied by the authors or
       
     7 // the Symbian Foundation. 
       
     8 // ////////////////////////////////////////////////////////////////////////////
       
     9 
       
    10 ///////////////////////////////////////////////////////////////////////////////
       
    11 // The ImageLabel class implements a control that displays an image
       
    12 
       
    13 // Constructor.
       
    14 function ImageLabel(id, caption, image) {
       
    15     if (id != UI_NO_INIT_ID) {
       
    16         this.init(id, caption, image);
       
    17     }
       
    18 }
       
    19 
       
    20 // Label inherits from Control.
       
    21 ImageLabel.prototype = new Control(UI_NO_INIT_ID);
       
    22 
       
    23 // Content element for the ImageLabel
       
    24 ImageLabel.prototype.contentElement = null;
       
    25 
       
    26 // DOM element for image 
       
    27 ImageLabel.prototype.imageElement = null;
       
    28 
       
    29 // Initializer - called from constructor.
       
    30 ImageLabel.prototype.init = function(id, caption, image) {
       
    31     uiLogger.debug("ImageLabel.init(" + id + ", " + caption + ", " + image + ")");
       
    32     
       
    33     // call superclass initializer
       
    34     Control.prototype.init.call(this, id, caption);
       
    35     
       
    36     // create content element
       
    37     this.contentElement = document.createElement("div");
       
    38     this.controlElement.appendChild(this.contentElement);
       
    39     
       
    40     // set the image
       
    41     this.setImage(image);
       
    42 }
       
    43 
       
    44 // Returns the enabled state for the control.
       
    45 ImageLabel.prototype.isEnabled = function() {
       
    46     return true;
       
    47 }
       
    48 
       
    49 // Returns the focusable state for the control.
       
    50 ImageLabel.prototype.isFocusable = function() {
       
    51     return false;
       
    52 }
       
    53 
       
    54 // Returns the button image (URL); null if none.
       
    55 ImageLabel.prototype.getImage = function() {
       
    56     return (this.imageElement != null) ? this.imageElement.src : null;
       
    57 }
       
    58 
       
    59 // Sets the button image (URL); null if none.
       
    60 ImageLabel.prototype.setImage = function(image) {
       
    61     uiLogger.debug("NavigationButton.setImage(" + image + ")");
       
    62     
       
    63     if (image == null) {
       
    64         // remove image - if any
       
    65         if (this.imageElement != null) {
       
    66             this.contentElement.removeChild(this.imageElement);
       
    67         }
       
    68     } else {
       
    69         // default to not append image element
       
    70         var append = false;
       
    71         
       
    72         // create image element if one doesn't exist
       
    73         if (this.imageElement == null) {
       
    74             this.imageElement = document.createElement("img");
       
    75             this.imageElement.setAttribute("alt", "");
       
    76             append = true;
       
    77         }
       
    78         
       
    79         // set image source URL
       
    80         this.imageElement.src = image;
       
    81         
       
    82         // append the image element to the left cell?
       
    83         if (append) {
       
    84             this.contentElement.appendChild(this.imageElement);
       
    85         }
       
    86     }
       
    87 }
       
    88 
       
    89 
       
    90 // Updates the style of the control to reflects the state of the control.
       
    91 ImageLabel.prototype.updateStyleFromState = function() {
       
    92     uiLogger.debug("Label.updateStyleFromState()");
       
    93     
       
    94     // set element class names
       
    95     this.setClassName(this.rootElement, "Control");
       
    96     this.setClassName(this.assemblyElement, "ControlAssembly ControlAssemblyNormal");
       
    97     this.setClassName(this.captionElement, "ControlCaption ControlCaptionNormal");
       
    98     this.setClassName(this.controlElement, "ControlElement");
       
    99     this.setClassName(this.contentElement, "LabelText");
       
   100 }