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