Skip to content

Instantly share code, notes, and snippets.

@aerith
Created September 8, 2011 05:17
Show Gist options
  • Save aerith/1202682 to your computer and use it in GitHub Desktop.
Save aerith/1202682 to your computer and use it in GitHub Desktop.
(function (ns, w, d, $) {
var harukaze = ns.harukaze = {};
harukaze.util = {
isElement: function (object) {
return object && object.nodeType && object.nodeType == 1;
},
boolean: {
isTrue: function (value) {
switch (value) {
case '1':
case 't':
case 'true':
return true;
}
return Boolean(value);
},
isFalse: function (value) {
switch (value) {
case '0':
case 'f':
case 'false':
return true;
}
return !Boolean(value);
}
},
html: {
escape: function (value) {
return value.toString()
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
},
unescape: function (value) {
return value.toString()
.replace(/&amp;/g, '&')
.replace(/&quot;/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
}
}
};
harukaze.event = {
listener: {
_items: {},
has: function (name, index) {
var exists = name in this._items && this._items[name].length > 0;
if (typeof index !== 'undefined') {
return exists && index in this._items[name];
}
return exists;
},
get: function (name) {
if (this.has(name)) {
return this._items[name];
}
},
add: function (name, handler, priority) {
if (!this.has(name)) this._items[name] = [];
if (typeof handler == 'function') {
this._items[name].push({
name: name,
handler: handler,
priority: Number(priority) || 0
});
}
},
remove: function (name, index) {
if (this.has(name, index)) {
this._items[name].splice(index, 1);
}
},
sort: function (name, handler) {
if (this.has(name)) {
if (typeof handler == 'function') {
this._items[name].sort(handler);
} else {
this._items[name].sort(function (a, b) {
return Number(b.priority) - Number(a.priority);
});
}
return this._items[name];
}
}
},
dispatch: function () {
var args = Array.prototype.slice.call(arguments);
var name = args.shift();
if (this.listener.has(name)) {
var listeners = this.listener.sort(name);
var event = {
is_propagation_stopped: false,
stopPropagation: function () { this.is_propagation_stopped = true; }
};
args.unshift(event);
for (var i = 0, l = listeners.length; i < l; i++) {
var result = listeners[i].handler.apply(this, args);
if (event.is_propagation_stopped || result === false) {
return;
}
}
}
}
}
harukaze.popup = {
manager: {
_items: [],
popupStyle: {
position: 'absolute'
},
modalClass: 'popup-modal',
modalStyle: {
position: 'absolute',
background: '#000',
opacity: '0.8',
top: '0',
right: '0',
bottom: '0',
left: '0',
margin: '0',
padding: '0'
},
createModal: function (id) {
var modal = $(document.createElement('div'))
.attr('id', id + '-modal')
.hide();
return modal;
},
add: function (element, parent, modal, config) {
if (!harukaze.util.isElement(element)) return;
var popup = this.find(element);
if (popup) {
this._show(popup);
} else {
parent = parent ? $(parent) : $(d.body) ;
modal = Boolean(modal);
var internal_id = 'popup-' + (new Date()).getTime().toString();
var popup = $(document.createElement('div'))
.attr('id', internal_id)
.append(element)
.hide();
this._items.push({
id: internal_id,
element: element,
parent: parent,
modal: Boolean(modal)
});
this._create(popup, parent, modal, config);
}
},
_create: function (popup, parent, modal, config) {
if (Boolean(modal)) {
var modal_layer = this.createModal(popup.attr('id'));
var modal_style = $.extend({}, this.modalStyle, config && "modalStyle" in config ? config.modalStyle : {});
modal_layer.css(modal_style).appendTo(d.body).show();
modal_layer.click(function () {
harukaze.popup.manager._hide(popup);
});
}
var popup_style = $.extend({}, this.popupStyle, config && "popupStyle" in config ? config.popupStyle : {});
var parent_offset = parent.offset() || { top: 0, left: 0 };
popup.css(popup_style).appendTo(parent);
var parent_width = parent.is('body') ? $(w).width() : parent.width() ;
var parent_height = parent.is('body') ? $(w).height() : parent.height() ;
var popup_left = parent_offset.left + (parent_width / 2) - (popup.width() / 2);
var popup_top = parent_offset.top + (parent_height / 2) - (popup.height() / 2);
return popup.css({ top: popup_top, left: popup_left }).show();
},
remove: function (element) {
for (var i = 0, l = this._items.length; i > l; i++) {
var item = this._items[i];
if (element === item.element) {
var popup = $('#' + item.id);
this._hide(popup);
break;
}
}
},
_show: function (popup) {
var modal = $('#' + popup.attr('id') + '-modal');
if (modal.length) {
modal.show();
}
return popup.show();
},
_hide: function (popup) {
var modal = $('#' + popup.attr('id') + '-modal');
if (modal.length) {
modal.hide();
}
return popup.hide();
},
find: function (element) {
for (var i = 0, l = this._items.length; i < l; i++) {
var item = this._items[i];
if (element === item.element) {
var popup = $('#' + item.id);
if (popup.length) {
return popup;
}
}
}
return null;
}
},
show: function (element) {
this.manager.add(element, null, false);
}
};
})(this, window, document, jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment