Dcat.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import Helpers from './extensions/Helpers'
  2. import Translator from './extensions/Translator'
  3. let $ = jQuery,
  4. $document = $(document),
  5. pjaxResponded = false,
  6. bootingCallbacks = [],
  7. actions = {},
  8. defaultOptions = {
  9. pjax_container_selector: '#pjax-container',
  10. };
  11. export default class Dcat {
  12. constructor(config) {
  13. this.token = null;
  14. this.lang = null;
  15. // 工具函数
  16. new Helpers(this);
  17. this.withConfig(config);
  18. }
  19. /**
  20. * 初始化事件监听方法
  21. *
  22. * @param callback
  23. * @param once
  24. * @returns {Dcat}
  25. */
  26. booting(callback, once) {
  27. once = once === undefined ? true : once;
  28. bootingCallbacks.push([callback, once]);
  29. return this
  30. }
  31. /**
  32. * 初始化事件监听方法,每个请求都会触发
  33. *
  34. * @param callback
  35. * @returns {Dcat}
  36. */
  37. bootingEveryRequest(callback) {
  38. return this.booting(callback, false)
  39. }
  40. /**
  41. * 初始化
  42. */
  43. boot() {
  44. let _this = this,
  45. callbacks = bootingCallbacks;
  46. bootingCallbacks = [];
  47. callbacks.forEach(data => {
  48. data[0](this);
  49. if (data[1] === false) {
  50. bootingCallbacks.push(data)
  51. }
  52. });
  53. // 脚本加载完毕后重新触发
  54. _this.onPjaxLoaded(_this.boot.bind(this))
  55. }
  56. /**
  57. * 监听所有js脚本加载完毕事件,需要用此方法代替 $.ready 方法
  58. * 此方法允许在iframe中监听父窗口的事件
  59. *
  60. * @param callback
  61. * @param _window
  62. * @returns {*|jQuery|*|jQuery.fn.init|jQuery|HTMLElement}
  63. */
  64. ready(callback, _window) {
  65. let _this = this;
  66. if (! _window || _window === window) {
  67. if (! pjaxResponded) {
  68. return $(callback);
  69. }
  70. return _this.onPjaxLoaded(callback);
  71. }
  72. function run(e) {
  73. _window.$(_this.config.pjax_container_selector).one('pjax:loaded', run);
  74. callback(e);
  75. }
  76. _window.Dcat.ready(run);
  77. }
  78. /**
  79. * 主动触发 ready 事件
  80. */
  81. triggerReady() {
  82. if (! pjaxResponded) {
  83. return;
  84. }
  85. $(() => {
  86. $document.trigger('pjax:loaded');
  87. });
  88. }
  89. /**
  90. * 如果是 pjax 响应的页面,需要调用此方法
  91. *
  92. * @returns {Dcat}
  93. */
  94. pjaxResponded(value) {
  95. pjaxResponded = value !== false;
  96. return this
  97. }
  98. /**
  99. * 使用pjax重载页面
  100. *
  101. * @param url
  102. */
  103. reload(url) {
  104. let container = this.config.pjax_container_selector,
  105. opt = {container: container};
  106. if ($(container).length) {
  107. url && (opt.url = url);
  108. $.pjax.reload(opt);
  109. return;
  110. }
  111. if (url) {
  112. location.href = url;
  113. } else {
  114. location.reload();
  115. }
  116. }
  117. /**
  118. * 监听pjax加载js脚本完毕事件方法,此事件在 pjax:complete 事件之后触发
  119. *
  120. * @param callback
  121. * @param once 默认true
  122. *
  123. * @returns {*|jQuery}
  124. */
  125. onPjaxLoaded(callback, once) {
  126. once = once === undefined ? true : once;
  127. if (once) {
  128. return $document.one('pjax:loaded', callback);
  129. }
  130. return $document.on('pjax:loaded', callback);
  131. }
  132. /**
  133. * 监听pjax加载完毕完毕事件方法
  134. *
  135. * @param callback
  136. * @param once 默认true
  137. * @returns {*|jQuery}
  138. */
  139. onPjaxComplete(callback, once) {
  140. once = once === undefined ? true : once;
  141. if (once) {
  142. return $document.one('pjax:complete', callback);
  143. }
  144. return $document.on('pjax:complete', callback);
  145. }
  146. withConfig(config) {
  147. this.config = $.extend(defaultOptions, config);
  148. this.withLang(config.lang);
  149. this.withToken(config.token);
  150. delete config.lang;
  151. delete config.token;
  152. return this
  153. }
  154. withToken(token) {
  155. token && (this.token = token);
  156. return this
  157. }
  158. withLang(lang) {
  159. if (lang && typeof lang === 'object') {
  160. this.lang = this.Translator(lang);
  161. }
  162. return this
  163. }
  164. // 语言包
  165. Translator(lang) {
  166. return new Translator(this, lang);
  167. }
  168. // 注册动作
  169. addAction(name, callback) {
  170. if (typeof callback === 'function') {
  171. actions[name] = callback;
  172. }
  173. }
  174. // 获取动作
  175. actions() {
  176. return actions
  177. }
  178. }