You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
12 KiB

  1. 'use strict';
  2. // ax5.ui.mask
  3. (function () {
  4. var UI = ax5.ui;
  5. var U = ax5.util;
  6. var MASK = void 0;
  7. UI.addClass({
  8. className: "mask"
  9. }, function () {
  10. /**
  11. * @class ax5mask
  12. * @classdesc
  13. * @author tom@axisj.com
  14. * @example
  15. * ```js
  16. * var customMask = function customMask() {
  17. * var cTmpl = '' +
  18. * '<div class="ax-mask" id="{{maskId}}" >' +
  19. * ' <div class="ax-mask-bg" style="background-color:red !important;"></div>' +
  20. * ' <div class="ax-mask-content">' +
  21. * ' {{{body}}}' +
  22. * ' </div>' +
  23. * '</div>';
  24. * return cTmpl;
  25. * };
  26. * ax5.ui.mask.tmpl.customMask = customMask;
  27. *
  28. * var mask = new ax5.ui.mask();
  29. *
  30. * mask.open({
  31. * templateName: 'customMask',
  32. * content: 'custom MASK on target',
  33. * target: $("#user-content").get(0),
  34. * onClick: function(){
  35. * console.log(this);
  36. * }
  37. * });
  38. * ```
  39. */
  40. return function () {
  41. var self = this,
  42. cfg = void 0;
  43. this.instanceId = ax5.getGuid();
  44. this.config = {
  45. theme: '',
  46. target: jQuery(document.body).get(0),
  47. animateTime: 250
  48. };
  49. this.maskContent = '';
  50. this.status = "off";
  51. cfg = this.config;
  52. var onStateChanged = function onStateChanged(opts, that) {
  53. if (opts && opts.onStateChanged) {
  54. opts.onStateChanged.call(that, that);
  55. } else if (this.onStateChanged) {
  56. this.onStateChanged.call(that, that);
  57. }
  58. opts = null;
  59. that = null;
  60. return true;
  61. };
  62. var getBodyTmpl = function getBodyTmpl(data) {
  63. if (typeof data.templateName === "undefined") data.templateName = "defaultMask";
  64. return MASK.tmpl.get.call(this, data.templateName, data);
  65. };
  66. var setBody = function setBody(content) {
  67. this.maskContent = content;
  68. };
  69. /**
  70. * Preferences of Mask UI
  71. * @method ax5mask.setConfig
  72. * @param {Object} config - 클래스 속성값
  73. * @returns {ax5mask}
  74. * @example
  75. * ```
  76. * setConfig({
  77. * target : {Element|AX5 nodelist}, // 마스크 처리할 대상
  78. * content : {String}, // 마스크안에 들어가는 내용물
  79. * onStateChanged: function(){} // 마스크 상태변경 시 호출되는 함수 this.type으로 예외처리 가능
  80. * }
  81. * ```
  82. */
  83. this.init = function () {
  84. // after setConfig();
  85. this.onStateChanged = cfg.onStateChanged;
  86. this.onClick = cfg.onClick;
  87. if (this.config.content) setBody.call(this, this.config.content);
  88. };
  89. /**
  90. * open mask
  91. * target 주지 않으면 기본적으로 body 마스크가 적용되고 원하는 타겟을 지정해서 마스크를 씌울 있습니다.
  92. * 기본 정의된 마스크 외에 사용자가 템플릿을 정의해서 마스크를 사용 가능합니다.
  93. * @method ax5mask.open
  94. * @param {Object} config
  95. * @param {String} config
  96. * @returns {ax5mask}
  97. * @example
  98. * ```js
  99. * my_mask.open({
  100. * target: document.body,
  101. * content: "<h1>Loading..</h1>",
  102. * onStateChanged: function () {
  103. *
  104. * }
  105. * });
  106. *
  107. * my_mask.open({
  108. * target: $("#mask-target").get(0), // dom Element
  109. * content: "<h1>Loading..</h1>",
  110. * onStateChanged: function () {
  111. *
  112. * }
  113. * });
  114. *
  115. *
  116. * var customMask = function customMask() {
  117. * var cTmpl = '' +
  118. * '<div class="ax-mask" id="{{maskId}}" >' +
  119. * ' <div class="ax-mask-bg" style="background-color:red !important;"></div>' +
  120. * ' <div class="ax-mask-content">' +
  121. * ' {{{body}}}' +
  122. * ' </div>' +
  123. * '</div>';
  124. * return cTmpl;
  125. * };
  126. * ax5.ui.mask.tmpl.customMask = customMask;
  127. *
  128. * my_mask.open({
  129. * target: $("#mask-target").get(0), // dom Element
  130. * content: "<h1>Loading..</h1>",
  131. *
  132. * onStateChanged: function () {
  133. *
  134. * }
  135. * });
  136. * ```
  137. */
  138. this.open = function (options) {
  139. if (this.status === "on") this.close();
  140. if (options && options.content) setBody.call(this, options.content);
  141. if (options && typeof options.templateName === "undefined") options.templateName = "defaultMask";
  142. self.maskConfig = jQuery.extend(true, {}, this.config, options);
  143. var _cfg = self.maskConfig,
  144. target = _cfg.target,
  145. $target = jQuery(target),
  146. maskId = 'ax-mask-' + ax5.getGuid(),
  147. $mask = void 0,
  148. css = {},
  149. that = {},
  150. templateName = _cfg.templateName,
  151. body = getBodyTmpl({
  152. theme: _cfg.theme,
  153. maskId: maskId,
  154. body: this.maskContent,
  155. templateName: templateName
  156. });
  157. jQuery(document.body).append(body);
  158. // 마스크의 타겟이 html body 가 아니라면
  159. if (target && target !== jQuery(document.body).get(0)) {
  160. css = {
  161. position: _cfg.position || "absolute",
  162. left: $target.offset().left,
  163. top: $target.offset().top,
  164. width: $target.outerWidth(),
  165. height: $target.outerHeight()
  166. };
  167. $target.addClass("ax-masking");
  168. // 마스크의 타겟이 html body가 아닌경우 window resize 이벤트를 추적하여 엘리먼트 마스크의 CSS 속성 변경
  169. jQuery(window).on("resize.ax5mask-" + this.instanceId, function (_$target) {
  170. this.align();
  171. }.bind(this));
  172. }
  173. if (typeof self.maskConfig.zIndex !== "undefined") {
  174. css["z-index"] = self.maskConfig.zIndex;
  175. }
  176. this.$mask = $mask = jQuery("#" + maskId);
  177. this.$target = $target;
  178. this.status = "on";
  179. $mask.css(css);
  180. if (_cfg.onClick) {
  181. $mask.on("click", function (e) {
  182. that = {
  183. self: self,
  184. state: "open",
  185. type: "click"
  186. };
  187. self.maskConfig.onClick.call(that, that);
  188. });
  189. }
  190. onStateChanged.call(this, null, {
  191. self: this,
  192. state: "open"
  193. });
  194. options = null;
  195. _cfg = null;
  196. target = null;
  197. $target = null;
  198. maskId = null;
  199. $mask = null;
  200. css = null;
  201. that = null;
  202. templateName = null;
  203. body = null;
  204. return this;
  205. };
  206. /**
  207. * close mask
  208. * @method ax5mask.close
  209. * @param {Number} [_delay=0]
  210. * @returns {ax5mask}
  211. * @example
  212. * ```
  213. * my_mask.close();
  214. * ```
  215. */
  216. this.close = function (_delay) {
  217. if (this.$mask) {
  218. var _close = function _close() {
  219. this.status = "off";
  220. this.$mask.remove();
  221. this.$target.removeClass("ax-masking");
  222. onStateChanged.call(this, null, {
  223. self: this,
  224. state: "close"
  225. });
  226. jQuery(window).off("resize.ax5mask-" + this.instanceId);
  227. };
  228. if (_delay) {
  229. setTimeout(function () {
  230. _close.call(this);
  231. }.bind(this), _delay);
  232. } else {
  233. _close.call(this);
  234. }
  235. }
  236. return this;
  237. };
  238. /**
  239. * @method ax5mask.fadeOut
  240. * @returns {ax5mask}
  241. */
  242. this.fadeOut = function () {
  243. if (this.$mask) {
  244. var _close = function _close() {
  245. this.status = "off";
  246. this.$mask.remove();
  247. this.$target.removeClass("ax-masking");
  248. onStateChanged.call(this, null, {
  249. self: this,
  250. state: "close"
  251. });
  252. jQuery(window).off("resize.ax5mask-" + this.instanceId);
  253. };
  254. this.$mask.addClass("fade-out");
  255. setTimeout(function () {
  256. _close.call(this);
  257. }.bind(this), cfg.animateTime);
  258. }
  259. return this;
  260. };
  261. /**
  262. * @method ax5mask.align
  263. * @returns {ax5mask}
  264. */
  265. this.align = function () {
  266. if (this.maskConfig && this.maskConfig.target && this.maskConfig.target !== jQuery(document.body).get(0)) {
  267. try {
  268. var css = {
  269. position: this.maskConfig.position || "absolute",
  270. left: this.$target.offset().left,
  271. top: this.$target.offset().top,
  272. width: this.$target.outerWidth(),
  273. height: this.$target.outerHeight()
  274. };
  275. this.$mask.css(css);
  276. } catch (e) {}
  277. }
  278. return this;
  279. };
  280. this.pullRequest = function () {
  281. console.log("test pullRequest01");
  282. console.log("test pullRequest02");
  283. };
  284. // 클래스 생성자
  285. this.main = function () {
  286. UI.mask_instance = UI.mask_instance || [];
  287. UI.mask_instance.push(this);
  288. if (arguments && U.isObject(arguments[0])) {
  289. this.setConfig(arguments[0]);
  290. }
  291. }.apply(this, arguments);
  292. };
  293. }());
  294. MASK = ax5.ui.mask;
  295. })();
  296. // ax5.ui.mask.tmpl
  297. (function () {
  298. var MASK = ax5.ui.mask;
  299. var defaultMask = function defaultMask(columnKeys) {
  300. return '\n <div class="ax-mask {{theme}}" id="{{maskId}}">\n <div class="ax-mask-bg"></div>\n <div class="ax-mask-content">\n <div class="ax-mask-body">\n {{{body}}}\n </div>\n </div>\n </div>\n ';
  301. };
  302. MASK.tmpl = {
  303. "defaultMask": defaultMask,
  304. get: function get(tmplName, data, columnKeys) {
  305. return ax5.mustache.render(MASK.tmpl[tmplName].call(this, columnKeys), data);
  306. }
  307. };
  308. })();