fancycheckboxes.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  2. /// <reference path="../../node_modules/@types/fancybox/index.d.ts"/>
  3. var FancyCheckboxes;
  4. (function (FancyCheckboxes) {
  5. var FancyCheckbox = (function () {
  6. function FancyCheckbox($selector) {
  7. this.events = {
  8. 'error': [],
  9. 'success': []
  10. };
  11. this.selected = {};
  12. this.$selector = $selector;
  13. this.checkboxData = [];
  14. this.populateCheckboxData(JSON.parse($selector.data('items') ? $selector.data('items').replace(/'/g, '"') : "[]"));
  15. this.$selector.click(this.onSelectorClick.bind(this));
  16. this.$selector.prev().append('<div class="selected-items"></div>');
  17. this.loadSelected();
  18. }
  19. FancyCheckbox.prototype.loadSelected = function () {
  20. var context = this;
  21. var first = true;
  22. var $selected = context.$selector.prev().find('.selected-items');
  23. this.selected = {};
  24. $selected.html('');
  25. console.dir($(this.$selector.data('field')).find(':selected'));
  26. $(this.$selector.data('field')).find(':selected').each(function () {
  27. context.selected[$(this).attr('value')] = $(this).html();
  28. if (!first) {
  29. $selected.append(', ');
  30. }
  31. if (first) {
  32. first = false;
  33. }
  34. $selected.append('<span>' + $(this).html() + '</span>');
  35. });
  36. console.dir(this.selected);
  37. };
  38. FancyCheckbox.prototype.onSelectorClick = function (event) {
  39. var context = this;
  40. var content = $('<div></div>');
  41. content.append('<div class="modal-header"><h3>' + this.$selector.html() + '</h3></div>');
  42. content.append('<div class="fancycheckboxes-items-container container">' + this.render().html() + '</div>');
  43. content.append('<div class="modal-footer"></div>');
  44. content.find('.modal-footer').append('<button type="submit" class="btn btn-primary">Выбрать</button>');
  45. var options = {
  46. content: '<div class="fancycheckboxes">' + content.html() + '</div>',
  47. type: 'html',
  48. modal: true,
  49. height: window.innerHeight,
  50. width: $('.mainBlock').outerWidth(),
  51. autoSize: false,
  52. afterClose: function () {
  53. context.loadSelected();
  54. },
  55. afterShow: function () {
  56. context.bindEvents();
  57. }
  58. };
  59. $.fancybox(options);
  60. };
  61. FancyCheckbox.prototype.bindEvents = function () {
  62. var context = this;
  63. var $fancy = $('.fancycheckboxes');
  64. $fancy.find('.more').each(function () {
  65. var $link = $(this);
  66. $link.on('click', function () {
  67. $link.parent().animate({ height: $link.parent()[0].scrollHeight }, 200, function () {
  68. $link.parent().height('auto');
  69. $link.remove();
  70. });
  71. });
  72. });
  73. $fancy.find('.btn-primary').click(function () {
  74. $(context.$selector.data('field')).find('option').removeAttr('selected');
  75. $fancy.find('input:checked').each(function () {
  76. console.dir($(context.$selector.data('field'))
  77. .find('[value=' + $(this).attr('value') + ']'));
  78. $(context.$selector.data('field'))
  79. .find('[value=' + $(this).attr('value') + ']')
  80. .prop('selected', 'selected');
  81. });
  82. $.fancybox.close();
  83. });
  84. };
  85. FancyCheckbox.prototype.checkChecked = function (id) {
  86. return this.selected.hasOwnProperty(id);
  87. };
  88. FancyCheckbox.prototype.on = function (eventName, callback) {
  89. this.events[eventName].push(callback);
  90. return this;
  91. };
  92. FancyCheckbox.prototype.trigger = function (eventName) {
  93. var args = [];
  94. for (var _i = 1; _i < arguments.length; _i++) {
  95. args[_i - 1] = arguments[_i];
  96. }
  97. var context = this;
  98. for (var i in this.events[eventName]) {
  99. if (this.events[eventName].hasOwnProperty(i)) {
  100. setTimeout(this.events[eventName][i].apply(context, args), 1);
  101. }
  102. }
  103. };
  104. FancyCheckbox.prototype.populateCheckboxData = function (checkboxData) {
  105. checkboxData.sort(function (item1, item2) {
  106. return item1.value.localeCompare(item2.value);
  107. });
  108. for (var i in checkboxData) {
  109. if (checkboxData.hasOwnProperty(i)) {
  110. if (checkboxData[i].items.length) {
  111. var group = new CheckboxGroup(checkboxData[i], this);
  112. this.checkboxData.push(group);
  113. }
  114. else {
  115. var item = new CheckboxItem(checkboxData[i], this);
  116. this.checkboxData.push(item);
  117. }
  118. }
  119. }
  120. };
  121. FancyCheckbox.prototype.render = function () {
  122. var items = $('<div></div>');
  123. this.checkboxData.forEach(function (item) {
  124. if (item instanceof CheckboxItem) {
  125. var group = $('<div class="fancycheckboxes-group"></div>');
  126. group.append(item.render());
  127. items.append(group);
  128. }
  129. else {
  130. items.append(item.render());
  131. }
  132. });
  133. items.find('.fancycheckboxes-group').each(function () {
  134. if ($(this).find('.fancycheckboxes-item').length > 5) {
  135. $(this).append($('<a class="more">Все &raquo;</a>'));
  136. $(this).height(150);
  137. }
  138. });
  139. return items;
  140. };
  141. return FancyCheckbox;
  142. }());
  143. FancyCheckboxes.FancyCheckbox = FancyCheckbox;
  144. var CheckboxGroup = (function () {
  145. function CheckboxGroup(item, widget) {
  146. this.widget = widget;
  147. this.name = item.value;
  148. this.items = [];
  149. this.populateCheckboxData(item.items);
  150. }
  151. CheckboxGroup.prototype.populateCheckboxData = function (checkboxData) {
  152. checkboxData.sort(function (item1, item2) {
  153. return item1.value.localeCompare(item2.value);
  154. });
  155. for (var i in checkboxData) {
  156. if (checkboxData.hasOwnProperty(i)) {
  157. if (checkboxData[i].items.length) {
  158. var group = new CheckboxGroup(checkboxData[i], this.widget);
  159. this.items.push(group);
  160. }
  161. else {
  162. var item = new CheckboxItem(checkboxData[i], this.widget);
  163. this.items.push(item);
  164. }
  165. }
  166. }
  167. };
  168. CheckboxGroup.prototype.render = function () {
  169. var group = $('<div class="fancycheckboxes-group"></div>');
  170. group.append($('<h4>' + this.name + '</h4>'));
  171. this.items.forEach(function (item) {
  172. group.append(item.render());
  173. });
  174. return group;
  175. };
  176. return CheckboxGroup;
  177. }());
  178. var CheckboxItem = (function () {
  179. function CheckboxItem(item, widget) {
  180. this.id = item.id;
  181. this.name = item.value;
  182. this.widget = widget;
  183. }
  184. CheckboxItem.prototype.render = function () {
  185. var item = $('<div class="fancycheckboxes-item"></div>');
  186. item.append($('<input type="checkbox" value="' + this.id + '" id="fchb_' + this.id + '" > <label for="fchb_' + this.id + '">' + this.name + '</label>'));
  187. if (this.widget.checkChecked(this.id)) {
  188. item.find('input').attr('checked', 'checked');
  189. }
  190. return item;
  191. };
  192. return CheckboxItem;
  193. }());
  194. })(FancyCheckboxes || (FancyCheckboxes = {}));