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