regionfilter.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  2. var RegionFilter;
  3. (function (RegionFilter_1) {
  4. var RegionFilter = (function () {
  5. function RegionFilter($selector, config) {
  6. this.selected = {};
  7. this.strings = ['район', 'района', 'районов'];
  8. this.typeName = 'районы';
  9. this.$selector = $selector;
  10. var strings = $selector.data('strings');
  11. var typeName = $selector.data('type-name');
  12. if (strings !== undefined) {
  13. this.strings = eval(strings);
  14. }
  15. if (typeName !== undefined) {
  16. this.typeName = typeName;
  17. }
  18. this.config = config;
  19. this.$selector.click(this.onSelectorClick.bind(this));
  20. this.loadSelected();
  21. }
  22. RegionFilter.prototype.onSelectorClick = function (event) {
  23. event.preventDefault();
  24. var context = this;
  25. var itemsContainer = this.$selector.parent().find('.' + this.config.itemsContainerClass);
  26. if (itemsContainer.length) {
  27. itemsContainer.fadeOut(200, function () {
  28. $(this).remove();
  29. });
  30. }
  31. else {
  32. this.$selector.parent().append(this.createContainer());
  33. }
  34. };
  35. RegionFilter.prototype.createContainer = function () {
  36. var itemsContainer = $("<div class=\"" + this.config.itemsContainerClass + "\"></div>");
  37. var context = this;
  38. var field = $(this.$selector.data('field'));
  39. field.find('option').each(function () {
  40. var id = $(this).attr('value');
  41. itemsContainer.append(
  42. // create container
  43. $("<div class=\"" + context.config.itemContainerClass + "\"></div>")
  44. .append($("<input type=\"checkbox\" class=\"" + context.config.itemCheckboxClass + "\" id=\"rfchb" + id + "\" value=\"" + id + "\">")
  45. .attr('checked', $(this).prop('selected'))
  46. .on('change', function () {
  47. field.find("option[value=" + id + "]").prop('selected', $(this).prop('checked'));
  48. context.setCount(field.find('option:selected').length);
  49. }))
  50. .append($("<label class=\"" + context.config.itemLabelClass + "\" for=\"rfchb" + id + "\">" + $(this).html() + "</label>")));
  51. });
  52. return itemsContainer;
  53. };
  54. RegionFilter.prototype.loadSelected = function () {
  55. };
  56. RegionFilter.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. RegionFilter.prototype.checkChecked = function (id) {
  61. return this.selected.hasOwnProperty(id);
  62. };
  63. RegionFilter.prototype.setCount = function (count) {
  64. if (count === void 0) { count = 0; }
  65. this.$selector.html('Все ' + this.typeName);
  66. if (count > 0) {
  67. this.$selector.html(count + ' ' + RegionFilter.declOfNum(count, this.strings));
  68. }
  69. };
  70. return RegionFilter;
  71. }());
  72. RegionFilter_1.RegionFilter = RegionFilter;
  73. })(RegionFilter || (RegionFilter = {}));