Guest User

Untitled

a guest
Jun 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. $.widget('grom.dynamicList', {
  2. options: {
  3. addClass: '.js-dl-add',
  4. addBefore: '.js-dl-add-before',
  5. removeClass: '.js-dl-remove',
  6. itemClass: '.js-dl-item',
  7. popupClass: '.js-dl-popup',
  8. popupYesClass: '.js-dl-popup-yes',
  9. popupCloseClass: '.js-dl-popup-close'
  10. },
  11. _create: function () {
  12. this._initProps();
  13. this._intiObservers();
  14. this._render();
  15. },
  16. _initProps: function () {
  17. this.currentRemoveItem = $();
  18. this.$popup = $(this.options.popupClass);
  19. },
  20. _intiObservers: function () {
  21. this.element.find(this.options.addClass).click(this._onAddItem.bind(this));
  22.  
  23. this.element.on('click', this.options.removeClass, this._onRemoveItem.bind(this));
  24.  
  25. var yesBtn = $(this.options.popupClass).find(this.options.popupYesClass);
  26. yesBtn.on('click', this._onPopupYes.bind(this));
  27. yesBtn.on('touchstart', this._onPopupYes.bind(this));
  28.  
  29. var closePopup = $(this.options.popupCloseClass);
  30. closePopup.on('click', this._hidePopup.bind(this));
  31. closePopup.on('touchstart', this._hidePopup.bind(this));
  32. },
  33. _onAddItem: function () {
  34. var itemHtml = $.parseHTML(this.options.itemString);
  35.  
  36. if (typeof this.options.onItemCreate === 'function') {
  37. this.options.onItemCreate(itemHtml);
  38. }
  39.  
  40. this.element.find(this.options.addBefore).before(itemHtml);
  41.  
  42. this._render();
  43. },
  44. _onPopupYes: function () {
  45. this.currentRemoveItem.remove();
  46. this._render();
  47. },
  48. _onRemoveItem: function (event) {
  49. this.currentRemoveItem = $(event.target).parents(this.options.itemClass);
  50. this._showPopup();
  51. },
  52. _showPopup: function () {
  53. this.$popup.fadeIn();
  54. },
  55. _hidePopup: function () {
  56. this.$popup.fadeOut();
  57. },
  58. _render: function () {
  59. var items = this.element.find(this.options.itemClass);
  60.  
  61. if (items.size() > 1) {
  62. items.find(this.options.removeClass).show();
  63. return;
  64. }
  65.  
  66. items.find(this.options.removeClass).hide();
  67. }
  68. });
  69.  
  70. $(function () {
  71. $('.js-cuisines-list').dynamicList({
  72. itemString: '<div class="flex-box selectbox-row js-dl-item">' +
  73. '<div class="selectbox-theme2 flex-item">' +
  74. '<select class="form-control js-form-control">' +
  75. '<option>European</option>' +
  76. '<option>Asian</option>' +
  77. '<option>American</option>' +
  78. '</select>' +
  79. '</div>' +
  80. '<div class="selectbox-theme2 flex-item">' +
  81. '<select class="form-control js-form-control">' +
  82. '<option>Italian</option>' +
  83. '<option>Argentine</option>' +
  84. '<option>Portuguese</option>' +
  85. '</select>' +
  86. '</div>' +
  87. '<div class="selectbox-theme2 flex-item">' +
  88. '<select class="form-control js-form-control">' +
  89. '<option>Sicilian</option>' +
  90. '<option>Sicilian</option>' +
  91. '<option>Sicilian</option>' +
  92. '</select>' +
  93. '</div>' +
  94. '<div class="flex-item flex-item-remove js-dl-remove">' +
  95. '<a href="javascript:;" class="form-control-icon bg-icon close-btn-small">' +
  96. '</a>' +
  97. '</div>' +
  98. '</div>',
  99. onItemCreate: function (html) {
  100. $(html).find('.js-form-control').select2({
  101. minimumResultsForSearch: Infinity
  102. });
  103. }
  104. });
  105.  
  106. $('.js-location-list').dynamicList({
  107. itemString: '<div class="form-group row js-dl-item">' +
  108. '<div class="col-sm-4">' +
  109. '<label class="form-control-label">Other location</label>' +
  110. '</div>' +
  111. '<div class="col-sm-8">' +
  112. '<div class="form-control-wrap has-close-control">' +
  113. '<input id="step1" type="text" class="form-control" value="">' +
  114. '<a href="javascript:;" class="form-control-icon bg-icon close-btn-small js-dl-remove">' +
  115. '</a>' +
  116. '</div>' +
  117. '</div>' +
  118. '</div>',
  119. onItemCreate: function (html) {
  120. $(html).find('.js-form-control').select2({
  121. minimumResultsForSearch: Infinity
  122. });
  123. }
  124. });
  125.  
  126. $('.js-dishes-list').dynamicList();
  127. $('.js-menus-list').dynamicList();
  128. $('.js-menu-creation-list').dynamicList();
  129. $('.js-recipes-list').dynamicList();
  130. });
Add Comment
Please, Sign In to add comment