corrade-nucleus-nucleons – Blame information for rev 43

Subversion Repositories:
Rev:
Rev Author Line No. Line
31 office 1 // Load the window manager desktop icons.
2 function loadWindowManagerIcons(path) {
3 // Search and load all nucleons.
37 office 4 $.get(path, function(nucleons) {
41 office 5 var ix = 0, iy = 0;
6 var states = {};
7 var cookie = Cookies.get('window-manager-icons');
8 if(cookie)
9 states = JSON.parse(cookie);
10 $.each(nucleons, function(index, file) {
31 office 11 // Skip files starting with full stop (POSIX).
41 office 12 if (/^\./.test(file)) return;
13 $.get(path + '/' + file, function(data) {
14 data = $(data).get(0);
15 var nucleon = data.getAttribute('data-target');
16  
17 if(typeof states[nucleon] !== 'undefined') {
18 var sx = states[nucleon].x,
19 sy = states[nucleon].y;
20  
21 data.style.webkitTransform =
22 data.style.transform =
23 'translate(' + sx + 'px, ' + sy + 'px)';
24  
25 // Update the position attributes
26 data.setAttribute('data-x', sx);
27 data.setAttribute('data-y', sy);
28  
29 $('#window-manager-desktop').append(data);
30 return;
31 }
32  
31 office 33 // Translate the desktop icons so they do not overlap.
41 office 34 data.style.webkitTransform =
35 data.style.transform =
36 'translate(' + ix + 'px, ' + iy + 'px)';
31 office 37  
38 // Update the position attributes
41 office 39 data.setAttribute('data-x', ix);
40 data.setAttribute('data-y', iy);
31 office 41  
41 office 42 $('#window-manager-desktop').append(data);
31 office 43 // Translate by the desktop icon size.
41 office 44 ix += 64;
31 office 45 });
46 });
47 });
48 }
49  
50 // Open a window manager window.
51 function openWindowManagerWindow(nucleon) {
52 // If the nucleon already exists, then do not append the content again.
53 if($('#' + nucleon).length)
54 return;
55  
41 office 56 // Store the icon.
57 var nucleon_icon = $('#' + nucleon + '-window-manager-button-icon').attr('src');
58  
31 office 59 // Change the nucleon icon to a loading icon.
60 $('#' + nucleon + '-window-manager-button-icon').attr('src', '/img/loader.gif');
41 office 61  
31 office 62 // Disable button for successive clicks.
63 $('#' + nucleon + '-window-manager-button').prop('disabled', true);
64  
65 // Load the nucleon.
66 $.get('/' + nucleon + '/index.html', function(data) {
41 office 67 // Get the DOM object.
68 data = $(data).get(0);
69  
33 office 70 // Select all new scripts that have to be inserted into the DOM.
71 var nucleonScripts = [];
72 $(data).find('script').each(function() {
73 var src = $(this).attr('src');
74 if(src === undefined)
75 return;
35 office 76 nucleonScripts[src] = $(this);
33 office 77 });
78 // Remove scripts from the nucleon if they are already added to the DOM.
41 office 79 $(document).find('script').each(function() {
33 office 80 var src = $(this).attr('src');
81 if(nucleonScripts[src] === undefined)
82 return;
35 office 83 nucleonScripts[src].remove();
33 office 84 });
41 office 85  
86 // Restore windows to their saved position.
87 var cookie = Cookies.get('window-manager-windows');
88 if(cookie) {
89 var states = JSON.parse(cookie);
90 if(typeof states[nucleon] !== 'undefined') {
91 var x = states[nucleon].x,
92 y = states[nucleon].y,
93 wm = $(data).find('.window-manager-window').get(0);
43 office 94  
41 office 95 wm.style.webkitTransform =
96 wm.style.transform =
97 'translate(' + x + 'px, ' + y + 'px)';
98  
99 // Update the position attributes
100 wm.setAttribute('data-x', x);
101 wm.setAttribute('data-y', y);
102 }
103 }
31 office 104 $(data).hide().appendTo('#window-manager-desktop').fadeIn(750);
105 }).done(function(data) {
106 // Change the nucleon icon back.
41 office 107 $('#' + nucleon + '-window-manager-button-icon').attr('src', nucleon_icon);
31 office 108 // Enable the button.
109 $('#' + nucleon + '-window-manager-button').prop('disabled', false);
110 });
111 }
112  
113 // Retrieve the top-most window manager window z-index.
25 office 114 function getTopWindowIndex() {
115 return Math.max.apply(null,
29 office 116 $.map($('.window-manager-window'), function(e, n) {
25 office 117 if ($(e).css('position') != 'static')
31 office 118 return parseInt($(e).css('z-index')) || 1;
25 office 119 })
120 );
121 }
122  
123 // Draggable and moveable windows.
124 $(document).ready(() => {
29 office 125 // Window manager windows.
126 interact('.window-manager-window')
25 office 127 .draggable({
128 // enable inertial throwing
129 inertia: true,
130 // keep the element within the area of it's parent
29 office 131 restrict: {
132 restriction: $('#window-manager-desktop').get(0),
133 endOnly: true,
134 elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
135 },
25 office 136  
137 // enable autoScroll
138 autoScroll: true,
139  
140 onmove: dragMoveListener,
141 onend: function (event) {
142 event.target.style.opacity = 1;
143 },
144 onstart: function(event) {
145 // Move windows to top on drag.
146 event.target.style.zIndex = getTopWindowIndex() + 1;
147 event.target.style.opacity = 0.5;
148 }
149 })
29 office 150 .allowFrom('.panel-heading');
30 office 151  
152 // Window manager icons.
153 interact('.window-manager-icon')
154 .draggable({
155 // enable inertial throwing
156 inertia: false,
157 // keep the element within the area of it's parent
158 restrict: {
159 restriction: $('#window-manager-desktop').get(0),
160 endOnly: true,
161 elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
162 },
163 snap: {
164 targets: [
165 interact.createSnapGrid({ x: 64, y: 64 })
166 ],
167 range: Infinity,
168 relativePoints: [ { x: 0, y: 0 } ]
169 },
170 // enable autoScroll
171 autoScroll: true,
172  
173 onmove: dragMoveListener,
174 onend: function (event) {
175 event.target.style.opacity = 1;
176 },
177 onstart: function(event) {
178 event.target.style.opacity = 0.5;
179 }
180 });
25 office 181  
29 office 182 // Closing windows.
183 $(document).on('click', '.window-manager-close-button', function(event) {
31 office 184 var wm = $('#' + $(this).data('target'));
185 wm.fadeOut(750, function() {
186 wm.remove();
187 });
29 office 188 });
189  
190 // Windows clicking brings to front.
191 $(document).on('click', '.window-manager-window', function(event) {
41 office 192 $(event.target)
193 .closest('.window-manager-window')
194 .css('z-index', getTopWindowIndex() + 1);
29 office 195 });
196  
25 office 197 function dragMoveListener (event) {
198 var target = event.target,
199 // keep the dragged position in the data-x/data-y attributes
200 x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
201 y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
202  
203 // translate the element
204 target.style.webkitTransform =
205 target.style.transform =
206 'translate(' + x + 'px, ' + y + 'px)';
207  
29 office 208 // update the position attributes
25 office 209 target.setAttribute('data-x', x);
210 target.setAttribute('data-y', y);
43 office 211  
212 if(target.classList.contains('window-manager-window')) {
213 // Save the window position.
214 var states = {};
215 var cookie = Cookies.get('window-manager-windows');
216 if(cookie)
217 states = JSON.parse(cookie);
218  
219 states[$(event.target).data('target')] = {
220 x: x,
221 y: y
222 };
223  
224 Cookies.set('window-manager-windows', states, { path: '' });
225 }
226  
227 if(target.classList.contains('window-manager-icon')) {
228 // Save the icon position.
229 var states = {};
230 var cookie = Cookies.get('window-manager-icons');
231 if(cookie)
232 states = JSON.parse(cookie);
233  
234 states[$(event.target).data('target')] = {
235 x: x,
236 y: y
237 };
238  
239 Cookies.set('window-manager-icons', states, { path: '' });
240 }
25 office 241 }
242  
243 // this is used later in the resizing and gesture demos
244 window.dragMoveListener = dragMoveListener;
245 });