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