region-selection.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /// <reference path="../../node_modules/@types/jquery/index.d.ts"/>
  2. namespace RegionSelection {
  3. function foreach(data, callback: (value: any, key?: number | string, array?: Array<any>) => void) {
  4. Array.prototype.forEach.call(data, callback);
  5. }
  6. class RegionSelectionBox {
  7. private selector: JQuery;
  8. private regions: Region[] = [];
  9. private box: JQuery;
  10. constructor(selector: JQuery) {
  11. this.selector = selector;
  12. if (this.selector.data('regions') !== undefined) {
  13. this.fillRegions(this.selector.data('regions'));
  14. }
  15. this.selector.on('click', this.showBox.bind(this));
  16. }
  17. private fillRegions(data) {
  18. foreach(eval(data), (value: RegionInterface) => {
  19. this.regions.push(new Region(value));
  20. });
  21. }
  22. private showBox(e: JQueryEventObject) {
  23. e.preventDefault();
  24. e.stopPropagation();
  25. if (this.box === undefined) {
  26. this.renderBox();
  27. }
  28. let box = this.box;
  29. let hide = function () {
  30. box.hide(0, () => {
  31. $('body').off('click', hide);
  32. });
  33. }.bind(this);
  34. if (box.is(':visible')) {
  35. hide();
  36. } else {
  37. $('body').on('click', hide);
  38. box.show(0);
  39. }
  40. }
  41. private renderBox() {
  42. if (!$('.region__selection-box').length) {
  43. this.box = $('<div class="region__selection-box region-selection__box"></div>');
  44. let itemTemplate = '<a href="{link}" class="region-selection__link">{icon} {name}</a>';
  45. let iconTemplate = '<img src="{icon}" class="region-selection__icon">';
  46. foreach(this.regions, (region: Region) => {
  47. let item = $(itemTemplate
  48. .replace('{link}', region.link)
  49. .replace('{name}', region.caption)
  50. .replace('{icon}', iconTemplate.replace('{icon}', region.icon))
  51. );
  52. this.box.append(item);
  53. });
  54. this.box.css('display', 'none');
  55. this.selector.parent().append(this.box);
  56. }
  57. }
  58. }
  59. interface RegionInterface {
  60. id: number;
  61. caption: string;
  62. link: string;
  63. icon: string;
  64. }
  65. class Region {
  66. constructor(data: RegionInterface) {
  67. this._id = data.id;
  68. this._caption = data.caption;
  69. this._link = data.link;
  70. this._icon = data.icon;
  71. }
  72. private _id: number;
  73. get id() {
  74. return this._id;
  75. }
  76. private _caption: string;
  77. get caption() {
  78. return this._caption;
  79. }
  80. private _link: string;
  81. get link(): string {
  82. return this._link;
  83. }
  84. private _icon: string;
  85. get icon(): string {
  86. return this._icon;
  87. }
  88. }
  89. export function createSelectionBox($object: JQuery) {
  90. return new RegionSelectionBox($object);
  91. }
  92. }