1 /* global BLOCK, LOGNS, DOCKEDITEM */
6 * This file contains the block class used to manage blocks (both docked and not) for the dock.
8 * @module moodle-core-dock
14 * @namespace M.core.dock
20 BLOCK.superclass.constructor.apply(this, arguments);
24 * A content place holder used when the block has been docked.
25 * @property contentplaceholder
29 contentplaceholder: null,
31 * The skip link associated with this block.
32 * @property contentskipanchor
36 contentskipanchor: null,
38 * The cached content node for the actual block
39 * @property cachedcontentnode
43 cachedcontentnode: null,
45 * If true the user preference isn't updated
46 * @property skipsetposition
50 skipsetposition: true,
52 * The dock item associated with this block
59 * Called during the initialisation process of the object.
62 initializer: function() {
63 var node = Y.one('#inst' + this.get('id'));
68 Y.log('Initialised block with instance id:' + this.get('id'), 'debug', LOGNS);
70 M.core.dock.ensureMoveToIconExists(node);
72 // Move the block straight to the dock if required
73 if (node.hasClass(CSS.dockonload)) {
74 node.removeClass(CSS.dockonload);
77 this.skipsetposition = false;
81 * Returns the class associated with this block.
82 * @method _getBlockClass
87 _getBlockClass: function(node) {
88 var block = node.getData('block'),
91 if (Y.Lang.isString(block) && block !== '') {
94 classes = node.getAttribute('className').toString();
95 matches = /(^| )block_([^ ]+)/.exec(classes);
103 * This function is responsible for moving a block from the page structure onto the dock.
105 * @param {EventFacade} e
107 moveToDock: function(e) {
112 var dock = M.core.dock.get(),
114 blockcontent = Y.one('#inst' + id).one('.content'),
115 icon = (window.right_to_left()) ? 't/dock_to_block_rtl' : 't/dock_to_block',
116 breakchar = (location.href.match(/\?/)) ? '&' : '?',
126 Y.log('Moving block to the dock:' + this.get('id'), 'debug', LOGNS);
128 this.recordBlockState();
130 blocktitle = this.cachedcontentnode.one('.title h2').cloneNode(true);
132 // Build up the block commands.
133 // These should not actually added to the DOM.
134 blockcommands = this.cachedcontentnode.one('.title .commands');
136 blockcommands = blockcommands.cloneNode(true);
138 blockcommands = Y.Node.create('<div class="commands"></div>');
140 movetoimg = Y.Node.create('<img />').setAttrs({
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')
145 moveto = Y.Node.create('<a class="moveto customcommand requiresjs"></a>').setAttrs({
146 href: Y.config.win.location.href + breakchar + 'dock=' + id
148 moveto.append(movetoimg);
149 blockcommands.append(moveto.append(movetoimg));
151 // Create a new dock item for the block
152 this.dockitem = new DOCKEDITEM({
157 contents: blockcontent,
158 commands: blockcommands,
159 blockclass: this._getBlockClass(Y.one('#inst' + id))
161 // Register an event so that when it is removed we can put it back as a block
162 dock.add(this.dockitem);
164 if (!this.skipsetposition) {
165 // save the users preference
166 M.util.set_user_preference('docked_block_instance_' + id, 1);
169 this.set('isDocked', true);
172 * Records the block state and adds it to the docks holding area.
173 * @method recordBlockState
175 recordBlockState: function() {
176 var id = this.get('id'),
177 dock = M.core.dock.get(),
178 node = Y.one('#inst' + id),
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();
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);
193 * This function removes a block from the dock and puts it back into the page structure.
194 * @method returnToPage
197 returnToPage: function() {
198 var id = this.get('id');
200 Y.log('Moving block out of the dock:' + this.get('id'), 'debug', LOGNS);
202 // Enable the skip anchor when going back to block mode
203 if (this.contentskipanchor) {
204 this.contentskipanchor.show();
207 if (this.cachedcontentnode.one('.header')) {
208 this.cachedcontentnode.one('.header').insert(this.dockitem.get('contents'), 'after');
210 this.cachedcontentnode.insert(this.dockitem.get('contents'));
213 this.contentplaceholder.replace(this.cachedcontentnode);
214 this.cachedcontentnode = null;
216 M.util.set_user_preference('docked_block_instance_' + id, 0);
217 this.set('isDocked', false);
221 Y.extend(BLOCK, Y.Base, BLOCK.prototype, {
222 NAME: 'moodle-core-dock-block',
225 * The block instance ID
231 writeOnce: 'initOnly',
232 setter: function(value) {
233 return parseInt(value, 10);
237 * True if the block has been docked.
238 * @attribute isDocked