regionfilter.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  2. namespace RegionFilter {
  3. export class RegionFilter {
  4. private $selector: JQuery;
  5. private selected: {} = {};
  6. private strings = ['район', 'района', 'районов'];
  7. private typeName = 'районы';
  8. private config: FilterConfigInterface;
  9. public constructor($selector: JQuery, config: FilterConfigInterface) {
  10. this.$selector = $selector;
  11. let strings = $selector.data('strings');
  12. let typeName = $selector.data('type-name');
  13. if (strings !== undefined) {
  14. this.strings = eval(strings);
  15. }
  16. if (typeName !== undefined) {
  17. this.typeName = typeName;
  18. }
  19. this.config = config;
  20. this.$selector.click(this.onSelectorClick.bind(this));
  21. this.loadSelected();
  22. }
  23. private onSelectorClick(event) {
  24. event.preventDefault();
  25. let context = this;
  26. let itemsContainer = this.$selector.parent().find('.' + this.config.itemsContainerClass);
  27. if (itemsContainer.length) {
  28. itemsContainer.fadeOut(200, function () {
  29. $(this).remove();
  30. });
  31. } else {
  32. this.$selector.parent().append(this.createContainer());
  33. }
  34. }
  35. private createContainer(): JQuery {
  36. let itemsContainer = $(`<div class="${this.config.itemsContainerClass}"></div>`);
  37. let context = this;
  38. let field = $(this.$selector.data('field'));
  39. field.find('option').each(function () {
  40. let id = $(this).attr('value');
  41. itemsContainer.append(
  42. // create container
  43. $(`<div class="${context.config.itemContainerClass}"></div>`)
  44. // create item
  45. .append($(`<input type="checkbox" class="${context.config.itemCheckboxClass}" id="rfchb${id}" value="${id}">`)
  46. // select items
  47. .attr('checked', $(this).prop('selected'))
  48. // bind change event
  49. .on('change', function () {
  50. field.find(`option[value=${id}]`).prop('selected', $(this).prop('checked'));
  51. context.setCount(field.find('option:selected').length);
  52. }))
  53. // create label
  54. .append($(`<label class="${context.config.itemLabelClass}" for="rfchb${id}">${$(this).html()}</label>`)));
  55. });
  56. return itemsContainer;
  57. }
  58. private loadSelected() {
  59. }
  60. private static declOfNum(number, titles) {
  61. let cases = [2, 0, 1, 1, 1, 2];
  62. return titles[(number % 100 > 4 && number % 100 < 20) ? 2 : cases[(number % 10 < 5) ? number % 10 : 5]];
  63. }
  64. public checkChecked(id) {
  65. return this.selected.hasOwnProperty(id);
  66. }
  67. public setCount(count: number = 0) {
  68. this.$selector.html('Все ' + this.typeName);
  69. if (count > 0) {
  70. this.$selector.html(count + ' ' + RegionFilter.declOfNum(count, this.strings));
  71. }
  72. }
  73. }
  74. interface FilterConfigInterface {
  75. itemsContainerClass: string;
  76. itemCheckboxClass: string;
  77. itemContainerClass: string;
  78. itemLabelClass: string;
  79. }
  80. }