Skip to content

Instantly share code, notes, and snippets.

@varya
Created December 13, 2008 17:05
Show Gist options
  • Save varya/35503 to your computer and use it in GitHub Desktop.
Save varya/35503 to your computer and use it in GitHub Desktop.
var LightWindow = function(el, index, params) {
this.el = el;
this.index = index;
this.params = params;
this.loadTemplate();
}
LightWindow.prototype = {
init : function(el, index, params) {
var thisLightWindow = this;
this.params = params;
this.createOverlay();
$(el).bind('click', function(e){
thisLightWindow.showWindow();
});
$(this.close).unbind('click');
$(this.close).bind('click', function(e){
thisLightWindow.hideWindow();
});
},
loadTemplate : function() {
var thisLightWindow = this;
$.get(this.params.template, '', function(data) {
thisLightWindow.template = data;
thisLightWindow.init(thisLightWindow.el, thisLightWindow.index, thisLightWindow.params);
});
},
createOverlay : function() {
var lightwindow = $('#js_LightWindow').get(0);
if (typeof lightwindow == 'undefined') {
$(this.template).appendTo("body");
}
this.lightwindow = $('#js_LightWindow').get(0);
this.container = $(this.lightwindow).find('.js_LightWindow-container').get(0);
this.window = $(this.lightwindow).find('.js_LightWindow-window').get(0);
this.close = $(this.lightwindow).find('.js_LightWindow-close');
this.overlay = $(this.lightwindow).find('.js_LightWindow-overlay').get(0);
this.content = $(this.lightwindow).find('.js_LightWindow-content').get(0);
},
showWindow : function() {
var thisLightWindow = this;
$(this.overlay).css({
'height': $(document).height() + 'px'
});
$(this.window).css({
'height': this.params.size.height + 'px',
'width': this.params.size.width + 'px'
});
var top = ($(window).height() - this.params.size.height)/2 + (window.scrollY | document.body.parentNode.scrollTop);
if (top < 0) { var top = 0; }
$(this.container).css({
top: top
});
$(this.lightwindow).removeClass('g-hidden');
if (!this.result) {
$.get(this.params.content_template, '', function(data) { thisLightWindow.showResult(data); });
} else {
$(this.result).removeClass('g-hidden');
}
},
hideWindow : function() {
$(this.content).find('div.js_LightWindow-result').addClass('g-hidden');
$(this.lightwindow).addClass('g-hidden');
// событие закрывания окна
$(this.el).trigger("LightWindow::closed", {element: this.content, params: this.params});
},
showResult : function(data) {
$(this.content).addClass('g-hidden');
var length = 'js_LightWindow-content_'.length;
this.result = document.createElement("div");
$(this.result).addClass('js_LightWindow-result');
var count = $(this.content).find('div.js_LightWindow-result').length;
this.result.id = 'js_LightWindow-result_' + count;
$(this.result).applyTemplate(data, this.params);
$(this.content).append(this.result);
// инициализация компонент
$(ManagerControl).trigger('Manager::ManagerControl', [this.result]);
$(this.content).removeClass('g-hidden');
this.findElements(this.result);
},
findElements : function(el) {
var thisLightWindow = this;
$('.js_LightWindow-close').bind('click', function(e){
thisLightWindow.hideWindow();
});
}
}
$(ManagerControl).trigger('onLightWindow');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment