1 var LOADERNAME = 'moodle-core-dock-loader';
10 M.core = M.core || {};
19 M.core.dock = M.core.dock || {};
22 * Creates the move to dock icon for dockable blocks if it doesn't already exist.
25 * @method ensureMoveToIconExists
26 * @param {Node} blocknode The Blocks node (.block[data-instanceid])
28 M.core.dock.ensureMoveToIconExists = function(blocknode) {
29 if (blocknode.one('.moveto')) {
34 moveto = Y.Node.create('<input type="image" class="moveto customcommand requiresjs" />'),
35 blockaction = blocknode.one('.block_action'),
36 icon = 't/block_to_dock';
38 // Must set the image src separately of we get an error with XML strict headers
39 if (Y.one(document.body).hasClass('dir-rtl')) {
42 moveto.setAttribute('alt', M.util.get_string('addtodock', 'block'));
43 moveto.setAttribute('title', Y.Escape.html(M.util.get_string('dockblock', 'block', blocknode.one('.header .title h2').getHTML())));
44 moveto.setAttribute('src', M.util.image_url(icon, 'moodle'));
47 blockaction.prepend(moveto);
49 commands = blocknode.one('.header .title .commands');
50 if (!commands && blocknode.one('.header .title')) {
51 commands = Y.Node.create('<div class="commands"></div>');
52 blocknode.one('.header .title').append(commands);
54 commands.append(moveto);
62 * The dock loader is repsponsible for loading and initialising the dock only when required.
63 * By doing this we avoid the need to load unnecessary JavaScript into the page for the dock just incase
67 * @namespace M.core.dock
70 M.core.dock.loader = M.core.dock.loader || {};
74 * @property delegationEvents
78 M.core.dock.loader.delegationEvents = [];
81 * Initialises the dock loader.
83 * The dock loader works by either firing the dock immediately if there are already docked blocks.
84 * Or if there are not any docked blocks delegating two events and then loading and firing the dock when one of
85 * those delegated events is triggered.
89 M.core.dock.loader.initLoader = function() {
90 Y.log('Dock loader initialising', 'note', LOADERNAME);
91 var dockedblocks = Y.all('.block[data-instanceid][data-dockable]'),
92 body = Y.one(document.body),
94 dockedblocks.each(function() {
95 var id = parseInt(this.getData('instanceid'), 10);
96 Y.log('Dock loader watching block with instance id: '+id, 'note', LOADERNAME);
97 M.core.dock.ensureMoveToIconExists(this);
99 if (dockedblocks.some(function(node){return node.hasClass('dock_on_load');})) {
100 Y.log('Loading dock module', 'note', LOADERNAME);
101 Y.use('moodle-core-dock', function() {
105 callback = function(e) {
107 block = this.ancestor('.block[data-instanceid]'),
108 instanceid = block.getData('instanceid');
110 for (i in M.core.dock.loader.delegationEvents) {
111 if (Y.Lang.isNumber(i) || Y.Lang.isString(i)) {
112 M.core.dock.loader.delegationEvents[i].detach();
115 block.addClass('dock_on_load');
116 Y.log('Loading dock module', 'note', LOADERNAME);
117 Y.use('moodle-core-dock', function(){
118 M.util.set_user_preference('docked_block_instance_'+instanceid, 1);
122 M.core.dock.loader.delegationEvents.push(body.delegate('click', callback, '.moveto'));
123 M.core.dock.loader.delegationEvents.push(body.delegate('key', callback, '.moveto', 'enter'));