Guest User

Untitled

a guest
Jan 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1.  
  2. (function (ns, w, d, $) {
  3.  
  4. var harukaze = ns.harukaze = {};
  5.  
  6. harukaze.util = {
  7. isElement: function (object) {
  8. return object && object.nodeType && object.nodeType == 1;
  9. },
  10.  
  11. boolean: {
  12. isTrue: function (value) {
  13. switch (value) {
  14. case '1':
  15. case 't':
  16. case 'true':
  17. return true;
  18. }
  19.  
  20. return Boolean(value);
  21. },
  22.  
  23. isFalse: function (value) {
  24. switch (value) {
  25. case '0':
  26. case 'f':
  27. case 'false':
  28. return true;
  29. }
  30.  
  31. return !Boolean(value);
  32. }
  33. },
  34.  
  35. html: {
  36. escape: function (value) {
  37. return value.toString()
  38. .replace(/&/g, '&')
  39. .replace(/"/g, '"')
  40. .replace(/</g, '<')
  41. .replace(/>/g, '>');
  42. },
  43.  
  44. unescape: function (value) {
  45. return value.toString()
  46. .replace(/&/g, '&')
  47. .replace(/"/g, '"')
  48. .replace(/</g, '<')
  49. .replace(/>/g, '>');
  50. }
  51. }
  52. };
  53.  
  54. harukaze.event = {
  55. listener: {
  56. _items: {},
  57.  
  58. has: function (name, index) {
  59. var exists = name in this._items && this._items[name].length > 0;
  60.  
  61. if (typeof index !== 'undefined') {
  62. return exists && index in this._items[name];
  63. }
  64.  
  65. return exists;
  66. },
  67.  
  68. get: function (name) {
  69. if (this.has(name)) {
  70. return this._items[name];
  71. }
  72. },
  73.  
  74. add: function (name, handler, priority) {
  75. if (!this.has(name)) this._items[name] = [];
  76.  
  77. if (typeof handler == 'function') {
  78. this._items[name].push({
  79. name: name,
  80. handler: handler,
  81. priority: Number(priority) || 0
  82. });
  83. }
  84. },
  85.  
  86. remove: function (name, index) {
  87. if (this.has(name, index)) {
  88. this._items[name].splice(index, 1);
  89. }
  90. },
  91.  
  92. sort: function (name, handler) {
  93. if (this.has(name)) {
  94. if (typeof handler == 'function') {
  95. this._items[name].sort(handler);
  96. } else {
  97. this._items[name].sort(function (a, b) {
  98. return Number(b.priority) - Number(a.priority);
  99. });
  100. }
  101.  
  102. return this._items[name];
  103. }
  104. }
  105. },
  106.  
  107. dispatch: function () {
  108. var args = Array.prototype.slice.call(arguments);
  109. var name = args.shift();
  110.  
  111. if (this.listener.has(name)) {
  112. var listeners = this.listener.sort(name);
  113.  
  114. var event = {
  115. is_propagation_stopped: false,
  116. stopPropagation: function () { this.is_propagation_stopped = true; }
  117. };
  118.  
  119. args.unshift(event);
  120.  
  121. for (var i = 0, l = listeners.length; i < l; i++) {
  122. var result = listeners[i].handler.apply(this, args);
  123.  
  124. if (event.is_propagation_stopped || result === false) {
  125. return;
  126. }
  127. }
  128. }
  129. }
  130. }
  131.  
  132. harukaze.popup = {
  133. manager: {
  134. _items: [],
  135.  
  136. popupStyle: {
  137. position: 'absolute'
  138. },
  139.  
  140. modalClass: 'popup-modal',
  141.  
  142. modalStyle: {
  143. position: 'absolute',
  144. background: '#000',
  145. opacity: '0.8',
  146. top: '0',
  147. right: '0',
  148. bottom: '0',
  149. left: '0',
  150. margin: '0',
  151. padding: '0'
  152. },
  153.  
  154. createModal: function (id) {
  155. var modal = $(document.createElement('div'))
  156. .attr('id', id + '-modal')
  157. .hide();
  158.  
  159. return modal;
  160. },
  161.  
  162. add: function (element, parent, modal, config) {
  163. if (!harukaze.util.isElement(element)) return;
  164.  
  165. var popup = this.find(element);
  166. if (popup) {
  167. this._show(popup);
  168. } else {
  169. parent = parent ? $(parent) : $(d.body) ;
  170. modal = Boolean(modal);
  171.  
  172. var internal_id = 'popup-' + (new Date()).getTime().toString();
  173.  
  174. var popup = $(document.createElement('div'))
  175. .attr('id', internal_id)
  176. .append(element)
  177. .hide();
  178.  
  179. this._items.push({
  180. id: internal_id,
  181. element: element,
  182. parent: parent,
  183. modal: Boolean(modal)
  184. });
  185.  
  186. this._create(popup, parent, modal, config);
  187. }
  188. },
  189.  
  190. _create: function (popup, parent, modal, config) {
  191. if (Boolean(modal)) {
  192. var modal_layer = this.createModal(popup.attr('id'));
  193. var modal_style = $.extend({}, this.modalStyle, config && "modalStyle" in config ? config.modalStyle : {});
  194.  
  195. modal_layer.css(modal_style).appendTo(d.body).show();
  196.  
  197. modal_layer.click(function () {
  198. harukaze.popup.manager._hide(popup);
  199. });
  200. }
  201.  
  202. var popup_style = $.extend({}, this.popupStyle, config && "popupStyle" in config ? config.popupStyle : {});
  203. var parent_offset = parent.offset() || { top: 0, left: 0 };
  204.  
  205.  
  206. popup.css(popup_style).appendTo(parent);
  207.  
  208. var parent_width = parent.is('body') ? $(w).width() : parent.width() ;
  209. var parent_height = parent.is('body') ? $(w).height() : parent.height() ;
  210. var popup_left = parent_offset.left + (parent_width / 2) - (popup.width() / 2);
  211. var popup_top = parent_offset.top + (parent_height / 2) - (popup.height() / 2);
  212.  
  213. return popup.css({ top: popup_top, left: popup_left }).show();
  214. },
  215.  
  216. remove: function (element) {
  217. for (var i = 0, l = this._items.length; i > l; i++) {
  218. var item = this._items[i];
  219. if (element === item.element) {
  220. var popup = $('#' + item.id);
  221. this._hide(popup);
  222. break;
  223. }
  224. }
  225. },
  226.  
  227. _show: function (popup) {
  228. var modal = $('#' + popup.attr('id') + '-modal');
  229. if (modal.length) {
  230. modal.show();
  231. }
  232.  
  233. return popup.show();
  234. },
  235.  
  236. _hide: function (popup) {
  237. var modal = $('#' + popup.attr('id') + '-modal');
  238. if (modal.length) {
  239. modal.hide();
  240. }
  241.  
  242. return popup.hide();
  243. },
  244.  
  245. find: function (element) {
  246. for (var i = 0, l = this._items.length; i < l; i++) {
  247. var item = this._items[i];
  248. if (element === item.element) {
  249. var popup = $('#' + item.id);
  250. if (popup.length) {
  251. return popup;
  252. }
  253. }
  254. }
  255.  
  256. return null;
  257. }
  258. },
  259.  
  260. show: function (element) {
  261. this.manager.add(element, null, false);
  262. }
  263. };
  264.  
  265. })(this, window, document, jQuery);
Add Comment
Please, Sign In to add comment