Dcat.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. if (initialized[selector]) {
  88. initialized[selector].takeRecords();
  89. initialized[selector].disconnect();
  90. }
  91. let self = this;
  92. // 这里必须使用定时器,否则无法立即停止上次绑定的观察回调
  93. setTimeout(function () {
  94. initialized[selector] = $.initialize(selector, function () {
  95. var $this = $(this);
  96. if ($this.hasClass('initialized')) {
  97. return;
  98. }
  99. $this.addClass('initialized');
  100. // 如果没有ID,则自动生成
  101. var id = $this.attr('id');
  102. if (! id) {
  103. id = "_"+self.helpers.random();
  104. $this.attr('id', id);
  105. }
  106. callback.call(this, $(this), id)
  107. }, options);
  108. }, 1)
  109. }
  110. /**
  111. * 主动触发 ready 事件
  112. */
  113. triggerReady() {
  114. if (! pjaxResponded) {
  115. return;
  116. }
  117. $(() => {
  118. $document.trigger('pjax:loaded');
  119. });
  120. }
  121. /**
  122. * 如果是 pjax 响应的页面,需要调用此方法
  123. *
  124. * @returns {Dcat}
  125. */
  126. pjaxResponded(value) {
  127. pjaxResponded = value !== false;
  128. return this
  129. }
  130. /**
  131. * 使用pjax重载页面
  132. *
  133. * @param url
  134. */
  135. reload(url) {
  136. let container = this.config.pjax_container_selector,
  137. opt = {container: container};
  138. if ($(container).length) {
  139. url && (opt.url = url);
  140. $.pjax.reload(opt);
  141. return;
  142. }
  143. if (url) {
  144. location.href = url;
  145. } else {
  146. location.reload();
  147. }
  148. }
  149. /**
  150. * 监听pjax加载js脚本完毕事件方法,此事件在 pjax:complete 事件之后触发
  151. *
  152. * @param callback
  153. * @param once 默认true
  154. *
  155. * @returns {*|jQuery}
  156. */
  157. onPjaxLoaded(callback, once) {
  158. once = once === undefined ? true : once;
  159. if (once) {
  160. return $document.one('pjax:loaded', callback);
  161. }
  162. return $document.on('pjax:loaded', callback);
  163. }
  164. /**
  165. * 监听pjax加载完毕完毕事件方法
  166. *
  167. * @param callback
  168. * @param once 默认true
  169. * @returns {*|jQuery}
  170. */
  171. onPjaxComplete(callback, once) {
  172. once = once === undefined ? true : once;
  173. if (once) {
  174. return $document.one('pjax:complete', callback);
  175. }
  176. return $document.on('pjax:complete', callback);
  177. }
  178. withConfig(config) {
  179. this.config = $.extend(defaultOptions, config);
  180. this.withLang(config.lang);
  181. this.withToken(config.token);
  182. delete config.lang;
  183. delete config.token;
  184. return this
  185. }
  186. withToken(token) {
  187. token && (this.token = token);
  188. return this
  189. }
  190. withLang(lang) {
  191. if (lang && typeof lang === 'object') {
  192. this.lang = this.Translator(lang);
  193. }
  194. return this
  195. }
  196. // 语言包
  197. Translator(lang) {
  198. return new Translator(this, lang);
  199. }
  200. // 注册动作
  201. addAction(name, callback) {
  202. if (typeof callback === 'function') {
  203. actions[name] = callback;
  204. }
  205. }
  206. // 获取动作
  207. actions() {
  208. return actions
  209. }
  210. }