forked from dojo/dojox-oldmirror
/
IconItem.js
395 lines (351 loc) · 11.1 KB
/
IconItem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
define([
"dojo/_base/declare",
"dojo/_base/event",
"dojo/_base/lang",
"dojo/_base/sniff",
"dojo/_base/window",
"dojo/dom-class",
"dojo/dom-construct",
"dojo/dom-geometry",
"dojo/dom-style",
"./_ItemBase",
"./Badge",
"./TransitionEvent",
"./iconUtils",
"./lazyLoadUtils",
"./viewRegistry"
], function(declare, event, lang, has, win, domClass, domConstruct, domGeometry, domStyle, ItemBase, Badge, TransitionEvent, iconUtils, lazyLoadUtils, viewRegistry){
// module:
// dojox/mobile/IconItem
return declare("dojox.mobile.IconItem", ItemBase, {
// summary:
// An icon item widget.
// description:
// IconItem represents an item that has an application component
// and its icon image. You can tap the icon to open the
// corresponding application component. You can also use the icon
// to move to a different view by specifying either of the moveTo,
// href or url parameters.
// lazy: String
// If true, the content of the widget, which includes dojo markup,
// is instantiated lazily. That is, only when the widget is opened
// by the user, the required modules are loaded and the content
// widgets are instantiated.
// This option works both in the sync and async loader mode.
lazy: false,
// requires: String
// Comma-separated required module names to be lazily loaded. This
// property is effective only when lazy=true. All the modules
// specified with data-dojo-type and their depending modules are
// automatically loaded by the IconItem when it is opened.
// However, if you need other extra modules to be loaded, use this parameter.
// This option works both in the sync and async loader mode.
requires: "",
// timeout: String
// Duration of highlight in seconds.
timeout: 10,
// content: String
// An HTML fragment to embed as icon content.
content: "",
// badge: String
// A text to show in a badge (ex. "55").
badge: "",
// badgeClass: String
// A class name of a DOM button for a badge.
badgeClass: "mblDomButtonRedBadge",
// deletable: Boolean
// If true, you can delete this IconItem by clicking on the delete
// icon during edit mode.
// If false, the delete icon is not displayed during edit mode so
// that it cannot be deleted.
deletable: true,
// deleteIcon: String
// A delete icon to display at the top-left corner of the item
// during edit mode. The value can be either a path for an image
// file or a class name of a DOM button.
deleteIcon: "",
// tag: String
// A name of the HTML tag to create as domNode.
tag: "li",
/* internal properties */
// Note these are overrides for similar properties defined in _ItemBase.
paramsToInherit: "transition,icon,deleteIcon,badgeClass,deleteIconTitle,deleteIconRole",
baseClass: "mblIconItem",
_selStartMethod: "touch",
_selEndMethod: "none",
destroy: function(){
if(this.badgeObj){
delete this.badgeObj;
}
this.inherited(arguments);
},
buildRendering: function(){
this.domNode = this.srcNodeRef || domConstruct.create(this.tag);
if(this.srcNodeRef){
// reparent
this._tmpNode = domConstruct.create("div");
for(var i = 0, len = this.srcNodeRef.childNodes.length; i < len; i++){
this._tmpNode.appendChild(this.srcNodeRef.firstChild);
}
}
this.iconDivNode = domConstruct.create("div", {className:"mblIconArea"}, this.domNode);
this.iconParentNode = domConstruct.create("div", {className:"mblIconAreaInner"}, this.iconDivNode);
this.labelNode = domConstruct.create("span", {className:"mblIconAreaTitle"}, this.iconDivNode);
this.inherited(arguments);
},
startup: function(){
if(this._started){ return; }
var p = this.getParent();
require([p.iconItemPaneClass], lang.hitch(this, function(module){
var w = this.paneWidget = new module(p.iconItemPaneProps);
this.containerNode = w.containerNode;
if(this._tmpNode){
// reparent
for(var i = 0, len = this._tmpNode.childNodes.length; i < len; i++){
w.containerNode.appendChild(this._tmpNode.firstChild);
}
this._tmpNode = null;
}
p.paneContainerWidget.addChild(w, this.getIndexInParent());
w.set("label", this.label);
this._clickCloseHandle = this.connect(w.closeIconNode, "onclick", "_closeIconClicked");
this._keydownCloseHandle = this.connect(w.closeIconNode, "onkeydown", "_closeIconClicked"); // for desktop browsers
}));
this.inherited(arguments);
if(!this._isOnLine){
this._isOnLine = true;
this.set("icon", this.icon); // retry applying the attribute
}
if(!this.icon && p.defaultIcon){
this.set("icon", p.defaultIcon);
}
this._dragstartHandle = this.connect(this.domNode, "ondragstart", event.stop);
this._keydownHandle = this.connect(this.domNode, "onkeydown", "_onClick"); // for desktop browsers
},
highlight: function(/*Number?*/timeout){
// summary:
// Shakes the icon 10 seconds.
domClass.add(this.iconDivNode, "mblVibrate");
timeout = (timeout !== undefined) ? timeout : this.timeout;
if(timeout > 0){
var _this = this;
setTimeout(function(){
_this.unhighlight();
}, timeout*1000);
}
},
unhighlight: function(){
// summary:
// Stops shaking the icon.
domClass.remove(this.iconDivNode, "mblVibrate");
},
isOpen: function(e){
// summary:
// Returns true if the icon is open.
return this.paneWidget.isOpen();
},
_onClick: function(e){
// summary:
// Internal handler for click events.
// tags:
// private
if(this.getParent().isEditing || e && e.type === "keydown" && e.keyCode !== 13){ return; }
if(this.onClick(e) === false){ return; } // user's click action
this.defaultClickAction(e);
},
onClick: function(/*Event*/ /*===== e =====*/){
// summary:
// User-defined function to handle clicks.
// tags:
// callback
},
_onNewWindowOpened: function(e){
// Override from _ItemBase
this.set("selected", false);
},
_prepareForTransition: function(e, transOpts){
// Override from _ItemBase
if(transOpts){
setTimeout(lang.hitch(this, function(d){
this.set("selected", false);
}), 1500);
return true;
}else{
if(this.getParent().transition === "below" && this.isOpen()){
this.close();
}else{
this.open(e);
}
return false;
}
},
_closeIconClicked: function(e){
// summary:
// Internal handler for click events.
// tags:
// private
if(e){
if(e.type === "keydown" && e.keyCode !== 13){ return; }
if(this.closeIconClicked(e) === false){ return; } // user's click action
setTimeout(lang.hitch(this, function(d){ this._closeIconClicked(); }), 0);
return;
}
this.close();
},
closeIconClicked: function(/*Event*/ /*===== e =====*/){
// summary:
// User-defined function to handle clicks for the close icon.
// tags:
// callback
},
open: function(e){
// summary:
// Opens the icon content, or makes a transition.
var parent = this.getParent(); // IconContainer
if(this.transition === "below"){
if(parent.single){
parent.closeAll();
}
this._open_1();
}else{
parent._opening = this;
if(parent.single){
this.paneWidget.closeHeaderNode.style.display = "none";
if(!this.isOpen()){
parent.closeAll();
}
parent.appView._heading.set("label", this.label);
}
this.moveTo = parent.id + "_mblApplView";
new TransitionEvent(this.domNode, this.getTransOpts(), e).dispatch();
}
},
_open_1: function(){
// tags:
// private
this.paneWidget.show();
this.unhighlight();
if(this.lazy){
lazyLoadUtils.instantiateLazyWidgets(this.containerNode, this.requires);
this.lazy = false;
}
this.scrollIntoView(this.paneWidget.domNode);
this.onOpen();
},
scrollIntoView: function(/*DomNode*/node){
// summary:
// Scrolls until the given node is in the view.
var s = viewRegistry.getEnclosingScrollable(node);
if(s){ // this node is placed inside scrollable
s.scrollIntoView(node, true);
}else{
win.global.scrollBy(0, domGeometry.position(node, false).y);
}
},
close: function(/*Boolean?*/noAnimation){
// summary:
// Closes the icon content.
if(!this.isOpen()){ return; }
this.set("selected", false);
if(has("webkit") && !noAnimation){
var contentNode = this.paneWidget.domNode;
if(this.getParent().transition == "below"){
domClass.add(contentNode, "mblCloseContent mblShrink");
var nodePos = domGeometry.position(contentNode, true);
var targetPos = domGeometry.position(this.domNode, true);
var origin = (targetPos.x + targetPos.w/2 - nodePos.x) + "px " + (targetPos.y + targetPos.h/2 - nodePos.y) + "px";
domStyle.set(contentNode, { webkitTransformOrigin:origin });
}else{
domClass.add(contentNode, "mblCloseContent mblShrink0");
}
}else{
this.paneWidget.hide();
}
this.onClose();
},
onOpen: function(){
// summary:
// Stub method to allow the application to connect.
},
onClose: function(){
// summary:
// Stub method to allow the application to connect.
},
_setLabelAttr: function(/*String*/text){
// tags:
// private
this.label = text;
var s = this._cv ? this._cv(text) : text;
this.labelNode.innerHTML = s;
if(this.paneWidget){
this.paneWidget.set("label", text);
}
},
_getBadgeAttr: function(){
// tags:
// private
return this.badgeObj ? this.badgeObj.getValue() : null;
},
_setBadgeAttr: function(/*String*/value){
// tags:
// private
if(!this.badgeObj){
this.badgeObj = new Badge({fontSize:14, className:this.badgeClass});
domStyle.set(this.badgeObj.domNode, {
position: "absolute",
top: "-2px",
right: "2px"
});
}
this.badgeObj.setValue(value);
if(value){
this.iconDivNode.appendChild(this.badgeObj.domNode);
}else{
this.iconDivNode.removeChild(this.badgeObj.domNode);
}
},
_setDeleteIconAttr: function(icon){
// tags:
// private
if(!this.getParent()){ return; } // icon may be invalid because inheritParams is not called yet
this._set("deleteIcon", icon);
icon = this.deletable ? icon : "";
this.deleteIconNode = iconUtils.setIcon(icon, this.deleteIconPos, this.deleteIconNode,
this.deleteIconTitle || this.alt, this.iconDivNode);
if(this.deleteIconNode){
domClass.add(this.deleteIconNode, "mblIconItemDeleteIcon");
if(this.deleteIconRole){
this.deleteIconNode.setAttribute("role", this.deleteIconRole);
}
}
},
_setContentAttr: function(/*String|DomNode*/data){
// tags:
// private
var root;
if(!this.paneWidget){
if(!this._tmpNode){
this._tmpNode = domConstruct.create("div");
}
root = this._tmpNode;
}else{
root = this.paneWidget.containerNode;
}
if(typeof data === "object"){
domConstruct.empty(root);
root.appendChild(data);
}else{
root.innerHTML = data;
}
},
_setSelectedAttr: function(/*Boolean*/selected){
// summary:
// Makes this widget in the selected or unselected state.
// tags:
// private
this.inherited(arguments);
domStyle.set(this.iconNode, "opacity",
selected ? this.getParent().pressedIconOpacity : 1);
}
});
});