Guest User

Untitled

a guest
Dec 14th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. (function(ajax) {
  2. 'use strict';
  3. const ON_TEXT = 'On';
  4. const OFF_TEXT = 'Off';
  5. const LOADING_TEXT = 'loading';
  6. const RESOURCE_URL = '/ze-api';
  7. class Toggle {
  8. constructor(wrapper) {
  9. this.handleToggle = this.handleToggle.bind(this);
  10. this.handleSuccess = this.handleSuccess.bind(this);
  11. this.handleFailure = this.handleFailure.bind(this);
  12. this.receiveDefaultValue = this.receiveDefaultValue.bind(this);
  13. this.wrapper = wrapper;
  14. this.onText = this.wrapper.getAttribute('data-toggle-on') || ON_TEXT;
  15. this.offText = this.wrapper.getAttribute('data-toggle-off') || OFF_TEXT;
  16. this.isActive = false;
  17. this.toggle = document.createElement('a');
  18. this.toggle.className = this.toggle.className += ' toggle-trigger loading';
  19. this.toggle.href = '#';
  20. this.toggle.text = LOADING_TEXT;
  21. this.wrapper.appendChild(this.toggle);
  22. this.getDefaultValue();
  23. }
  24. getDefaultValue() {
  25. ajax({
  26. method: 'get',
  27. url: RESOURCE_URL,
  28. data: {
  29. id: this.wrapper.getAttribute('data-toggle-id'),
  30. tid: 1
  31. },
  32. dataType: 'json',
  33. success: this.receiveDefaultValue,
  34. error: this.handleFailure
  35. });
  36. }
  37. receiveDefaultValue(data) {
  38. this.toggle.addEventListener('click', this.handleToggle);
  39. this.isActive = (data.result && data.result == true);
  40. this.toggle.text = this.isActive == true ? this.offText : this.onText;
  41. this.toggle.className = this.toggle.className.replace(' loading', '');
  42. }
  43. handleToggle(e) {
  44. this.isActive = this.isActive == true ? false : true;
  45. this.toggle.text = this.isActive == true ? this.offText : this.onText;
  46. if (this.toggle.className.indexOf('loading') < 0) {
  47. this.toggle.className = this.toggle.className += ' loading';
  48. ajax({
  49. method: 'post',
  50. url: RESOURCE_URL,
  51. data: {
  52. id: this.wrapper.getAttribute('data-toggle-id'),
  53. toggleState: this.isActive
  54. },
  55. success: this.handleSuccess,
  56. error: this.handleFailure
  57. });
  58. }
  59. e.preventDefault();
  60. }
  61. handleSuccess() {
  62. this.toggle.className = this.toggle.className.replace(' loading', '');
  63. }
  64. handleFailure() {
  65. this.toggle.className = this.toggle.className.replace(' loading', '');
  66. this.toggle.className = this.toggle.className += ' error';
  67. }
  68. }
  69. const init = function() {
  70. var toggles = document.getElementsByClassName('toggle');
  71. for (var i = 0; i < toggles.length; i ++) {
  72. var toggle = toggles.item(i);
  73. if (toggle.className.indexOf('toggle-processed') < 0) {
  74. toggle.className += toggles.item(i).className += ' toggle-processed';
  75. new Toggle(toggle);
  76. }
  77. }
  78. }
  79. window.addEventListener('load', init);
  80. })(jQuery.ajax);
Add Comment
Please, Sign In to add comment