mirrored from git://git.moodle.org/moodle.git
/
dockeditem.js
324 lines (315 loc) · 9.67 KB
/
dockeditem.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
/* global LOGNS, DOCKEDITEM */
/**
* Dock JS.
*
* This file contains the docked item class.
*
* @module moodle-core-dock
*/
/**
* Docked item.
*
* @namespace M.core.dock
* @class DockedItem
* @constructor
* @extends Base
* @uses EventTarget
*/
DOCKEDITEM = function() {
DOCKEDITEM.superclass.constructor.apply(this, arguments);
};
DOCKEDITEM.prototype = {
/**
* Set to true if this item is currently being displayed.
* @property active
* @protected
* @type Boolean
*/
active: false,
/**
* Called during the initialisation process of the object.
* @method initializer
*/
initializer: function() {
var title = this.get('title'),
titlestring,
type;
/**
* Fired before the docked item has been drawn.
* @event dockeditem:drawstart
*/
this.publish('dockeditem:drawstart', {prefix: 'dockeditem'});
/**
* Fired after the docked item has been drawn.
* @event dockeditem:drawcomplete
*/
this.publish('dockeditem:drawcomplete', {prefix: 'dockeditem'});
/**
* Fired before the docked item is to be shown.
* @event dockeditem:showstart
*/
this.publish('dockeditem:showstart', {prefix: 'dockeditem'});
/**
* Fired after the docked item has been shown.
* @event dockeditem:showcomplete
*/
this.publish('dockeditem:showcomplete', {prefix: 'dockeditem'});
/**
* Fired before the docked item has been hidden.
* @event dockeditem:hidestart
*/
this.publish('dockeditem:hidestart', {prefix: 'dockeditem'});
/**
* Fired after the docked item has been hidden.
* @event dockeditem:hidecomplete
*/
this.publish('dockeditem:hidecomplete', {prefix: 'dockeditem'});
/**
* Fired when the docked item is removed from the dock.
* @event dockeditem:itemremoved
*/
this.publish('dockeditem:itemremoved', {prefix: 'dockeditem'});
if (title) {
type = title.get('nodeName');
titlestring = title.cloneNode(true);
title = Y.Node.create('<' + type + '></' + type + '>');
title = M.core.dock.fixTitleOrientation(title, titlestring.get('text'));
this.set('title', title);
this.set('titlestring', titlestring);
}
Y.log('Initialised dockeditem for block with title "' + this._getLogDescription(), 'debug', LOGNS);
},
/**
* This function draws the item on the dock.
* @method draw
* @return Boolean
*/
draw: function() {
var create = Y.Node.create,
dock = this.get('dock'),
count = dock.count,
docktitle,
dockitem,
closeicon,
closeiconimg,
id = this.get('id');
this.fire('dockeditem:drawstart');
docktitle = create('<div id="dock_item_' + id + '_title" role="menu" aria-haspopup="true" class="'
+ CSS.dockedtitle + '"></div>');
docktitle.append(this.get('title'));
dockitem = create('<div id="dock_item_' + id + '" class="' + CSS.dockeditem + '" tabindex="0" rel="' + id + '"></div>');
if (count === 1) {
dockitem.addClass('firstdockitem');
}
dockitem.append(docktitle);
dock.append(dockitem);
closeiconimg = create('<img alt="' + M.util.get_string('hidepanel', 'block') +
'" title="' + M.util.get_string('hidedockpanel', 'block') + '" />');
closeiconimg.setAttribute('src', M.util.image_url('t/dockclose', 'moodle'));
closeicon = create('<span class="hidepanelicon" tabindex="0"></span>').append(closeiconimg);
closeicon.on('forceclose|click', this.hide, this);
closeicon.on('dock:actionkey', this.hide, this, {actions: {enter: true, toggle: true}});
this.get('commands').append(closeicon);
this.set('dockTitleNode', docktitle);
this.set('dockItemNode', dockitem);
this.fire('dockeditem:drawcomplete');
return true;
},
/**
* This function toggles makes the item active and shows it.
* @method show
* @return Boolean
*/
show: function() {
var dock = this.get('dock'),
panel = dock.getPanel(),
docktitle = this.get('dockTitleNode');
dock.hideActive();
this.fire('dockeditem:showstart');
Y.log('Showing ' + this._getLogDescription(), 'debug', LOGNS);
panel.setHeader(this.get('titlestring'), this.get('commands'));
panel.setBody(Y.Node.create('<div class="block_' + this.get('blockclass') + ' block_docked"></div>')
.append(this.get('contents')));
if (M.core.actionmenu !== undefined) {
M.core.actionmenu.newDOMNode(panel.get('node'));
}
panel.show();
panel.correctWidth();
this.active = true;
// Add active item class first up
docktitle.addClass(CSS.activeitem);
// Set aria-exapanded property to true.
docktitle.set('aria-expanded', "true");
this.fire('dockeditem:showcomplete');
dock.resize();
return true;
},
/**
* This function hides the item and makes it inactive.
* @method hide
*/
hide: function() {
this.fire('dockeditem:hidestart');
Y.log('Hiding "' + this._getLogDescription(), 'debug', LOGNS);
if (this.active) {
// No longer active
this.active = false;
// Hide the panel
this.get('dock').getPanel().hide();
}
// Remove the active class
// Set aria-exapanded property to false
this.get('dockTitleNode').removeClass(CSS.activeitem).set('aria-expanded', "false");
this.fire('dockeditem:hidecomplete');
},
/**
* A toggle between calling show and hide functions based on css.activeitem
* Applies rules to key press events (dock:actionkey)
* @method toggle
* @param {String} action
*/
toggle: function(action) {
var docktitle = this.get('dockTitleNode');
if (docktitle.hasClass(CSS.activeitem) && action !== 'expand') {
this.hide();
} else if (!docktitle.hasClass(CSS.activeitem) && action !== 'collapse') {
this.show();
}
},
/**
* This function removes the node and destroys it's bits.
* @method remove.
*/
remove: function() {
this.hide();
// Return the block to its original position.
this.get('block').returnToPage();
// Remove the dock item node.
this.get('dockItemNode').remove();
this.fire('dockeditem:itemremoved');
},
/**
* Returns the description of this item to use for log calls.
* @method _getLogDescription
* @private
* @return {String}
*/
_getLogDescription: function() {
return this.get('titlestring').get('innerHTML') + ' (' + this.get('blockinstanceid') + ')';
}
};
Y.extend(DOCKEDITEM, Y.Base, DOCKEDITEM.prototype, {
NAME: 'moodle-core-dock-dockeditem',
ATTRS: {
/**
* The block this docked item is associated with.
* @attribute block
* @type BLOCK
* @writeOnce
* @required
*/
block: {
writeOnce: 'initOnly'
},
/**
* The dock itself.
* @attribute dock
* @type DOCK
* @writeOnce
* @required
*/
dock: {
writeOnce: 'initOnly'
},
/**
* The docked item ID. This will be given by the dock.
* @attribute id
* @type Number
*/
id: {},
/**
* Block instance id.Taken from the associated block.
* @attribute blockinstanceid
* @type Number
* @writeOnce
*/
blockinstanceid: {
writeOnce: 'initOnly',
setter: function(value) {
return parseInt(value, 10);
}
},
/**
* The title nodeof the docked item.
* @attribute title
* @type Node
* @default null
*/
title: {
value: null
},
/**
* The title string.
* @attribute titlestring
* @type String
*/
titlestring: {
value: null
},
/**
* The contents of the docked item
* @attribute contents
* @type Node
* @writeOnce
* @required
*/
contents: {
writeOnce: 'initOnly'
},
/**
* Commands associated with the block.
* @attribute commands
* @type Node
* @writeOnce
* @required
*/
commands: {
writeOnce: 'initOnly'
},
/**
* The block class.
* @attribute blockclass
* @type String
* @writeOnce
* @required
*/
blockclass: {
writeOnce: 'initOnly'
},
/**
* The title node for the docked block.
* @attribute dockTitleNode
* @type Node
*/
dockTitleNode: {
value: null
},
/**
* The item node for the docked block.
* @attribute dockItemNode
* @type Node
*/
dockItemNode: {
value: null
},
/**
* The container for the docked item (will contain the block contents when visible)
* @attribute dockcontainerNode
* @type Node
*/
dockcontainerNode: {
value: null
}
}
});
Y.augment(DOCKEDITEM, Y.EventTarget);