Dcat.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. initialized = {},
  9. defaultOptions = {
  10. pjax_container_selector: '#pjax-container',
  11. };
  12. export default class Dcat {
  13. constructor(config) {
  14. this.token = null;
  15. this.lang = null;
  16. // 工具函数
  17. new Helpers(this);
  18. this.withConfig(config);
  19. }
  20. /**
  21. * 初始化事件监听方法
  22. *
  23. * @param callback
  24. * @param once
  25. * @returns {Dcat}
  26. */
  27. booting(callback, once) {
  28. once = once === undefined ? true : once;
  29. bootingCallbacks.push([callback, once]);
  30. return this
  31. }
  32. /**
  33. * 初始化事件监听方法,每个请求都会触发
  34. *
  35. * @param callback
  36. * @returns {Dcat}
  37. */
  38. bootingEveryRequest(callback) {
  39. return this.booting(callback, false)
  40. }
  41. /**
  42. * 初始化
  43. */
  44. boot() {
  45. let _this = this,
  46. callbacks = bootingCallbacks;
  47. bootingCallbacks = [];
  48. callbacks.forEach(data => {
  49. data[0](this);
  50. if (data[1] === false) {
  51. bootingCallbacks.push(data)
  52. }
  53. });
  54. // 脚本加载完毕后重新触发
  55. _this.onPjaxLoaded(_this.boot.bind(this))
  56. }
  57. /**
  58. * 监听所有js脚本加载完毕事件,需要用此方法代替 $.ready 方法
  59. * 此方法允许在iframe中监听父窗口的事件
  60. *
  61. * @param callback
  62. * @param _window
  63. * @returns {*|jQuery|*|jQuery.fn.init|jQuery|HTMLElement}
  64. */
  65. ready(callback, _window) {
  66. let _this = this;
  67. if (! _window || _window === window) {
  68. if (! pjaxResponded) {
  69. return $(callback);
  70. }
  71. return _this.onPjaxLoaded(callback);
  72. }
  73. function run(e) {
  74. _window.$(_this.config.pjax_container_selector).one('pjax:loaded', run);
  75. callback(e);
  76. }
  77. _window.Dcat.ready(run);
  78. }
  79. /**
  80. * 监听动态生成元素.
  81. *
  82. * @param selector
  83. * @param callback
  84. * @param options
  85. */
  86. init(selector, callback, options) {
  87. let self = this,
  88. clear = function () {
  89. if (initialized[selector]) {
  90. initialized[selector].disconnect();
  91. }
  92. };
  93. $document.one('pjax:complete', clear);
  94. clear();
  95. setTimeout(function () {
  96. initialized[selector] = $.initialize(selector, function () {
  97. let $this = $(this),
  98. id = $this.attr('id');
  99. if ($this.attr('initialized')) {
  100. return;
  101. }
  102. $this.attr('initialized', '1');
  103. // 如果没有ID,则自动生成
  104. if (! id) {
  105. id = "_"+self.helpers.random();
  106. $this.attr('id', id);
  107. }
  108. callback.call(this, $this, id)
  109. }, options);
  110. });
  111. }
  112. /**
  113. * 清理注册过的init回调.
  114. *
  115. * @param selector
  116. */
  117. offInit(selector) {
  118. if (initialized[selector]) {
  119. initialized[selector].disconnect();
  120. }
  121. $(document).trigger('init:off', selector, initialized[selector])
  122. initialized[selector] = null;
  123. }
  124. /**
  125. * 主动触发 ready 事件
  126. */
  127. triggerReady() {
  128. if (! pjaxResponded) {
  129. return;
  130. }
  131. $(() => {
  132. $document.trigger('pjax:loaded');
  133. });
  134. }
  135. /**
  136. * 如果是 pjax 响应的页面,需要调用此方法
  137. *
  138. * @returns {Dcat}
  139. */
  140. pjaxResponded(value) {
  141. pjaxResponded = value !== false;
  142. $document.trigger('pjax:responded');
  143. return this
  144. }
  145. /**
  146. * 使用pjax重载页面
  147. *
  148. * @param url
  149. */
  150. reload(url) {
  151. let container = this.config.pjax_container_selector,
  152. opt = {container: container};
  153. if ($(container).length) {
  154. url && (opt.url = url);
  155. $.pjax.reload(opt);
  156. return;
  157. }
  158. if (url) {
  159. location.href = url;
  160. } else {
  161. location.reload();
  162. }
  163. }
  164. /**
  165. * 监听pjax加载js脚本完毕事件方法,此事件在 pjax:complete 事件之后触发
  166. *
  167. * @param callback
  168. * @param once 默认true
  169. *
  170. * @returns {*|jQuery}
  171. */
  172. onPjaxLoaded(callback, once) {
  173. once = once === undefined ? true : once;
  174. if (once) {
  175. return $document.one('pjax:loaded', callback);
  176. }
  177. return $document.on('pjax:loaded', callback);
  178. }
  179. /**
  180. * 监听pjax加载完毕完毕事件方法
  181. *
  182. * @param callback
  183. * @param once 默认true
  184. * @returns {*|jQuery}
  185. */
  186. onPjaxComplete(callback, once) {
  187. once = once === undefined ? true : once;
  188. if (once) {
  189. return $document.one('pjax:complete', callback);
  190. }
  191. return $document.on('pjax:complete', callback);
  192. }
  193. withConfig(config) {
  194. this.config = $.extend(defaultOptions, config);
  195. this.withLang(config.lang);
  196. this.withToken(config.token);
  197. delete config.lang;
  198. delete config.token;
  199. return this
  200. }
  201. withToken(token) {
  202. token && (this.token = token);
  203. return this
  204. }
  205. withLang(lang) {
  206. if (lang && typeof lang === 'object') {
  207. this.lang = this.Translator(lang);
  208. }
  209. return this
  210. }
  211. // 语言包
  212. Translator(lang) {
  213. return new Translator(this, lang);
  214. }
  215. // 注册动作
  216. addAction(name, callback) {
  217. if (typeof callback === 'function') {
  218. actions[name] = callback;
  219. }
  220. }
  221. // 获取动作
  222. actions() {
  223. return actions
  224. }
  225. }