dcat.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. let $ = jQuery,
  2. pjaxResponded = false,
  3. bootingCallbacks = [],
  4. formCallbacks = {
  5. before: [], success: [], error: []
  6. };
  7. export default class Dcat {
  8. constructor(config) {
  9. this.withConfig(config);
  10. }
  11. booting(callback) {
  12. bootingCallbacks.push(callback)
  13. return this
  14. }
  15. boot() {
  16. bootingCallbacks.forEach(callback => callback(Vue, router, store));
  17. bootingCallbacks = []
  18. }
  19. liftOff() {
  20. this.boot()
  21. }
  22. ready(callback, _window) {
  23. if (! _window || _window === window) {
  24. if (! pjaxResponded) {
  25. return $(callback);
  26. }
  27. return $(document).one('pjax:done', callback);
  28. }
  29. var proxy = function (e) {
  30. _window.$(_window.$('#pjax-container')).one('pjax:done', proxy);
  31. callback(e);
  32. };
  33. _window.Dcat.ready(proxy);
  34. }
  35. withConfig(config) {
  36. this.config = config;
  37. this.withLang(config['lang']);
  38. this.withToken(config['token']);
  39. return this
  40. }
  41. withToken(token) {
  42. token && (this.token = token);
  43. return this
  44. }
  45. withLang(lang) {
  46. lang && (this.lang = lang);
  47. return this
  48. }
  49. pjaxResponded() {
  50. pjaxResponded = true;
  51. return this
  52. }
  53. submiting(callback) {
  54. typeof callback == 'function' && (formCallbacks.before.push(callback));
  55. return this
  56. }
  57. submitted(success, error) {
  58. typeof success == 'function' && (formCallbacks.success.push(success));
  59. typeof error == 'function' && (formCallbacks.error.push(error));
  60. return this
  61. }
  62. }