// ========================================================================== // Project: SproutCore - JavaScript Application Framework // Copyright: ©2006-2009 Sprout Systems, Inc. and contributors. // Portions ©2008-2009 Apple Inc. All rights reserved. // License: Licened under MIT license (see license.js) // ========================================================================== sc_require('views/view') ; sc_require('mixins/control') ; SC.ALIGN_LEFT = 'left'; SC.ALIGN_RIGHT = 'right'; SC.ALIGN_CENTER = 'center'; SC.REGULAR_WEIGHT = 'normal'; SC.BOLD_WEIGHT = 'bold'; /** @class Displays a static string of text. You use a label view anytime you need to display a static string of text or to display text that may need to be edited using only an inline control. @extends SC.View @extends SC.Control @extends SC.InlineEditorDelegate @since SproutCore 1.0 */ SC.LabelView = SC.View.extend(SC.Control, /** @scope SC.LabelView.prototype */ { classNames: ['sc-label-view'], /** Specify the font weight for this. You may pass SC.REGULAR_WEIGHT, or SC.BOLD_WEIGHT. */ fontWeight: SC.REGULAR_WEIGHT, /** If true, value will be escaped to avoid scripting attacks. This is a default value that can be overridden by the settings on the owner view. */ escapeHTML: true, escapeHTMLBindingDefault: SC.Binding.oneWay().bool(), /** If true, then the value will be localized. This is a default default that can be overidden by the settings in the owner view. */ localize: false, localizeBindingDefault: SC.Binding.oneWay().bool(), /** Set this to a validator or to a function and the value will be passed through it before being set. This is a default default that can be overidden by the settings in the owner view. */ formatter: null, /** The value of the label. You may also set the value using a content object and a contentValueKey. @field {String} */ value: '', /** The hint to display if no value is set. Should be used only if isEditable is set to YES. */ hint: null, /** The exampleInlineTextFieldView property is by default a SC.InlineTextFieldView but it can be set to a customized inline text field view. @property @type {SC.View} @default {SC.InlineTextFieldView} */ exampleInlineTextFieldView: SC.InlineTextFieldView, /** An optional icon to display to the left of the label. Set this value to either a CSS class name (for spriting) or an image URL. */ icon: null, /** Set the alignment of the label view. */ textAlign: SC.ALIGN_LEFT, /** If you want the inline editor to be multiline set this property to YES. */ isInlineEditorMultiline: NO, /** [RO] The value that will actually be displayed. This property is dynamically computed by applying localization, string conversion and other normalization utilities. @field */ displayValue: function() { var value = this.get('value') ; // 1. apply the formatter var formatter = this.getDelegateProperty('formatter', this.displayDelegate) ; if (formatter) { var formattedValue = (SC.typeOf(formatter) === SC.T_FUNCTION) ? formatter(value, this) : formatter.fieldValueForObject(value, this) ; if (!SC.none(formattedValue)) value = formattedValue ; } // 2. If the returned value is an array, convert items to strings and // join with commas. if (SC.typeOf(value) === SC.T_ARRAY) { var ary = []; for(var idx=0;idx=0) ? icon : SC.BLANK_IMAGE_URL; var className = (url === icon) ? '' : icon ; icon = ''.fmt(url, className) ; context.push(icon); } // if there is a hint set and no value, render the hint // otherwise, render the value if (hint && (!value || value === '')) { context.push('', hint, ''); } else { context.push(value); } // and setup alignment and font-weight on styles context.addStyle('text-align', this.get('textAlign')) .addStyle('font-weight', this.get('fontWeight')); var classes = this._TEMPORARY_CLASS_HASH; classes.icon = !!this.get('icon'); context.setClass(classes); // if we are editing, set the opacity to 0 if (this.get('isEditing')) context.addStyle('opacity', 0.0); } });