root / snf-cyclades-app / synnefo / ui / new_ui / ui / javascripts / common.js @ e5c8c342
History | View | Annotate | Download (11.4 kB)
1 |
/*
|
---|---|
2 |
* Various functions that will be used throughout all templates
|
3 |
* are inside ui Object
|
4 |
*/
|
5 |
|
6 |
ui = {}; |
7 |
|
8 |
/* when closeEl el is clicked, its parent with class divToCloseClass slidesUp */
|
9 |
ui.closeDiv = function(closeEl, divToCloseClass) { |
10 |
closeEl.click(function(e){
|
11 |
e.preventDefault(); |
12 |
$(this).parents(divToCloseClass).slideUp('slow'); |
13 |
}); |
14 |
} |
15 |
|
16 |
|
17 |
ui.trimChars = function( str, chars) { |
18 |
if ( str.length>chars){
|
19 |
return $.trim(str).substring(0, chars)+ "..."; |
20 |
} else {
|
21 |
return str;
|
22 |
} |
23 |
} |
24 |
|
25 |
/* sets lt-sidebar height. Useful for jscrollpane scrollbar */
|
26 |
ui.setSidebarHeight = function(){ |
27 |
var WindowHeight = $(window).height(); |
28 |
var h1= WindowHeight - $('.header').outerHeight(); |
29 |
var h2= $('.main').outerHeight(); |
30 |
$('.lt-sidebar').height((h2>h1) ? h2 : h1); |
31 |
} |
32 |
|
33 |
|
34 |
/*
|
35 |
* Logic for Entities actions. Present in items_list pages
|
36 |
* Available categories are :
|
37 |
* - both/single ( for multiple entities/single entities)
|
38 |
* - running/stopped ( for running/stopped entities)
|
39 |
* - permanent ( for entities always active )
|
40 |
*/
|
41 |
ui.entitiesActionsEnabled = function(){ |
42 |
var all = $('.snf-checkbox-checked').length; |
43 |
var running = $('.snf-checkbox-checked').parents('.container').find('.running').length; |
44 |
var stopped = $('.snf-checkbox-checked').parents('.container').find('.stopped').length; |
45 |
$('.header .main-actions li:not(.permanent) a').removeClass('active'); |
46 |
if ( (running*stopped) > 0 ){ |
47 |
$('.main-actions li.both a').addClass('active'); |
48 |
$('.main-actions li.single a').removeClass('active'); |
49 |
} else {
|
50 |
if (running > 0) { |
51 |
$('.main-actions li.both a').addClass('active'); |
52 |
$('.main-actions li.running a').addClass('active'); |
53 |
} else if (stopped>0) { |
54 |
$('.main-actions li.both a').addClass('active'); |
55 |
$('.main-actions li.stopped a').addClass('active'); |
56 |
} |
57 |
if ( all > 1 ) { |
58 |
$('.main-actions li.single a').removeClass('active'); |
59 |
} |
60 |
} |
61 |
} |
62 |
|
63 |
ui.entitiesActionsInit = function(){ |
64 |
|
65 |
$('.entities li .container').mouseleave( |
66 |
function(e){
|
67 |
$(this).find('.snf-checkbox-unchecked').parents('.check').removeClass('active'); |
68 |
} |
69 |
); |
70 |
|
71 |
$('.entities .container .check').click(function(e){ |
72 |
e.preventDefault(); |
73 |
var checkbox = $(this).find('.snf-checkbox-unchecked, .snf-checkbox-checked'); |
74 |
checkbox.toggleClass('snf-checkbox-unchecked');
|
75 |
checkbox.toggleClass('snf-checkbox-checked');
|
76 |
|
77 |
if(checkbox.hasClass('snf-checkbox-checked')){ |
78 |
$(this).parents('.container').addClass('set-bg'); |
79 |
$(this).addClass('active'); |
80 |
} |
81 |
else{
|
82 |
$(this).parents('.container').removeClass('set-bg'); |
83 |
} |
84 |
ui.entitiesActionsEnabled(); |
85 |
}) |
86 |
|
87 |
} |
88 |
|
89 |
/*
|
90 |
* In order for the editable value functionality to work, the html markup
|
91 |
* should be:
|
92 |
<div class="editable">
|
93 |
<span class="input-txt">editable value</span>
|
94 |
<input type="text">
|
95 |
<a href="#" class="edit">edit</a>
|
96 |
<a href="#" class="save">save</a>
|
97 |
<a href="#" class="cancel">cancel</a>
|
98 |
</div>
|
99 |
*/
|
100 |
ui.editable = function(){ |
101 |
|
102 |
/*
|
103 |
* resetForm hides save and cancel buttons,
|
104 |
* text input and shows input-txt. resetForm does not alter
|
105 |
* input-txt content.
|
106 |
*/
|
107 |
|
108 |
function resetForm(e, elem) { |
109 |
var el = elem.parents('.editable'); |
110 |
el.find('input[type="text"]').hide();
|
111 |
el.find('a.cancel, a.save').hide();
|
112 |
el.find('a.edit, .input-txt').show();
|
113 |
} |
114 |
|
115 |
/*
|
116 |
* showForm hides input-txt, shows save and cancel buttons and
|
117 |
* sets input value to input-txt content.
|
118 |
*/
|
119 |
function showForm(e,elem) { |
120 |
e.stopPropagation(); |
121 |
e.preventDefault(); |
122 |
var el = elem.parents('.editable'); |
123 |
el.find('input[type="text"]').val(el.find('.input-txt').html()); |
124 |
el.find('input[type="text"]').show();
|
125 |
el.find('a.cancel, a.save').show();
|
126 |
elem.hide(); |
127 |
el.find('.input-txt').hide();
|
128 |
|
129 |
} |
130 |
|
131 |
/*
|
132 |
setValue sets input-txt value to the input value.
|
133 |
Makes sure that the input value is not empty.
|
134 |
TODO:
|
135 |
Ajax request to submit form
|
136 |
*/
|
137 |
|
138 |
function setValue(elem) { |
139 |
var el = elem.parents('.editable'); |
140 |
if( el.find('input[type="text"]').val() ) { |
141 |
el.find('.input-txt').html(el.find('input[type="text"]').val()); |
142 |
} |
143 |
} |
144 |
|
145 |
|
146 |
$('.editable .edit').click(function(e){ |
147 |
showForm(e, $(this)); |
148 |
}) |
149 |
|
150 |
$('.editable .cancel').click(function(e){ |
151 |
e.stopPropagation(); |
152 |
e.preventDefault(); |
153 |
resetForm(e, $(this)); |
154 |
}) |
155 |
|
156 |
$('.editable .save').click(function(e){ |
157 |
e.stopPropagation(); |
158 |
e.preventDefault(); |
159 |
setValue($(this)); |
160 |
resetForm(e, $(this)); |
161 |
|
162 |
}) |
163 |
|
164 |
|
165 |
$('.editable input[type="text"]').click(function(e){ |
166 |
e.stopPropagation(); |
167 |
}) |
168 |
|
169 |
$('.editable input[type="text"]').keyup(function(e){ |
170 |
if(e.keyCode == 13) { |
171 |
setValue($(this)); |
172 |
resetForm(e, $(this)); |
173 |
|
174 |
} |
175 |
|
176 |
}) |
177 |
|
178 |
$('html').click(function(e) { |
179 |
resetForm(e, $('.editable a.cancel')); |
180 |
}); |
181 |
|
182 |
} |
183 |
|
184 |
/* TODO: better overlay functionality */
|
185 |
ui.overlay = function() { |
186 |
$('[data-overlay-id]').click(function(e){ |
187 |
e.preventDefault(); |
188 |
var el = $(this); |
189 |
// main-actions a need to be active to trigger overlay
|
190 |
if ( (el.parents('.main-actions').find('li a.active').length == 0) && (el.parents('.main-actions').length > 0) ) { |
191 |
return false; |
192 |
} |
193 |
var id = el.data('overlay-id'); |
194 |
$('.overlay-area').show(); |
195 |
$(id).slideDown('slow'); |
196 |
}); |
197 |
|
198 |
|
199 |
} |
200 |
|
201 |
//permits only one checkbox to be checked in a ul
|
202 |
ui.checkAction = function(checkbox) { |
203 |
var otherChecked = checkbox.closest('li').siblings('li').find('span.snf-checkbox-checked').length; |
204 |
if(otherChecked!=0){ |
205 |
checkbox.toggleClass('snf-checkbox-checked');
|
206 |
checkbox.toggleClass('snf-checkbox-unchecked');
|
207 |
} |
208 |
|
209 |
} |
210 |
|
211 |
|
212 |
// when user moves a vm or network icon (list view)
|
213 |
ui.placementByUser = function() { |
214 |
if($('.sortable').length != 0) { |
215 |
$( ".sortable" ).sortable({ |
216 |
items: "> li:not(:last)", |
217 |
stop: function(event, ui) { |
218 |
$.map($(this).find('li'), function(el) { |
219 |
return $(el).attr('data-order', $(el).index()); |
220 |
}); |
221 |
} |
222 |
}); |
223 |
|
224 |
$( ".sortable" ).disableSelection(); //i think unnecessary |
225 |
} |
226 |
} |
227 |
|
228 |
//create vm
|
229 |
ui.pickFlavor = function(flavorSelection) { |
230 |
ui.select_flavor = 1;
|
231 |
console.log(flavorSelection); |
232 |
var classes = $(flavorSelection).attr('class').split(" "); |
233 |
// the second class is: 'small_flavor' or 'medium_flavor' or 'large_flavor'
|
234 |
|
235 |
$(flavorSelection).parent('li').siblings('li').find('a.chosen_flavor').removeClass('chosen_flavor'); |
236 |
$(flavorSelection).addClass('chosen_flavor'); |
237 |
$('.select-flavor').find('dl.cpus span.current, dl.ram span.current, dl.disk span.current').removeClass('current'); |
238 |
$('.select-flavor').find('.'+classes[1]).addClass('current'); |
239 |
|
240 |
} |
241 |
|
242 |
|
243 |
ui.pickResources = function(resource) { |
244 |
$(resource).parents('dl').find('span').removeClass('current'); |
245 |
$(resource).addClass('current'); |
246 |
if(ui.select_flavor == 1){ |
247 |
if(!$(resource).parents('dl').hasClass('storage')){ |
248 |
$('.lt-sidebar').find('a.chosen_flavor').removeClass('chosen_flavor'); |
249 |
select_flavor = 0;
|
250 |
} |
251 |
} |
252 |
} |
253 |
|
254 |
|
255 |
ui.netOptions = function(option) { |
256 |
var checkbox = $(option).find('.snf-checkbox-checked, .snf-checkbox-unchecked'); |
257 |
var list = $(option).closest('ul'); |
258 |
|
259 |
ui.checkAction(checkbox); //allazw to checkbox p pataw
|
260 |
if(list.hasClass('subnet_options')){ |
261 |
checkedBefore = $(option).closest('li').siblings('li').find('span.snf-checkbox-checked'); |
262 |
if($(checkedBefore).closest('li').find('a').hasClass('manual')) |
263 |
{ |
264 |
$(checkedBefore).closest('li').find('.manual_sub').hide(); |
265 |
} |
266 |
ui.checkAction(checkedBefore); //allazw ta alla checkboxes
|
267 |
|
268 |
if($(option).hasClass('manual')) { |
269 |
|
270 |
if($(checkbox).hasClass('snf-checkbox-unchecked')) { |
271 |
$(option).closest('span').find('.manual_sub').hide(); |
272 |
} |
273 |
else {
|
274 |
$(option).closest('span').find('.manual_sub').show(); |
275 |
} |
276 |
} |
277 |
} |
278 |
else if($(option).closest('li').hasClass('dhcp_option')) { |
279 |
if($(checkbox).hasClass('snf-checkbox-unchecked')) { |
280 |
$('.network_options').find('.subnet_options').hide(); |
281 |
} |
282 |
else {
|
283 |
$('.network_options').find('.subnet_options').show(); |
284 |
} |
285 |
} |
286 |
} |
287 |
|
288 |
|
289 |
$(document).ready(function(){ |
290 |
|
291 |
ui.setSidebarHeight(); |
292 |
ui.closeDiv($('.info .close'), '.info'); |
293 |
ui.entitiesActionsInit(); |
294 |
ui.editable(); |
295 |
ui.overlay(); |
296 |
|
297 |
|
298 |
$('.select-os li').click(function(e){ |
299 |
$('.select-os li').removeClass('selected'); |
300 |
$(this).addClass('selected'); |
301 |
}) |
302 |
|
303 |
|
304 |
if ($('.overlay').length >0 ){ |
305 |
$('body').addClass('with-overlay'); |
306 |
} |
307 |
|
308 |
$('.new-btn a.current').click(function(e){ |
309 |
e.preventDefault(); |
310 |
}) |
311 |
|
312 |
$('.main-actions li a').click(function(e){ |
313 |
if (!($(this).hasClass('active'))) { |
314 |
e.preventDefault(); |
315 |
} |
316 |
}) |
317 |
$('.scroll-pane').jScrollPane(); |
318 |
|
319 |
$('.main .items-list .title em').each(function(){ |
320 |
$(this).html( ui.trimChars($(this).html(), 20) ); |
321 |
|
322 |
}) |
323 |
|
324 |
$('.main-actions li a').click(function(e){ |
325 |
if (!($(this).hasClass('active'))) { |
326 |
e.preventDefault(); |
327 |
} |
328 |
}) |
329 |
$('.overlay-area .close').click(function(e){ |
330 |
e.preventDefault(); |
331 |
$(this).parents('.overlay-area').hide(); |
332 |
$(this).parents('.overlay-area').find($('[data-overlay-id]')).hide(); |
333 |
}) |
334 |
|
335 |
$('.browse-files').click(function(e){ |
336 |
e.preventDefault(); |
337 |
}) |
338 |
|
339 |
Dropzone.options.filesDropzone = { |
340 |
dictDefaultMessage:'', |
341 |
clickable: '.browse-files', |
342 |
previewsContainer: '.dropzone-files', |
343 |
createImageThumbnails: false, |
344 |
dictRemoveFile: "snf-Remove file", |
345 |
}; |
346 |
|
347 |
|
348 |
$('.main .files').magnificPopup({ |
349 |
delegate: 'a.show.image', |
350 |
type: 'image', |
351 |
tLoading: 'Loading image #%curr%...', |
352 |
mainClass: 'mfp-img-mobile', |
353 |
gallery: {
|
354 |
enabled: true, |
355 |
navigateByImgClick: true, |
356 |
preload: [0,1] // Will preload 0 - before current, and 1 after the current image |
357 |
}, |
358 |
image: {
|
359 |
tError: '<a href="%url%">The image #%curr%</a> could not be loaded.', |
360 |
titleSrc: function(item) { |
361 |
return item.el.data('title'); |
362 |
} |
363 |
} |
364 |
}); |
365 |
|
366 |
|
367 |
// create vm
|
368 |
// choose resources one by one
|
369 |
ui.select_flavor =0;
|
370 |
$('.select-flavor dl span').click(function(e){ |
371 |
e.preventDefault(); |
372 |
ui.pickResources(this);
|
373 |
|
374 |
|
375 |
}); |
376 |
|
377 |
// create vm
|
378 |
// if a predefined flavor has been selected from the user, it highlights the proper resources
|
379 |
$('.lt-sidebar li a.flavor_selection').click(function(e){ |
380 |
e.preventDefault(); |
381 |
ui.pickFlavor(this);
|
382 |
}); |
383 |
|
384 |
// create network
|
385 |
// checkbox: basic reaction on click (checked, unchecked)
|
386 |
$('.network_options .check').click(function(e){ |
387 |
e.preventDefault(); |
388 |
ui.netOptions(this);
|
389 |
}) |
390 |
|
391 |
ui.placementByUser(); |
392 |
|
393 |
|
394 |
}) |
395 |
|
396 |
|
397 |
$(window).resize(function(e){ |
398 |
ui.setSidebarHeight(); |
399 |
$('.scroll-pane').jScrollPane(); |
400 |
}) |