|
| 1 | +/** |
| 2 | + * ESUI (Enterprise Simple UI) |
| 3 | + * Copyright 2013 Baidu Inc. All rights reserved. |
| 4 | + * |
| 5 | + * @file 按钮 |
| 6 | + * @author dbear |
| 7 | + */ |
| 8 | + |
| 9 | +define( |
| 10 | + function (require) { |
| 11 | + var lib = require('./lib'); |
| 12 | + var helper = require('./controlHelper'); |
| 13 | + var Control = require('./Control'); |
| 14 | + var ui = require('./main'); |
| 15 | + |
| 16 | + /** button模板 */ |
| 17 | + var tplButton = '' |
| 18 | + + '<div id="${lfIconId}" class="${lfIconClass}"></div>' |
| 19 | + + '<div id="${labelId}" class="${labelClass}">${btnLabel}</div>' |
| 20 | + + '<div id="${rtIconId}" class="${rtIconClass}"></div>'; |
| 21 | + |
| 22 | + /** |
| 23 | + * 默认选项配置 |
| 24 | + * |
| 25 | + * @const |
| 26 | + * @inner |
| 27 | + * @type {Object} |
| 28 | + */ |
| 29 | + var DEFAULT_OPTION = { |
| 30 | + content: '', // 按钮的显示文字 |
| 31 | + disabled: false // 控件是否禁用 |
| 32 | + }; |
| 33 | + |
| 34 | + /** |
| 35 | + * 按钮控件类 |
| 36 | + * |
| 37 | + * @constructor |
| 38 | + * @param {Object} options 初始化参数 |
| 39 | + */ |
| 40 | + function Button(options) { |
| 41 | + Control.apply(this, arguments); |
| 42 | + }; |
| 43 | + |
| 44 | + /** |
| 45 | + * 构建main的主内容 |
| 46 | + * |
| 47 | + * @param {string} type foot | body |
| 48 | + * @inner |
| 49 | + */ |
| 50 | + function getMainHtml(control, type) { |
| 51 | + var me = control; |
| 52 | + var data = { |
| 53 | + lfIconId: helper.getId(me, 'lf-icon'), |
| 54 | + lfIconClass: helper.getClasses(me, 'lf-icon').join(' '), |
| 55 | + labelId: helper.getId(me, 'label'), |
| 56 | + labelClass: helper.getClasses(me, 'label').join(' '), |
| 57 | + btnLabel: me.content || ' ', |
| 58 | + rtIconId: helper.getId(me, 'rt-icon'), |
| 59 | + rtIconClass: helper.getClasses(me, 'rt-icon').join(' ') |
| 60 | + }; |
| 61 | + var innerHtml = lib.format(tplButton, data); |
| 62 | + return innerHtml; |
| 63 | + |
| 64 | + }; |
| 65 | + |
| 66 | + Button.prototype = { |
| 67 | + /** |
| 68 | + * 控件类型 |
| 69 | + * |
| 70 | + * @type {string} |
| 71 | + */ |
| 72 | + type: 'Button', |
| 73 | + |
| 74 | + /** |
| 75 | + * 初始化参数 |
| 76 | + * |
| 77 | + * @param {Object=} options 构造函数传入的参数 |
| 78 | + * @override |
| 79 | + * @protected |
| 80 | + */ |
| 81 | + initOptions: function(options) { |
| 82 | + options = lib.extend(options, DEFAULT_OPTION); |
| 83 | + if (options.main) { |
| 84 | + options.tagName = options.main.nodeName.toLowerCase(); |
| 85 | + if (options.text == null) { |
| 86 | + options.text = lib.getText(options.main); |
| 87 | + } |
| 88 | + if (options.tagName != 'DIV') { |
| 89 | + var tempInnerHtml = options.main.innerHTML; |
| 90 | + options.main = document.createElement('div'); |
| 91 | + options.main.innerHTML = tempInnerHtml; |
| 92 | + options.tagName = 'DIV'; |
| 93 | + } |
| 94 | + var innerDiv = options.main.firstChild; |
| 95 | + if (!options.content |
| 96 | + && innerDiv |
| 97 | + && innerDiv.tagName != 'DIV' |
| 98 | + ) { |
| 99 | + options.content = options.main.innerHTML; |
| 100 | + } |
| 101 | + } |
| 102 | + this.setProperties(options); |
| 103 | + }, |
| 104 | + |
| 105 | + /** |
| 106 | + * 创建控件主元素 |
| 107 | + * |
| 108 | + * @return {HTMLElement} |
| 109 | + * @override |
| 110 | + */ |
| 111 | + createMain: function () { |
| 112 | + this.main = document.createElement('div'); |
| 113 | + }, |
| 114 | + |
| 115 | + /** |
| 116 | + * 重新渲染视图 |
| 117 | + * 仅当生命周期处于RENDER时,该方法才重新渲染 |
| 118 | + * |
| 119 | + * @param {Array=} 变更过的属性的集合 |
| 120 | + * @override |
| 121 | + */ |
| 122 | + repaint: function (changes) { |
| 123 | + var main = this.main; |
| 124 | + main.innerHTML = getMainHtml(this); |
| 125 | + |
| 126 | + // 初始化状态事件 |
| 127 | + main.onclick = lib.bind(this.clickHandler, this); |
| 128 | + helper.initMouseBehavior(this); |
| 129 | + |
| 130 | + }, |
| 131 | + |
| 132 | + /** |
| 133 | + * 鼠标点击事件处理函数 |
| 134 | + */ |
| 135 | + clickHandler: function () { |
| 136 | + this.fire('click'); |
| 137 | + }, |
| 138 | + |
| 139 | + /** |
| 140 | + * 设置内容 |
| 141 | + * |
| 142 | + * @param {string} content 要设置的内容. |
| 143 | + */ |
| 144 | + setContent: function (content) { |
| 145 | + this.content = content; |
| 146 | + if ( this.lifeCycle == Control.LifeCycle.RENDERED) { |
| 147 | + this.repaint({'content': content}); |
| 148 | + } |
| 149 | + }, |
| 150 | + |
| 151 | + /** |
| 152 | + * 销毁释放控件 |
| 153 | + * |
| 154 | + * @override |
| 155 | + */ |
| 156 | + dispose: function () { |
| 157 | + helper.beforeDispose(this); |
| 158 | + |
| 159 | + var main = this.main; |
| 160 | + if (main) { |
| 161 | + main.onclick = null; |
| 162 | + } |
| 163 | + |
| 164 | + helper.dispose(this); |
| 165 | + helper.afterDispose(this); |
| 166 | + } |
| 167 | + }; |
| 168 | + |
| 169 | + lib.inherits(Button, Control); |
| 170 | + ui.register(Button); |
| 171 | + |
| 172 | + return Button; |
| 173 | + } |
| 174 | +); |
0 commit comments