MDL-64506 templates: Move BS2 labels to BS4 badges
[moodle.git] / lib / yui / src / dock / js / loader.js
CommitLineData
84192d78
SH
1var LOADERNAME = 'moodle-core-dock-loader';
2
84192d78 3M.core = M.core || {};
84192d78
SH
4M.core.dock = M.core.dock || {};
5
6/**
7 * Creates the move to dock icon for dockable blocks if it doesn't already exist.
8 *
9 * @static
1f777e5c 10 * @method M.core.dock.ensureMoveToIconExists
84192d78
SH
11 * @param {Node} blocknode The Blocks node (.block[data-instanceid])
12 */
13M.core.dock.ensureMoveToIconExists = function(blocknode) {
14 if (blocknode.one('.moveto')) {
15 return true;
16 }
17
18 var commands,
19 moveto = Y.Node.create('<input type="image" class="moveto customcommand requiresjs" />'),
20 blockaction = blocknode.one('.block_action'),
9b22eeff
SH
21 icon = 't/block_to_dock',
22 titleh2 = blocknode.one('.header .title h2');
84192d78 23
83fa6ab6
SH
24 // Must set the image src separately of we get an error with XML strict headers
25 if (Y.one(document.body).hasClass('dir-rtl')) {
84192d78
SH
26 icon = icon + '_rtl';
27 }
28 moveto.setAttribute('alt', M.util.get_string('addtodock', 'block'));
9b22eeff
SH
29 if (titleh2) {
30 moveto.setAttribute('title', Y.Escape.html(M.util.get_string('dockblock', 'block', titleh2.getHTML())));
31 }
84192d78
SH
32 moveto.setAttribute('src', M.util.image_url(icon, 'moodle'));
33
34 if (blockaction) {
35 blockaction.prepend(moveto);
36 } else {
37 commands = blocknode.one('.header .title .commands');
38 if (!commands && blocknode.one('.header .title')) {
39 commands = Y.Node.create('<div class="commands"></div>');
40 blocknode.one('.header .title').append(commands);
41 }
42 commands.append(moveto);
43 }
44 return true;
45};
46
47/**
48 * Dock loader.
49 *
50 * The dock loader is repsponsible for loading and initialising the dock only when required.
51 * By doing this we avoid the need to load unnecessary JavaScript into the page for the dock just incase
52 * it is being used.
53 *
54 * @static
55 * @namespace M.core.dock
56 * @class Loader
57 */
58M.core.dock.loader = M.core.dock.loader || {};
59
60/**
61 * Delegation events
62 * @property delegationEvents
63 * @protected
64 * @type {Array}
65 */
66M.core.dock.loader.delegationEvents = [];
67
68/**
69 * Initialises the dock loader.
70 *
71 * The dock loader works by either firing the dock immediately if there are already docked blocks.
72 * Or if there are not any docked blocks delegating two events and then loading and firing the dock when one of
73 * those delegated events is triggered.
74 *
75 * @method initLoader
76 */
77M.core.dock.loader.initLoader = function() {
82955480 78 Y.log('Dock loader initialising', 'debug', LOADERNAME);
84192d78 79 var dockedblocks = Y.all('.block[data-instanceid][data-dockable]'),
a69a7e89
SH
80 body = Y.one(document.body),
81 callback;
84192d78
SH
82 dockedblocks.each(function() {
83 var id = parseInt(this.getData('instanceid'), 10);
5bb4f444 84 Y.log('Dock loader watching block with instance id: ' + id, 'debug', LOADERNAME);
84192d78
SH
85 M.core.dock.ensureMoveToIconExists(this);
86 });
5bb4f444 87 if (dockedblocks.some(function(node) { return node.hasClass('dock_on_load'); })) {
82955480 88 Y.log('Loading dock module', 'debug', LOADERNAME);
84192d78
SH
89 Y.use('moodle-core-dock', function() {
90 M.core.dock.init();
91 });
92 } else {
a69a7e89 93 callback = function(e) {
84192d78
SH
94 var i,
95 block = this.ancestor('.block[data-instanceid]'),
96 instanceid = block.getData('instanceid');
97 e.halt();
98 for (i in M.core.dock.loader.delegationEvents) {
a69a7e89
SH
99 if (Y.Lang.isNumber(i) || Y.Lang.isString(i)) {
100 M.core.dock.loader.delegationEvents[i].detach();
101 }
84192d78
SH
102 }
103 block.addClass('dock_on_load');
82955480 104 Y.log('Loading dock module', 'debug', LOADERNAME);
5bb4f444
DP
105 Y.use('moodle-core-dock', function() {
106 M.util.set_user_preference('docked_block_instance_' + instanceid, 1);
84192d78
SH
107 M.core.dock.init();
108 });
109 };
110 M.core.dock.loader.delegationEvents.push(body.delegate('click', callback, '.moveto'));
111 M.core.dock.loader.delegationEvents.push(body.delegate('key', callback, '.moveto', 'enter'));
112 }
82955480 113};