Dcat.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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:loaded', clear);
  94. $document.one('init:off', clear);
  95. clear();
  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. offInit() {
  112. $(document).trigger('init:off')
  113. }
  114. /**
  115. * 主动触发 ready 事件
  116. */
  117. triggerReady() {
  118. if (! pjaxResponded) {
  119. return;
  120. }
  121. $(() => {
  122. $document.trigger('pjax:loaded');
  123. });
  124. }
  125. /**
  126. * 如果是 pjax 响应的页面,需要调用此方法
  127. *
  128. * @returns {Dcat}
  129. */
  130. pjaxResponded(value) {
  131. pjaxResponded = value !== false;
  132. $document.trigger('pjax:responded');
  133. return this
  134. }
  135. /**
  136. * 使用pjax重载页面
  137. *
  138. * @param url
  139. */
  140. reload(url) {
  141. let container = this.config.pjax_container_selector,
  142. opt = {container: container};
  143. if ($(container).length) {
  144. url && (opt.url = url);
  145. $.pjax.reload(opt);
  146. return;
  147. }
  148. if (url) {
  149. location.href = url;
  150. } else {
  151. location.reload();
  152. }
  153. }
  154. /**
  155. * 监听pjax加载js脚本完毕事件方法,此事件在 pjax:complete 事件之后触发
  156. *
  157. * @param callback
  158. * @param once 默认true
  159. *
  160. * @returns {*|jQuery}
  161. */
  162. onPjaxLoaded(callback, once) {
  163. once = once === undefined ? true : once;
  164. if (once) {
  165. return $document.one('pjax:loaded', callback);
  166. }
  167. return $document.on('pjax:loaded', callback);
  168. }
  169. /**
  170. * 监听pjax加载完毕完毕事件方法
  171. *
  172. * @param callback
  173. * @param once 默认true
  174. * @returns {*|jQuery}
  175. */
  176. onPjaxComplete(callback, once) {
  177. once = once === undefined ? true : once;
  178. if (once) {
  179. return $document.one('pjax:complete', callback);
  180. }
  181. return $document.on('pjax:complete', callback);
  182. }
  183. withConfig(config) {
  184. this.config = $.extend(defaultOptions, config);
  185. this.withLang(config.lang);
  186. this.withToken(config.token);
  187. delete config.lang;
  188. delete config.token;
  189. return this
  190. }
  191. withToken(token) {
  192. token && (this.token = token);
  193. return this
  194. }
  195. withLang(lang) {
  196. if (lang && typeof lang === 'object') {
  197. this.lang = this.Translator(lang);
  198. }
  199. return this
  200. }
  201. // 语言包
  202. Translator(lang) {
  203. return new Translator(this, lang);
  204. }
  205. // 注册动作
  206. addAction(name, callback) {
  207. if (typeof callback === 'function') {
  208. actions[name] = callback;
  209. }
  210. }
  211. // 获取动作
  212. actions() {
  213. return actions
  214. }
  215. }