Merge branch 'MDL-30341' of git://github.com/scriby/moodle
[moodle.git] / theme / anomaly / javascript / navigation.js
1 /**
2  * Customises the dock for the anomaly theme and does some other cool stuff
3  */
4 function customise_dock_for_theme() {
5     // If we don't have M.core_dock or Y then bail
6     if (!M.core_dock) {
7         return false;
8     }
9     // Change the defautl remove all icon to work with our black dock
10     M.core_dock.cfg.display.removeallicon = M.util.image_url('dock_removeall', 'theme');
12     // On draw completed add the ability to move the dock to from the left to the right
13     M.core_dock.on('dock:drawcompleted', anomaly.dock.enable_side_switching, anomaly.dock);
14     // When the dock is first drawn check to see if it should be moved
15     M.core_dock.on('dock:drawstarted', anomaly.dock.check_initial_position, anomaly.dock);
16     // Corrects the panel x position for the theme
17     M.core_dock.on('dock:itemadded', function(item) {
18         item.on('dockeditem:showstart', anomaly.dock.correct_panel_x_position, anomaly.dock, item);
19         item.on('dockeditem:resizecomplete', anomaly.dock.correct_panel_x_position, anomaly.dock, item);
20     });
22     // Override the default fix_title_orientation method with our anomaly method
23     // this will use SVG and rotate the text if possible.
24     M.core_dock.genericblock.prototype.fix_title_orientation = anomaly.dock.fix_title_orientation;
25     M.core_dock.genericblock.prototype.resize_block_space = anomaly.dock.resize_block_space;
26     return true;
27 }
29 var anomaly = (function(){
30     return {
31         namespaces : {
32             svg : 'http://www.w3.org/2000/svg'
33         },
34         dock : {
35             enable_side_switching : function() {
36                 var movedock = M.core_dock.Y.Node.create('<img src="'+M.util.image_url('movedock', 'theme')+'" />');
37                 var c = M.core_dock.node.one('.controls');
38                 c.insertBefore(M.core_dock.Y.Node.create('<br />'), c.one('img'));
39                 c.insertBefore(movedock, c.one('br'));
40                 movedock.on('click', this.switch_dock_side);
41             },
42             correct_panel_x_position : function(item) {
43                 var dockoffset = M.core_dock.Y.one('#dock_item_'+item.id+'_title').get('offsetWidth');
44                 var panelwidth = M.core_dock.Y.one(item.panel.body).get('offsetWidth');
45                 var screenwidth = parseInt(M.core_dock.Y.get(document.body).get('winWidth'));
46                 switch (M.core_dock.cfg.position) {
47                     case 'left':
48                         item.panel.cfg.setProperty('x', dockoffset);
49                         break;
50                     case 'right':
51                         item.panel.cfg.setProperty('x', (screenwidth-panelwidth-dockoffset-5));
52                         break;
53                 }
54             },
55             switch_dock_side : function () {
56                 var oldorientation = M.core_dock.cfg.orientation;
57                 var oldclass = M.core_dock.cfg.css.dock+'_'+M.core_dock.cfg.position+'_'+oldorientation;
58                 switch (M.core_dock.cfg.position) {
59                     case 'right':
60                         M.core_dock.cfg.position = 'left';
61                         M.core_dock.cfg.orientation = 'vertical';
62                         break;
63                     case 'left':
64                         M.core_dock.cfg.position = 'right';
65                         M.core_dock.cfg.orientation = 'vertical';
66                         break;
67                 }
68                 var newclass = M.core_dock.cfg.css.dock+'_'+M.core_dock.cfg.position+'_'+M.core_dock.cfg.orientation;
69                 M.core_dock.node.replaceClass(oldclass, newclass);
70                 M.core_dock.Y.Cookie.set('dock_position', M.core_dock.cfg.position);
71             },
72             check_initial_position : function () {
73                 var cookieposition = M.core_dock.Y.Cookie.get('dock_position');
74                 if (cookieposition && cookieposition != 'null' && cookieposition !== M.core_dock.cfg.position) {
75                     var oldclass = M.core_dock.cfg.css.dock+'_'+M.core_dock.cfg.position+'_'+M.core_dock.cfg.orientation;
76                     M.core_dock.cfg.position = cookieposition;
77                     if (M.core_dock.node) {
78                         var newclass = M.core_dock.cfg.css.dock+'_'+M.core_dock.cfg.position+'_'+M.core_dock.cfg.orientation;
79                         M.core_dock.node.replaceClass(oldclass, newclass);
80                     }
81                 }
82             },
83             fix_title_orientation : function (node) {
84                 if (M.core_dock.cfg.orientation == 'vertical') {
85                     return anomaly.transform.make_vertical_text(node);
86                 }
87                 return node;
88             },
89             resize_block_space : function (node) {
90                 var blockregions = {
91                     pre: {hasblocks:true,c:'side-pre-only'},
92                     post: {hasblocks:true,c:'side-post-only'},
93                     noblocksc:'noblocks'
94                 };
95                 M.core_dock.Y.all('div.block-region').each(function(blockregion){
96                     if (blockregion.hasClass('side-pre') && blockregion.all('.block').size() == 0) {
97                         blockregions.pre.hasblocks = false;
98                     } else if (blockregion.hasClass('side-post') && blockregion.all('.block').size() == 0) {
99                         blockregions.post.hasblocks = false;
100                     }
101                 });
102                 if (blockregions.pre.hasblocks && blockregions.post.hasblocks) {
103                     // No classes required both regions have blocks
104                     M.core_dock.Y.one(document.body).removeClass(blockregions.pre.c).removeClass(blockregions.post.c).removeClass(blockregions.noblocksc);
105                 } else if (blockregions.pre.hasblocks) {
106                     // side-pre-only required: remove any other classes
107                     M.core_dock.Y.one(document.body).addClass(blockregions.pre.c).removeClass(blockregions.post.c).removeClass(blockregions.noblocksc);
108                 } else if (blockregions.post.hasblocks) {
109                     // side-post-only required: remove any other classes
110                     M.core_dock.Y.one(document.body).removeClass(blockregions.pre.c).addClass(blockregions.post.c).removeClass(blockregions.noblocksc);
111                 } else {
112                     // All blocks have been docked: add noblocks remove side-xxx-only's if set
113                     M.core_dock.Y.one(document.body).removeClass(blockregions.pre.c).removeClass(blockregions.post.c).addClass(blockregions.noblocksc);
114                 }
115                 return '200px';
116             }
117         },
118         transform : {
119             make_vertical_text : function(node) {
121                 if (YAHOO.env.ua.ie > 0) {
122                     if (YAHOO.env.ua.ie > 7) {
123                         node.setAttribute('style', 'writing-mode: tb-rl; filter: flipV flipH;');
124                     } else {
125                         node.innerHTML = node.innerHTML.replace(/(.)/g, "$1<br />");
126                     }
127                     return node;
128                 }
130                 var test = M.core_dock.Y.Node.create('<div><span>'+node.firstChild.nodeValue+'</span></div>');
131                 M.core_dock.Y.one(document.body).append(test);
132                 var height = test.one('span').get('offsetWidth');
133                 test.remove();
135                 var txt = document.createElementNS(anomaly.namespaces.svg, 'text');
136                 txt.setAttribute('x', '0');
137                 txt.setAttribute('y', '0');
138                 txt.setAttribute('transform','rotate(90, 5, 5)');
139                 txt.appendChild(document.createTextNode(node.firstChild.nodeValue));
141                 var svg = document.createElementNS(anomaly.namespaces.svg, 'svg');
142                 svg.setAttribute('version', '1.1');
143                 svg.setAttribute('height', height);
144                 svg.setAttribute('width', 30);
145                 svg.appendChild(txt);
147                 var div = document.createElement(node.nodeName);
148                 div.appendChild(svg);
150                 return div;
151             }
152         }
153     }
154 })();