root / ui / static / snf / js / views.js @ f0532b59
History | View | Annotate | Download (9.3 kB)
1 |
;(function(root){
|
---|---|
2 |
|
3 |
// root
|
4 |
var root = root;
|
5 |
|
6 |
// setup namepsaces
|
7 |
var snf = root.synnefo = root.synnefo || {};
|
8 |
var models = snf.models = snf.models || {}
|
9 |
var storage = snf.storage = snf.storage || {};
|
10 |
var views = snf.views = snf.views || {}
|
11 |
var util = snf.util = snf.util || {}
|
12 |
|
13 |
// shortcuts
|
14 |
var bb = root.Backbone;
|
15 |
|
16 |
// logging
|
17 |
var logger = new snf.logging.logger("SNF-VIEWS"); |
18 |
var debug = _.bind(logger.debug, logger);
|
19 |
|
20 |
// Base view object
|
21 |
views.View = bb.View.extend({ |
22 |
// the main element of the view
|
23 |
// view delegates show, visible, hide etc to this element
|
24 |
view_id: false, |
25 |
|
26 |
el: 'body', |
27 |
data_from: false, |
28 |
selectors: {},
|
29 |
|
30 |
initialize: function() { |
31 |
this.log = new snf.logging.logger("SNF-VIEWS:" + this.view_id); |
32 |
}, |
33 |
|
34 |
// is the view visible ?
|
35 |
visible: function(){ |
36 |
return $(this.el).is(":visible"); |
37 |
}, |
38 |
|
39 |
// hide view
|
40 |
hide: function() { |
41 |
if (!this.visible()) { return this }; |
42 |
return $(this.el).hide(); |
43 |
}, |
44 |
|
45 |
// show view
|
46 |
show: function() { |
47 |
if (this.visible()) { return this }; |
48 |
$(this.el).show(); |
49 |
if (this.show_view) { this.show_view.apply(this, arguments)}; |
50 |
}, |
51 |
|
52 |
sel: function(id) { |
53 |
return this.$(this.selectors[id]); |
54 |
}, |
55 |
|
56 |
// animations
|
57 |
fadeIn: function(time, callback) { |
58 |
$(this.el).fadeIn(time, callback); |
59 |
return this.show(); |
60 |
}, |
61 |
|
62 |
fadeOut: function(time, callback) { |
63 |
$(this.el).fadeOut(time, callback); |
64 |
return this.hide(); |
65 |
} |
66 |
}); |
67 |
|
68 |
|
69 |
// overlays registry
|
70 |
views._overlay_index = []; |
71 |
|
72 |
// overlay view helper
|
73 |
views.Overlay = views.View.extend({ |
74 |
view_id: 'overlay', |
75 |
tpl_selector: '#generic-overlay-tpl', |
76 |
css_class: 'overlay', |
77 |
oneInstance: true, |
78 |
fixed: false, |
79 |
|
80 |
|
81 |
initialize: function(options, selector) { |
82 |
this.defaults = {
|
83 |
load: false, |
84 |
closeOnClick: false, |
85 |
mask: {
|
86 |
color: "#444", |
87 |
loadSpeed: 200, |
88 |
opacity: 0.7 |
89 |
} |
90 |
} |
91 |
|
92 |
this.tpl_selector = selector || this.tpl_selector; |
93 |
views.Overlay.__super__.initialize.apply(this);
|
94 |
views._overlay_index.push(this);
|
95 |
|
96 |
this.options = _.extend(this.defaults, options); |
97 |
this.options.clone = this.options.clone == undefined ? true : this.options.clonde; |
98 |
this.options.fixed = this.fixed; |
99 |
|
100 |
this.options.onOpen = this.options.onOpen || function() {}; |
101 |
this.options.onClose = this.options.onClose || function() {}; |
102 |
this.options.beforeOpen = this.options.beforeOpen || function() {}; |
103 |
this.options.beforeClose = this.options.beforeClose || function() {}; |
104 |
this.el = this.create_element(); |
105 |
this.el.hide();
|
106 |
|
107 |
var ajax_params = _.clone(this.options); |
108 |
|
109 |
ajax_params.onBeforeLoad = _.bind(this._beforeOpen, this); |
110 |
ajax_params.onLoad = _.bind(this._onOpen, this); |
111 |
ajax_params.onBeforeClose = _.bind(this._beforeClose, this); |
112 |
ajax_params.onClose = _.bind(this._onClose, this); |
113 |
ajax_params.oneInstance = this.oneInstance;
|
114 |
// create overlay
|
115 |
// TODO: does this return overlay object ?? (to avoid the next code line)
|
116 |
$(this.el).overlay(ajax_params); |
117 |
|
118 |
this.overlay = $(this.el).overlay(); |
119 |
this.append_css = this.options ? this.options.css_class ? this.options.css_class : "" : ""; |
120 |
return this; |
121 |
}, |
122 |
|
123 |
create_element: function() { |
124 |
var el = undefined; |
125 |
if (this.options.clone) { |
126 |
el = $(this.tpl_selector).clone(); |
127 |
} else {
|
128 |
el = $(this.tpl_selector); |
129 |
} |
130 |
|
131 |
// append content
|
132 |
if (this.content_selector) { |
133 |
var content = $(this.content_selector).clone(); |
134 |
content.addClass("content");
|
135 |
|
136 |
if ($(el).find(".content").length) { |
137 |
$(el).find(".content").replaceWith(content); |
138 |
} |
139 |
content.removeClass("hidden");
|
140 |
} |
141 |
|
142 |
if (this.overlay_id) { |
143 |
} |
144 |
|
145 |
$(el).addClass("overlay"); |
146 |
if (this.css_class) { |
147 |
$(el).addClass(this.css_class); |
148 |
} |
149 |
|
150 |
if (this.options.clone) { |
151 |
$("body").append(el); |
152 |
} |
153 |
|
154 |
return el;
|
155 |
}, |
156 |
|
157 |
set_title: function(title) { |
158 |
if (title || this.title) { |
159 |
$(this.el).find(".header .title").html(title || this.title) |
160 |
} |
161 |
}, |
162 |
|
163 |
set_subtitle: function(subtitle) { |
164 |
if (subtitle || this.subtitle) { |
165 |
$(this.el).find(".header .subtitle").html(subtitle || this.subtitle) |
166 |
} |
167 |
}, |
168 |
|
169 |
_beforeOpen: function() { |
170 |
if (this.append_css) { |
171 |
$(this.el).addClass(this.append_css); |
172 |
} |
173 |
|
174 |
this.set_title();
|
175 |
this.set_subtitle();
|
176 |
|
177 |
this.beforeOpen.apply(this, arguments); |
178 |
this.options.beforeOpen.apply(this, arguments); |
179 |
}, |
180 |
|
181 |
_onOpen: function() { |
182 |
if ($(this.el).find(".closeme").length) { |
183 |
$(this.el).find(".closeme").click(_.bind(function(){ |
184 |
this.hide();
|
185 |
}, this))
|
186 |
} |
187 |
this.onOpen.apply(this, arguments); |
188 |
this.options.onOpen.apply(this, arguments); |
189 |
}, |
190 |
|
191 |
_beforeClose: function() { |
192 |
this.beforeClose.apply(this, arguments); |
193 |
this.options.beforeClose.apply(this, arguments); |
194 |
}, |
195 |
|
196 |
_onClose: function() { |
197 |
if (this.append_css) { |
198 |
$(this.el).removeClass(this.append_css); |
199 |
} |
200 |
this.onClose.apply(this, arguments); |
201 |
this.options.onClose.apply(this, arguments); |
202 |
}, |
203 |
|
204 |
beforeOpen: function () {}, |
205 |
onOpen: function () {}, |
206 |
beforeClose: function () {}, |
207 |
onClose: function () {}, |
208 |
|
209 |
show: function() { |
210 |
var hidden = false; |
211 |
_.each(views._overlay_index, function(ovr){
|
212 |
if (ovr == this) { return }; |
213 |
if (ovr.visible()) {
|
214 |
hidden = true;
|
215 |
ovr.hide(); |
216 |
} |
217 |
}) |
218 |
|
219 |
if (hidden) {
|
220 |
delay = 300;
|
221 |
} else {
|
222 |
delay = 0;
|
223 |
} |
224 |
window.setTimeout(_.bind(function(){ this.overlay.load() }, this), delay) |
225 |
return this; |
226 |
}, |
227 |
|
228 |
hide: function() { |
229 |
if (!this.overlay.isOpened()) { |
230 |
// if its not opened events wont trigger
|
231 |
this._onClose()
|
232 |
} else {
|
233 |
this.overlay.close();
|
234 |
} |
235 |
return this; |
236 |
} |
237 |
}); |
238 |
|
239 |
|
240 |
// overlay view helper
|
241 |
views.VMOverlay = views.Overlay.extend({ |
242 |
|
243 |
initialize: function() { |
244 |
views.VMOverlay.__super__.initialize.apply(this);
|
245 |
this.vm = undefined; |
246 |
this.view_id_tpl = this.view_id; |
247 |
|
248 |
_.bindAll(this, "_handle_vm_change", "_handle_vm_remove"); |
249 |
}, |
250 |
|
251 |
set_vm: function(vm) { |
252 |
if (this.vm) { this.unbind_vm_handlers }; |
253 |
this.vm = vm;
|
254 |
this.view_id = this.view_id + "_" + vm.id; |
255 |
this.bind_vm_handlers();
|
256 |
}, |
257 |
|
258 |
bind_vm_handlers: function() { |
259 |
this.log.debug("binding handlers"); |
260 |
this.vm.bind("change", this._handle_vm_change); |
261 |
storage.vms.bind("remove", this._handle_vm_remove); |
262 |
}, |
263 |
|
264 |
unbind_vm_handlers: function() { |
265 |
this.log.debug("unbinding handlers", this.vm); |
266 |
if (!this.vm) { return }; |
267 |
this.vm.unbind("change", this._handle_vm_change); |
268 |
storage.vms.unbind("remove", this._handle_vm_remove); |
269 |
}, |
270 |
|
271 |
_update_vm_details: function() { |
272 |
if (!this.vm) { console.error("invalid view state"); return } |
273 |
this.set_subtitle(this.vm.get("name") + snf.ui.helpers.vm_icon_tag(this.vm, "small")); |
274 |
|
275 |
var ico_path = snf.ui.helpers.os_icon_path(this.vm.get("OS"), "oslarge"); |
276 |
this.$(".content").css({"background-image":"url(" + ico_path +")"}) |
277 |
this.update_vm_details()
|
278 |
}, |
279 |
|
280 |
update_vm_details: function() {}, |
281 |
handle_vm_remove: function() {}, |
282 |
handle_vm_change: function () {}, |
283 |
|
284 |
_handle_vm_remove: function(vm, collection) { |
285 |
if (this.vm && vm.id == this.vm.id) { |
286 |
this.hide();
|
287 |
} |
288 |
this.handle_vm_remove();
|
289 |
}, |
290 |
|
291 |
_handle_vm_change: function(vm) { |
292 |
this._update_vm_details();
|
293 |
this.handle_vm_change(vm);
|
294 |
}, |
295 |
|
296 |
beforeClose: function() { |
297 |
this.unbind_vm_handlers();
|
298 |
this.vm = undefined; |
299 |
}, |
300 |
|
301 |
show: function(vm) { |
302 |
this.set_vm(vm);
|
303 |
views.VMOverlay.__super__.show.apply(this, arguments); |
304 |
this._update_vm_details();
|
305 |
} |
306 |
|
307 |
}); |
308 |
|
309 |
})(this);
|