MDL-64506 templates: Move BS2 labels to BS4 badges
[moodle.git] / lib / yui / src / dock / js / block.js
CommitLineData
ad3f8cd1
DP
1/* global BLOCK, LOGNS, DOCKEDITEM */
2
84192d78
SH
3/**
4 * Dock JS.
5 *
6 * This file contains the block class used to manage blocks (both docked and not) for the dock.
7 *
8 * @module moodle-core-dock
9 */
10
11/**
12 * Block.
13 *
14 * @namespace M.core.dock
15 * @class Block
16 * @constructor
1f777e5c 17 * @extends Base
84192d78 18 */
a69a7e89 19BLOCK = function() {
84192d78
SH
20 BLOCK.superclass.constructor.apply(this, arguments);
21};
22BLOCK.prototype = {
23 /**
24 * A content place holder used when the block has been docked.
25 * @property contentplaceholder
26 * @protected
27 * @type Node
28 */
5bb4f444 29 contentplaceholder: null,
84192d78
SH
30 /**
31 * The skip link associated with this block.
32 * @property contentskipanchor
33 * @protected
34 * @type Node
35 */
5bb4f444 36 contentskipanchor: null,
84192d78
SH
37 /**
38 * The cached content node for the actual block
39 * @property cachedcontentnode
40 * @protected
41 * @type Node
42 */
5bb4f444 43 cachedcontentnode: null,
84192d78
SH
44 /**
45 * If true the user preference isn't updated
46 * @property skipsetposition
47 * @protected
48 * @type Boolean
49 */
5bb4f444 50 skipsetposition: true,
84192d78
SH
51 /**
52 * The dock item associated with this block
53 * @property dockitem
54 * @protected
a69a7e89 55 * @type DOCKEDITEM
84192d78 56 */
5bb4f444 57 dockitem: null,
84192d78
SH
58 /**
59 * Called during the initialisation process of the object.
60 * @method initializer
61 */
5bb4f444
DP
62 initializer: function() {
63 var node = Y.one('#inst' + this.get('id'));
84192d78
SH
64 if (!node) {
65 return false;
66 }
67
5bb4f444 68 Y.log('Initialised block with instance id:' + this.get('id'), 'debug', LOGNS);
84192d78
SH
69
70 M.core.dock.ensureMoveToIconExists(node);
71
72 // Move the block straight to the dock if required
73 if (node.hasClass(CSS.dockonload)) {
74 node.removeClass(CSS.dockonload);
e3554c1c 75 this.moveToDock();
84192d78
SH
76 }
77 this.skipsetposition = false;
78 return true;
79 },
80 /**
81 * Returns the class associated with this block.
82 * @method _getBlockClass
83 * @private
84 * @param {Node} node
85 * @return String
86 */
5bb4f444 87 _getBlockClass: function(node) {
a69a7e89
SH
88 var block = node.getData('block'),
89 classes,
90 matches;
91 if (Y.Lang.isString(block) && block !== '') {
92 return block;
93 }
94 classes = node.getAttribute('className').toString();
95 matches = /(^| )block_([^ ]+)/.exec(classes);
84192d78
SH
96 if (matches) {
97 return matches[2];
98 }
99 return matches;
100 },
101
102 /**
a69a7e89 103 * This function is responsible for moving a block from the page structure onto the dock.
84192d78
SH
104 * @method moveToDock
105 * @param {EventFacade} e
106 */
5bb4f444 107 moveToDock: function(e) {
84192d78
SH
108 if (e) {
109 e.halt(true);
110 }
111
112 var dock = M.core.dock.get(),
113 id = this.get('id'),
5bb4f444 114 blockcontent = Y.one('#inst' + id).one('.content'),
557f44d9 115 icon = (window.right_to_left()) ? 't/dock_to_block_rtl' : 't/dock_to_block',
a69a7e89
SH
116 breakchar = (location.href.match(/\?/)) ? '&' : '?',
117 blocktitle,
118 blockcommands,
119 movetoimg,
120 moveto;
84192d78
SH
121
122 if (!blockcontent) {
123 return;
124 }
125
5bb4f444 126 Y.log('Moving block to the dock:' + this.get('id'), 'debug', LOGNS);
84192d78 127
84192d78
SH
128 this.recordBlockState();
129
130 blocktitle = this.cachedcontentnode.one('.title h2').cloneNode(true);
84192d78 131
e3554c1c
AN
132 // Build up the block commands.
133 // These should not actually added to the DOM.
134 blockcommands = this.cachedcontentnode.one('.title .commands');
135 if (blockcommands) {
136 blockcommands = blockcommands.cloneNode(true);
137 } else {
138 blockcommands = Y.Node.create('<div class="commands"></div>');
139 }
a69a7e89 140 movetoimg = Y.Node.create('<img />').setAttrs({
5bb4f444
DP
141 alt: Y.Escape.html(M.util.get_string('undockitem', 'block')),
142 title: Y.Escape.html(M.util.get_string('undockblock', 'block', blocktitle.get('innerHTML'))),
143 src: M.util.image_url(icon, 'moodle')
a69a7e89
SH
144 });
145 moveto = Y.Node.create('<a class="moveto customcommand requiresjs"></a>').setAttrs({
5bb4f444 146 href: Y.config.win.location.href + breakchar + 'dock=' + id
a69a7e89
SH
147 });
148 moveto.append(movetoimg);
149 blockcommands.append(moveto.append(movetoimg));
84192d78
SH
150
151 // Create a new dock item for the block
152 this.dockitem = new DOCKEDITEM({
5bb4f444
DP
153 block: this,
154 dock: dock,
155 blockinstanceid: id,
156 title: blocktitle,
157 contents: blockcontent,
158 commands: blockcommands,
159 blockclass: this._getBlockClass(Y.one('#inst' + id))
84192d78
SH
160 });
161 // Register an event so that when it is removed we can put it back as a block
162 dock.add(this.dockitem);
163
164 if (!this.skipsetposition) {
165 // save the users preference
5bb4f444 166 M.util.set_user_preference('docked_block_instance_' + id, 1);
84192d78
SH
167 }
168
a69a7e89 169 this.set('isDocked', true);
84192d78
SH
170 },
171 /**
172 * Records the block state and adds it to the docks holding area.
173 * @method recordBlockState
174 */
5bb4f444 175 recordBlockState: function() {
84192d78
SH
176 var id = this.get('id'),
177 dock = M.core.dock.get(),
5bb4f444 178 node = Y.one('#inst' + id),
84192d78
SH
179 skipanchor = node.previous();
180 // Disable the skip anchor when docking
181 if (skipanchor.hasClass('skip-block')) {
182 this.contentskipanchor = skipanchor;
183 this.contentskipanchor.hide();
184 }
185 this.cachedcontentnode = node;
186 this.contentplaceholder = Y.Node.create('<div class="block_dock_placeholder"></div>');
187 node.replace(this.contentplaceholder);
188 dock.addToHoldingArea(node);
189 node = null;
84192d78
SH
190 },
191
192 /**
193 * This function removes a block from the dock and puts it back into the page structure.
a69a7e89 194 * @method returnToPage
84192d78
SH
195 * @return {Boolean}
196 */
5bb4f444 197 returnToPage: function() {
e3554c1c 198 var id = this.get('id');
84192d78 199
5bb4f444 200 Y.log('Moving block out of the dock:' + this.get('id'), 'debug', LOGNS);
84192d78
SH
201
202 // Enable the skip anchor when going back to block mode
203 if (this.contentskipanchor) {
204 this.contentskipanchor.show();
205 }
206
207 if (this.cachedcontentnode.one('.header')) {
208 this.cachedcontentnode.one('.header').insert(this.dockitem.get('contents'), 'after');
209 } else {
210 this.cachedcontentnode.insert(this.dockitem.get('contents'));
211 }
212
213 this.contentplaceholder.replace(this.cachedcontentnode);
84192d78 214 this.cachedcontentnode = null;
e3554c1c 215
5bb4f444 216 M.util.set_user_preference('docked_block_instance_' + id, 0);
a69a7e89 217 this.set('isDocked', false);
84192d78
SH
218 return true;
219 }
220};
221Y.extend(BLOCK, Y.Base, BLOCK.prototype, {
5bb4f444
DP
222 NAME: 'moodle-core-dock-block',
223 ATTRS: {
84192d78
SH
224 /**
225 * The block instance ID
226 * @attribute id
227 * @writeOnce
228 * @type Number
229 */
5bb4f444
DP
230 id: {
231 writeOnce: 'initOnly',
232 setter: function(value) {
84192d78
SH
233 return parseInt(value, 10);
234 }
235 },
236 /**
237 * True if the block has been docked.
238 * @attribute isDocked
239 * @default false
240 * @type Boolean
241 */
5bb4f444
DP
242 isDocked: {
243 value: false
84192d78
SH
244 }
245 }
246});