specfilter.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  2. /// <reference path="../../node_modules/@types/fancybox/index.d.ts"/>
  3. var SpecFilter;
  4. (function (SpecFilter_1) {
  5. var SpecFilter = (function () {
  6. function SpecFilter($selector) {
  7. this.selected = {};
  8. this.strings = ['специализация', 'специализации', 'специализаций'];
  9. this.typeName = 'специализации';
  10. this.$selector = $selector;
  11. this.endpointUrl = $selector.data('enpoint-url');
  12. var strings = $selector.data('strings');
  13. var typeName = $selector.data('type-name');
  14. if (strings !== undefined) {
  15. this.strings = eval(strings);
  16. }
  17. if (typeName !== undefined) {
  18. this.typeName = typeName;
  19. }
  20. $.ajax({
  21. url: this.endpointUrl
  22. }).done((function (data) {
  23. this.content = data;
  24. }).bind(this));
  25. this.$selector.click(this.onSelectorClick.bind(this));
  26. this.loadSelected();
  27. }
  28. SpecFilter.prototype.onSelectorClick = function (event) {
  29. event.preventDefault();
  30. var context = this;
  31. var options = {
  32. content: this.content,
  33. type: 'html',
  34. height: 799,
  35. width: $('.centerbar').outerWidth(),
  36. padding: 0,
  37. autoSize: false,
  38. afterClose: function () {
  39. },
  40. afterShow: function () {
  41. context.bindEvents();
  42. context.loadSelected();
  43. }
  44. };
  45. $.fancybox(options);
  46. };
  47. SpecFilter.prototype.loadSelected = function () {
  48. var $fancy = $('.specializations__listBlock--check').parent();
  49. var count = 0;
  50. $(this.$selector.data('field')).find(':selected').each(function () {
  51. count++;
  52. $fancy.find('input[value=' + $(this).val() + ']').prop('checked', 'checked');
  53. });
  54. this.setCount(count);
  55. };
  56. SpecFilter.declOfNum = function (number, titles) {
  57. var cases = [2, 0, 1, 1, 1, 2];
  58. return titles[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5]];
  59. };
  60. SpecFilter.prototype.bindEvents = function () {
  61. var context = this;
  62. var $fancy = $('.specializations__listBlock--check').parent();
  63. $fancy.find('.btn-primary').click(function () {
  64. $(context.$selector.data('field')).find('option').removeAttr('selected');
  65. var count = 0;
  66. var all = $fancy.find('input[value=all]').is(':checked');
  67. $fancy.find('.fancycheckbox__checkbox:checked').each(function () {
  68. if (all)
  69. return null;
  70. console.dir($(context.$selector.data('field'))
  71. .find('[value=' + $(this).attr('value') + ']'));
  72. $(context.$selector.data('field'))
  73. .find('[value=' + $(this).attr('value') + ']')
  74. .prop('selected', 'selected');
  75. if ($(this).attr('value') !== 'all')
  76. count++;
  77. });
  78. context.setCount(count);
  79. $(context.$selector.data('field')).trigger('change');
  80. $.fancybox.close();
  81. });
  82. $fancy.find('.fancycheckbox__selectGroup').click(function () {
  83. var checked = $(this).prop('checked');
  84. $(this).parents('.specializations__listLetter').next().find('.fancycheckbox__checkbox').each(function () {
  85. $(this).prop('checked', checked);
  86. });
  87. });
  88. $fancy.find('.fancycheckbox__checkbox').click(function () {
  89. if ($(this).parents('ul').find('.fancycheckbox__checkbox').length == $(this).parents('ul').find('.fancycheckbox__checkbox:checked').length) {
  90. $(this).parents('.specializations__listRow').find('.fancycheckbox__selectGroup').prop('checked', true);
  91. }
  92. if (!$(this).is(':checked')) {
  93. $(this).parents('.specializations__listRow').find('.fancycheckbox__selectGroup').prop('checked', false);
  94. }
  95. });
  96. $fancy.find('.specializations__listRegionMore').click(function () {
  97. var $link = $(this);
  98. var $list = $link.prev();
  99. $list.animate({'max-height': $list[0].scrollHeight}, 200, function () {
  100. $link.remove();
  101. });
  102. });
  103. };
  104. SpecFilter.prototype.checkChecked = function (id) {
  105. return this.selected.hasOwnProperty(id);
  106. };
  107. SpecFilter.prototype.setCount = function (count) {
  108. if (count === void 0) {
  109. count = 0;
  110. }
  111. this.$selector.html('Все ' + this.typeName);
  112. if (count > 0) {
  113. this.$selector.html(count + ' ' + SpecFilter.declOfNum(count, this.strings));
  114. }
  115. };
  116. return SpecFilter;
  117. }());
  118. SpecFilter_1.SpecFilter = SpecFilter;
  119. })(SpecFilter || (SpecFilter = {}));