specfilter.ts 5.1 KB

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