region-selection.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  2. var RegionSelection;
  3. (function (RegionSelection) {
  4. function foreach(data, callback) {
  5. Array.prototype.forEach.call(data, callback);
  6. }
  7. var RegionSelectionBox = (function () {
  8. function RegionSelectionBox(selector) {
  9. this.regions = [];
  10. this.selector = selector;
  11. if (this.selector.data('regions') !== undefined) {
  12. this.fillRegions(this.selector.data('regions'));
  13. }
  14. this.selector.on('click', this.showBox.bind(this));
  15. }
  16. RegionSelectionBox.prototype.fillRegions = function (data) {
  17. var _this = this;
  18. foreach(eval(data), function (value) {
  19. _this.regions.push(new Region(value));
  20. });
  21. };
  22. RegionSelectionBox.prototype.showBox = function (e) {
  23. e.preventDefault();
  24. if (this.box === undefined) {
  25. this.renderBox();
  26. }
  27. this.box.toggle();
  28. };
  29. RegionSelectionBox.prototype.renderBox = function () {
  30. var box = $('<div class="region__selection-box"></div>');
  31. var itemTemplate = '<a href="{link}" class="region__selection-item">{name}</a>';
  32. foreach(this.regions, function (region) {
  33. var item = $(itemTemplate
  34. .replace('{link}', region.link)
  35. .replace('{name}', region.caption));
  36. box.append(item);
  37. });
  38. box.css('display', 'none');
  39. this.selector.parent().append(box);
  40. this.box = box;
  41. };
  42. return RegionSelectionBox;
  43. }());
  44. var Region = (function () {
  45. function Region(data) {
  46. this._id = data.id;
  47. this._caption = data.caption;
  48. this._link = data.link;
  49. this._icon = data.icon;
  50. }
  51. Object.defineProperty(Region.prototype, "id", {
  52. get: function () {
  53. return this._id;
  54. },
  55. enumerable: true,
  56. configurable: true
  57. });
  58. Object.defineProperty(Region.prototype, "caption", {
  59. get: function () {
  60. return this._caption;
  61. },
  62. enumerable: true,
  63. configurable: true
  64. });
  65. Object.defineProperty(Region.prototype, "link", {
  66. get: function () {
  67. return this._link;
  68. },
  69. enumerable: true,
  70. configurable: true
  71. });
  72. Object.defineProperty(Region.prototype, "icon", {
  73. get: function () {
  74. return this._icon;
  75. },
  76. enumerable: true,
  77. configurable: true
  78. });
  79. return Region;
  80. }());
  81. function createSelectionBox($object) {
  82. return new RegionSelectionBox($object);
  83. }
  84. RegionSelection.createSelectionBox = createSelectionBox;
  85. })(RegionSelection || (RegionSelection = {}));