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