region-selection.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. e.stopPropagation();
  25. if (this.box === undefined) {
  26. this.renderBox();
  27. }
  28. var box = this.box;
  29. var hide = function () {
  30. box.hide(0, function () {
  31. $('body').off('click', hide);
  32. });
  33. }.bind(this);
  34. if (box.is(':visible')) {
  35. hide();
  36. }
  37. else {
  38. $('body').on('click', hide);
  39. box.show(0);
  40. }
  41. };
  42. RegionSelectionBox.prototype.renderBox = function () {
  43. var _this = this;
  44. if (!$('.region__selection-box').length) {
  45. this.box = $('<div class="region__selection-box region-selection__box"></div>');
  46. var itemTemplate_1 = '<a href="{link}" class="region-selection__link">{icon} {name}</a>';
  47. var iconTemplate_1 = '<img src="{icon}" class="region-selection__icon">';
  48. foreach(this.regions, function (region) {
  49. var item = $(itemTemplate_1
  50. .replace('{link}', region.link)
  51. .replace('{name}', region.caption)
  52. .replace('{icon}', iconTemplate_1.replace('{icon}', region.icon)));
  53. _this.box.append(item);
  54. });
  55. this.box.css('display', 'none');
  56. this.selector.parent().append(this.box);
  57. }
  58. };
  59. return RegionSelectionBox;
  60. }());
  61. var Region = (function () {
  62. function Region(data) {
  63. this._id = data.id;
  64. this._caption = data.caption;
  65. this._link = data.link;
  66. this._icon = data.icon;
  67. }
  68. Object.defineProperty(Region.prototype, "id", {
  69. get: function () {
  70. return this._id;
  71. },
  72. enumerable: true,
  73. configurable: true
  74. });
  75. Object.defineProperty(Region.prototype, "caption", {
  76. get: function () {
  77. return this._caption;
  78. },
  79. enumerable: true,
  80. configurable: true
  81. });
  82. Object.defineProperty(Region.prototype, "link", {
  83. get: function () {
  84. return this._link;
  85. },
  86. enumerable: true,
  87. configurable: true
  88. });
  89. Object.defineProperty(Region.prototype, "icon", {
  90. get: function () {
  91. return this._icon;
  92. },
  93. enumerable: true,
  94. configurable: true
  95. });
  96. return Region;
  97. }());
  98. function createSelectionBox($object) {
  99. return new RegionSelectionBox($object);
  100. }
  101. RegionSelection.createSelectionBox = createSelectionBox;
  102. })(RegionSelection || (RegionSelection = {}));