| 1 |
define([ |
| 2 |
"dojo/_base/kernel", |
| 3 |
"dojo/_base/declare", |
| 4 |
"dojo/_base/array", |
| 5 |
"dojo/_base/lang", |
| 6 |
"dojo/_base/event", |
| 7 |
"dojo/query", |
| 8 |
"dojo/dom-attr", |
| 9 |
"dojo/text!./resources/TriStateCheckBox.html", |
| 10 |
"dijit/form/Button", |
| 11 |
"dijit/form/_ToggleButtonMixin", |
| 12 |
"dojo/NodeList-dom" |
| 13 |
], function(kernel, declare, array, lang, event, query, domAttr, template, Button, _ToggleButtonMixin){ |
| 14 |
|
| 15 |
return declare("dojox.form.TriStateCheckBox", [Button, _ToggleButtonMixin], { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
templateString: template, |
| 20 |
|
| 21 |
baseClass: "dojoxTriStateCheckBox", |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
type: "checkbox", |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
states: "", |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
_stateLabels: null, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
stateValue: null, |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
_currentState: 0, |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
_stateType: "False", |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
readOnly: false, |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
checked: "", |
| 63 |
|
| 64 |
|
| 65 |
_aria_attr: "aria-checked", |
| 66 |
|
| 67 |
constructor: function(){ |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
this.states = [false, "mixed", true]; |
| 73 |
this.checked = false; |
| 74 |
this._stateLabels = { |
| 75 |
"False": '□', |
| 76 |
"True": '√', |
| 77 |
"Mixed": '■' |
| 78 |
}; |
| 79 |
this.stateValues = { |
| 80 |
"False": false, |
| 81 |
"True": "on", |
| 82 |
"Mixed": "mixed" |
| 83 |
}; |
| 84 |
}, |
| 85 |
|
| 86 |
_fillContent: function( source){ |
| 87 |
|
| 88 |
|
| 89 |
}, |
| 90 |
|
| 91 |
postCreate: function(){ |
| 92 |
domAttr.set(this.stateLabelNode, 'innerHTML', this._stateLabels[this._stateType]); |
| 93 |
this.inherited(arguments); |
| 94 |
}, |
| 95 |
|
| 96 |
startup: function(){ |
| 97 |
this.set("checked", this.params.checked || this.states[this._currentState]); |
| 98 |
domAttr.set(this.stateLabelNode, 'innerHTML', this._stateLabels[this._stateType]); |
| 99 |
this.inherited(arguments); |
| 100 |
}, |
| 101 |
|
| 102 |
|
| 103 |
_setIconClassAttr: null, |
| 104 |
|
| 105 |
_setCheckedAttr: function( checked, priorityChange){ |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
var stateIndex = array.indexOf(this.states, checked), changed = false; |
| 116 |
if(stateIndex >= 0){ |
| 117 |
this._currentState = stateIndex; |
| 118 |
this._stateType = this._getStateType(checked); |
| 119 |
domAttr.set(this.focusNode, "value", this.stateValues[this._stateType]); |
| 120 |
domAttr.set(this.stateLabelNode, 'innerHTML', this._stateLabels[this._stateType]); |
| 121 |
this.inherited(arguments); |
| 122 |
}else{ |
| 123 |
console.warn("Invalid state!"); |
| 124 |
} |
| 125 |
}, |
| 126 |
|
| 127 |
setChecked: function( checked){ |
| 128 |
|
| 129 |
|
| 130 |
kernel.deprecated("setChecked("+checked+") is deprecated. Use set('checked',"+checked+") instead.", "", "2.0"); |
| 131 |
this.set('checked', checked); |
| 132 |
}, |
| 133 |
|
| 134 |
_setStatesAttr: function( states){ |
| 135 |
if(lang.isArray(states)){ |
| 136 |
this._set("states", states); |
| 137 |
}else if(lang.isString(states)){ |
| 138 |
var map = { |
| 139 |
"true": true, |
| 140 |
"false": false, |
| 141 |
"mixed": "mixed" |
| 142 |
}; |
| 143 |
states = states.split(/\s*,\s*/); |
| 144 |
for(var i = 0; i < states.length; i++){ |
| 145 |
states[i] = map[states[i]] !== undefined ? map[states[i]] : false; |
| 146 |
} |
| 147 |
this._set("states", states); |
| 148 |
} |
| 149 |
}, |
| 150 |
|
| 151 |
_setReadOnlyAttr: function( value){ |
| 152 |
this._set("readOnly", value); |
| 153 |
domAttr.set(this.focusNode, "readOnly", value); |
| 154 |
}, |
| 155 |
|
| 156 |
_setValueAttr: function( newValue, priorityChange){ |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
if(typeof newValue == "string" && (array.indexOf(this.states, newValue) < 0)){ |
| 169 |
if(newValue == ""){ |
| 170 |
newValue = "on"; |
| 171 |
} |
| 172 |
this.stateValues["True"] = newValue; |
| 173 |
newValue = true; |
| 174 |
} |
| 175 |
if(this._created){ |
| 176 |
this._currentState = array.indexOf(this.states, newValue); |
| 177 |
this.set('checked', newValue, priorityChange); |
| 178 |
domAttr.set(this.focusNode, "value", this.stateValues[this._stateType]); |
| 179 |
} |
| 180 |
}, |
| 181 |
|
| 182 |
_setValuesAttr: function( newValues){ |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
this.stateValues["True"] = newValues[0] ? newValues[0] : this.stateValues["True"]; |
| 194 |
this.stateValues["Mixed"] = newValues[1] ? newValues[1] : this.stateValues["Mixed"]; |
| 195 |
}, |
| 196 |
|
| 197 |
_getValueAttr: function(){ |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
return this.stateValues[this._stateType]; |
| 203 |
}, |
| 204 |
|
| 205 |
reset: function(){ |
| 206 |
this._hasBeenBlurred = false; |
| 207 |
this.set("states", this.params.states || [false, "mixed", true]); |
| 208 |
this.stateValues = this.params.stateValues || { |
| 209 |
"False" : false, |
| 210 |
"True" : "on", |
| 211 |
"Mixed" : "mixed" |
| 212 |
}; |
| 213 |
this.set("values", this.params.values || []); |
| 214 |
this.set('checked', this.params.checked || this.states[0]); |
| 215 |
}, |
| 216 |
|
| 217 |
_onFocus: function(){ |
| 218 |
if(this.id){ |
| 219 |
query("label[for='"+this.id+"']").addClass("dijitFocusedLabel"); |
| 220 |
} |
| 221 |
this.inherited(arguments); |
| 222 |
}, |
| 223 |
|
| 224 |
_onBlur: function(){ |
| 225 |
if(this.id){ |
| 226 |
query("label[for='"+this.id+"']").removeClass("dijitFocusedLabel"); |
| 227 |
} |
| 228 |
this.mouseFocus = false; |
| 229 |
this.inherited(arguments); |
| 230 |
}, |
| 231 |
|
| 232 |
_onClick: function( e){ |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
if(this.readOnly || this.disabled){ |
| 237 |
event.stop(e); |
| 238 |
return false; |
| 239 |
} |
| 240 |
this.click(); |
| 241 |
return this.onClick(e); |
| 242 |
}, |
| 243 |
|
| 244 |
click: function(){ |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
if(this._currentState >= this.states.length - 1){ |
| 249 |
this._currentState = 0; |
| 250 |
}else{ |
| 251 |
if(this._currentState == -1){ |
| 252 |
this.fixState(); |
| 253 |
}else{ |
| 254 |
this._currentState++; |
| 255 |
} |
| 256 |
} |
| 257 |
var oldState = this._currentState; |
| 258 |
this.set("checked", this.states[this._currentState]); |
| 259 |
this._currentState = oldState; |
| 260 |
domAttr.set(this.stateLabelNode, 'innerHTML', this._stateLabels[this._stateType]); |
| 261 |
}, |
| 262 |
|
| 263 |
fixState: function(){ |
| 264 |
|
| 265 |
|
| 266 |
this._currentState = this.states.length - 1; |
| 267 |
}, |
| 268 |
|
| 269 |
_getStateType: function( state){ |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
return state ? (state == "mixed" ? "Mixed" : "True") : "False"; |
| 277 |
}, |
| 278 |
|
| 279 |
_onMouseDown: function(){ |
| 280 |
this.mouseFocus = true; |
| 281 |
} |
| 282 |
}); |
| 283 |
|
| 284 |
}); |