PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/js/src/components/card.js

https://gitlab.com/darmah250903/agung
JavaScript | 435 lines | 251 code | 75 blank | 109 comment | 43 complexity | 3b5bad0e5ce1f93544fed7499eb3eefc MD5 | raw file
  1. "use strict";
  2. import KTUtil from "./util";
  3. // Component Definition
  4. var KTCard = function(elementId, options) {
  5. // Main object
  6. var the = this;
  7. var init = false;
  8. // Get element object
  9. var element = KTUtil.getById(elementId);
  10. var body = KTUtil.getBody();
  11. if (!element) {
  12. return;
  13. }
  14. // Default options
  15. var defaultOptions = {
  16. toggleSpeed: 400,
  17. sticky: {
  18. releseOnReverse: false,
  19. offset: 300,
  20. zIndex: 101
  21. }
  22. };
  23. ////////////////////////////
  24. // ** Private Methods ** //
  25. ////////////////////////////
  26. var Plugin = {
  27. /**
  28. * Construct
  29. */
  30. construct: function(options) {
  31. if (KTUtil.data(element).has('card')) {
  32. the = KTUtil.data(element).get('card');
  33. } else {
  34. // reset menu
  35. Plugin.init(options);
  36. // build menu
  37. Plugin.build();
  38. KTUtil.data(element).set('card', the);
  39. }
  40. return the;
  41. },
  42. /**
  43. * Init card
  44. */
  45. init: function(options) {
  46. the.element = element;
  47. the.events = [];
  48. // merge default and user defined options
  49. the.options = KTUtil.deepExtend({}, defaultOptions, options);
  50. the.header = KTUtil.child(element, '.card-header');
  51. the.footer = KTUtil.child(element, '.card-footer');
  52. if (KTUtil.child(element, '.card-body')) {
  53. the.body = KTUtil.child(element, '.card-body');
  54. } else if (KTUtil.child(element, '.form')) {
  55. the.body = KTUtil.child(element, '.form');
  56. }
  57. },
  58. /**
  59. * Build Form Wizard
  60. */
  61. build: function() {
  62. // Remove
  63. var remove = KTUtil.find(the.header, '[data-card-tool=remove]');
  64. if (remove) {
  65. KTUtil.addEvent(remove, 'click', function(e) {
  66. e.preventDefault();
  67. Plugin.remove();
  68. });
  69. }
  70. // Reload
  71. var reload = KTUtil.find(the.header, '[data-card-tool=reload]');
  72. if (reload) {
  73. KTUtil.addEvent(reload, 'click', function(e) {
  74. e.preventDefault();
  75. Plugin.reload();
  76. });
  77. }
  78. // Toggle
  79. var toggle = KTUtil.find(the.header, '[data-card-tool=toggle]');
  80. if (toggle) {
  81. KTUtil.addEvent(toggle, 'click', function(e) {
  82. e.preventDefault();
  83. Plugin.toggle();
  84. });
  85. }
  86. },
  87. /**
  88. * Enable stickt mode
  89. */
  90. initSticky: function() {
  91. var lastScrollTop = 0;
  92. var offset = the.options.sticky.offset;
  93. if (!the.header) {
  94. return;
  95. }
  96. window.addEventListener('scroll', Plugin.onScrollSticky);
  97. },
  98. /**
  99. * Window scroll handle event for sticky card
  100. */
  101. onScrollSticky: function(e) {
  102. var offset = the.options.sticky.offset;
  103. if(isNaN(offset)) return;
  104. var st = KTUtil.getScrollTop();
  105. if (st >= offset && KTUtil.hasClass(body, 'card-sticky-on') === false) {
  106. Plugin.eventTrigger('stickyOn');
  107. KTUtil.addClass(body, 'card-sticky-on');
  108. Plugin.updateSticky();
  109. } else if ((st*1.5) <= offset && KTUtil.hasClass(body, 'card-sticky-on')) {
  110. // Back scroll mode
  111. Plugin.eventTrigger('stickyOff');
  112. KTUtil.removeClass(body, 'card-sticky-on');
  113. Plugin.resetSticky();
  114. }
  115. },
  116. updateSticky: function() {
  117. if (!the.header) {
  118. return;
  119. }
  120. var top;
  121. if (KTUtil.hasClass(body, 'card-sticky-on')) {
  122. if (the.options.sticky.position.top instanceof Function) {
  123. top = parseInt(the.options.sticky.position.top.call(this, the));
  124. } else {
  125. top = parseInt(the.options.sticky.position.top);
  126. }
  127. var left;
  128. if (the.options.sticky.position.left instanceof Function) {
  129. left = parseInt(the.options.sticky.position.left.call(this, the));
  130. } else {
  131. left = parseInt(the.options.sticky.position.left);
  132. }
  133. var right;
  134. if (the.options.sticky.position.right instanceof Function) {
  135. right = parseInt(the.options.sticky.position.right.call(this, the));
  136. } else {
  137. right = parseInt(the.options.sticky.position.right);
  138. }
  139. KTUtil.css(the.header, 'z-index', the.options.sticky.zIndex);
  140. KTUtil.css(the.header, 'top', top + 'px');
  141. KTUtil.css(the.header, 'left', left + 'px');
  142. KTUtil.css(the.header, 'right', right + 'px');
  143. }
  144. },
  145. resetSticky: function() {
  146. if (!the.header) {
  147. return;
  148. }
  149. if (KTUtil.hasClass(body, 'card-sticky-on') === false) {
  150. KTUtil.css(the.header, 'z-index', '');
  151. KTUtil.css(the.header, 'top', '');
  152. KTUtil.css(the.header, 'left', '');
  153. KTUtil.css(the.header, 'right', '');
  154. }
  155. },
  156. /**
  157. * Remove card
  158. */
  159. remove: function() {
  160. if (Plugin.eventTrigger('beforeRemove') === false) {
  161. return;
  162. }
  163. KTUtil.remove(element);
  164. Plugin.eventTrigger('afterRemove');
  165. },
  166. /**
  167. * Set content
  168. */
  169. setContent: function(html) {
  170. if (html) {
  171. the.body.innerHTML = html;
  172. }
  173. },
  174. /**
  175. * Get body
  176. */
  177. getBody: function() {
  178. return the.body;
  179. },
  180. /**
  181. * Get self
  182. */
  183. getSelf: function() {
  184. return element;
  185. },
  186. /**
  187. * Reload
  188. */
  189. reload: function() {
  190. Plugin.eventTrigger('reload');
  191. },
  192. /**
  193. * Toggle
  194. */
  195. toggle: function() {
  196. if (KTUtil.hasClass(element, 'card-collapse') || KTUtil.hasClass(element, 'card-collapsed')) {
  197. Plugin.expand();
  198. } else {
  199. Plugin.collapse();
  200. }
  201. },
  202. /**
  203. * Collapse
  204. */
  205. collapse: function() {
  206. if (Plugin.eventTrigger('beforeCollapse') === false) {
  207. return;
  208. }
  209. KTUtil.slideUp(the.body, the.options.toggleSpeed, function() {
  210. Plugin.eventTrigger('afterCollapse');
  211. });
  212. KTUtil.addClass(element, 'card-collapse');
  213. },
  214. /**
  215. * Expand
  216. */
  217. expand: function() {
  218. if (Plugin.eventTrigger('beforeExpand') === false) {
  219. return;
  220. }
  221. KTUtil.slideDown(the.body, the.options.toggleSpeed, function() {
  222. Plugin.eventTrigger('afterExpand');
  223. });
  224. KTUtil.removeClass(element, 'card-collapse');
  225. KTUtil.removeClass(element, 'card-collapsed');
  226. },
  227. /**
  228. * Trigger events
  229. */
  230. eventTrigger: function(name) {
  231. //KTUtil.triggerCustomEvent(name);
  232. for (var i = 0; i < the.events.length; i++) {
  233. var event = the.events[i];
  234. if (event.name == name) {
  235. if (event.one == true) {
  236. if (event.fired == false) {
  237. the.events[i].fired = true;
  238. return event.handler.call(this, the);
  239. }
  240. } else {
  241. return event.handler.call(this, the);
  242. }
  243. }
  244. }
  245. },
  246. addEvent: function(name, handler, one) {
  247. the.events.push({
  248. name: name,
  249. handler: handler,
  250. one: one,
  251. fired: false
  252. });
  253. return the;
  254. }
  255. };
  256. //////////////////////////
  257. // ** Public Methods ** //
  258. //////////////////////////
  259. /**
  260. * Set default options
  261. */
  262. the.setDefaults = function(options) {
  263. defaultOptions = options;
  264. };
  265. /**
  266. * Remove card
  267. */
  268. the.remove = function() {
  269. return Plugin.remove(html);
  270. };
  271. /**
  272. * Init sticky card
  273. */
  274. the.initSticky = function() {
  275. return Plugin.initSticky();
  276. };
  277. /**
  278. * Rerender sticky layout
  279. */
  280. the.updateSticky = function() {
  281. return Plugin.updateSticky();
  282. };
  283. /**
  284. * Reset the sticky
  285. */
  286. the.resetSticky = function() {
  287. return Plugin.resetSticky();
  288. };
  289. /**
  290. * Destroy sticky card
  291. */
  292. the.destroySticky = function() {
  293. Plugin.resetSticky();
  294. window.removeEventListener('scroll', Plugin.onScrollSticky);
  295. };
  296. /**
  297. * Reload card
  298. */
  299. the.reload = function() {
  300. return Plugin.reload();
  301. };
  302. /**
  303. * Set card content
  304. */
  305. the.setContent = function(html) {
  306. return Plugin.setContent(html);
  307. };
  308. /**
  309. * Toggle card
  310. */
  311. the.toggle = function() {
  312. return Plugin.toggle();
  313. };
  314. /**
  315. * Collapse card
  316. */
  317. the.collapse = function() {
  318. return Plugin.collapse();
  319. };
  320. /**
  321. * Expand card
  322. */
  323. the.expand = function() {
  324. return Plugin.expand();
  325. };
  326. /**
  327. * Get cardbody
  328. * @returns {jQuery}
  329. */
  330. the.getBody = function() {
  331. return Plugin.getBody();
  332. };
  333. /**
  334. * Get cardbody
  335. * @returns {jQuery}
  336. */
  337. the.getSelf = function() {
  338. return Plugin.getSelf();
  339. };
  340. /**
  341. * Attach event
  342. */
  343. the.on = function(name, handler) {
  344. return Plugin.addEvent(name, handler);
  345. };
  346. /**
  347. * Attach event that will be fired once
  348. */
  349. the.one = function(name, handler) {
  350. return Plugin.addEvent(name, handler, true);
  351. };
  352. // Construct plugin
  353. Plugin.construct.apply(the, [options]);
  354. return the;
  355. };
  356. // webpack support
  357. if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
  358. module.exports = KTCard;
  359. }
  360. export default KTCard;