Bez popisu

index.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var Siema = require("siema");
  4. var index_1 = require("../WhitePage/index");
  5. var uuid_1 = require("../../utils/uuid");
  6. var dom_1 = require("../../utils/dom");
  7. require("./index.less");
  8. var LeftArrowIcon = require('../../assets/bx-left-arrow.svg');
  9. var RightArrowIcon = require('../../assets/bx-right-arrow.svg');
  10. var prefix = 'fcw-board';
  11. var SerializableWhiteboard = (function () {
  12. function SerializableWhiteboard() {
  13. }
  14. return SerializableWhiteboard;
  15. }());
  16. exports.SerializableWhiteboard = SerializableWhiteboard;
  17. var Whiteboard = (function () {
  18. function Whiteboard(target, _a) {
  19. var _b = _a === void 0 ? {} : _a, sources = _b.sources, eventHub = _b.eventHub, mode = _b.mode, visiblePageIndex = _b.visiblePageIndex;
  20. this.id = uuid_1.uuid();
  21. this.sources = [];
  22. this.mode = 'master';
  23. this.isFullscreen = false;
  24. this.pages = [];
  25. this.isInitialized = false;
  26. this.isSyncing = false;
  27. this.visiblePageIndex = 0;
  28. if (target) {
  29. this.target = target;
  30. }
  31. else {
  32. this.target = document.createElement('div');
  33. document.body.appendChild(this.target);
  34. }
  35. if (!this.target.id) {
  36. this.target.id = this.id;
  37. }
  38. dom_1.addClassName(this.target, prefix);
  39. if (sources) {
  40. this.sources = sources;
  41. }
  42. this.eventHub = eventHub;
  43. if (mode) {
  44. this.mode = mode;
  45. }
  46. if (typeof visiblePageIndex !== 'undefined') {
  47. this.visiblePageIndex = visiblePageIndex;
  48. }
  49. this.init();
  50. }
  51. Object.defineProperty(Whiteboard.prototype, "activePage", {
  52. get: function () {
  53. return this.pages[this.visiblePageIndex];
  54. },
  55. enumerable: true,
  56. configurable: true
  57. });
  58. Whiteboard.prototype.open = function () {
  59. var _this = this;
  60. this.pages.forEach(function (page, i) {
  61. page.open();
  62. if (i !== _this.visiblePageIndex) {
  63. page.hide();
  64. }
  65. });
  66. };
  67. Whiteboard.prototype.close = function () {
  68. if (this.emitInterval) {
  69. clearInterval(this.emitInterval);
  70. }
  71. };
  72. Whiteboard.prototype.show = function () {
  73. if (this.activePage) {
  74. this.activePage.show();
  75. }
  76. };
  77. Whiteboard.prototype.hide = function () {
  78. if (this.activePage) {
  79. this.activePage.hide();
  80. }
  81. };
  82. Whiteboard.prototype.snap = function () {
  83. return {
  84. id: this.id,
  85. sources: this.sources,
  86. pageIds: this.pages.map(function (page) { return page.id; }),
  87. visiblePageIndex: this.visiblePageIndex
  88. };
  89. };
  90. Whiteboard.prototype.init = function () {
  91. this.imgsContainer = dom_1.createDivWithClassName(prefix + "-imgs", this.target);
  92. this.pagesContainer = dom_1.createDivWithClassName(prefix + "-pages", this.target);
  93. if (this.mode === 'master') {
  94. this.initMaster();
  95. this.emitSnapshot();
  96. }
  97. if (this.mode === 'mirror') {
  98. this.initMirror();
  99. }
  100. };
  101. Whiteboard.prototype.initMaster = function () {
  102. var _this = this;
  103. this.sources.forEach(function (source) {
  104. var page = new index_1.WhitePage({ imgSrc: source }, {
  105. mode: _this.mode,
  106. eventHub: _this.eventHub,
  107. parentContainer: _this.pagesContainer
  108. });
  109. page.container.style.visibility = 'hidden';
  110. _this.pages.push(page);
  111. });
  112. this.initSiema();
  113. var controller = dom_1.createDivWithClassName(prefix + "-controller", this.target);
  114. var prevEle = dom_1.createDivWithClassName(prefix + "-flip-arrow", controller);
  115. prevEle.innerHTML = LeftArrowIcon;
  116. var nextEle = dom_1.createDivWithClassName(prefix + "-flip-arrow", controller);
  117. nextEle.innerHTML = RightArrowIcon;
  118. nextEle.addEventListener('click', function () {
  119. var nextPageIndex = _this.visiblePageIndex + 1 > _this.pages.length - 1 ? 0 : _this.visiblePageIndex + 1;
  120. _this.onPageChange(nextPageIndex);
  121. });
  122. prevEle.addEventListener('click', function () {
  123. var nextPageIndex = _this.visiblePageIndex - 1 < 0 ? _this.pages.length - 1 : _this.visiblePageIndex - 1;
  124. _this.onPageChange(nextPageIndex);
  125. });
  126. };
  127. Whiteboard.prototype.initMirror = function () {
  128. var _this = this;
  129. if (!this.eventHub) {
  130. throw new Error('Invalid eventHub');
  131. }
  132. this.eventHub.on('sync', function (ev) {
  133. if (ev.target !== 'whiteboard') {
  134. return;
  135. }
  136. if (ev.event === 'snap') {
  137. if (_this.isInitialized) {
  138. return;
  139. }
  140. _this.onSnapshot(ev.data);
  141. }
  142. if (ev.event === 'changeIndex' && ev.id === _this.id) {
  143. if (_this.isInitialized) {
  144. _this.onPageChange(ev.data);
  145. }
  146. }
  147. });
  148. };
  149. Whiteboard.prototype.initSiema = function () {
  150. var _this = this;
  151. this.sources.forEach(function (source) {
  152. var imgEle = document.createElement('img');
  153. dom_1.addClassName(imgEle, prefix + "-img");
  154. imgEle.src = source;
  155. imgEle.alt = 'Siema image';
  156. _this.imgsContainer.appendChild(imgEle);
  157. });
  158. this.siema = new Siema({
  159. selector: this.imgsContainer,
  160. duration: 200,
  161. easing: 'ease-out',
  162. perPage: 1,
  163. startIndex: 0,
  164. draggable: false,
  165. multipleDrag: true,
  166. threshold: 20,
  167. loop: false,
  168. rtl: false
  169. });
  170. };
  171. Whiteboard.prototype.onPageChange = function (nextPageIndex) {
  172. this.siema.goTo(nextPageIndex);
  173. this.visiblePageIndex = nextPageIndex;
  174. this.pages.forEach(function (page, i) {
  175. if (nextPageIndex === i) {
  176. page.show();
  177. }
  178. else {
  179. page.hide();
  180. }
  181. });
  182. if (this.mode === 'master' && this.eventHub) {
  183. this.eventHub.emit('sync', {
  184. event: 'changeIndex',
  185. id: this.id,
  186. target: 'whiteboard',
  187. data: nextPageIndex
  188. });
  189. }
  190. };
  191. Whiteboard.prototype.emitSnapshot = function () {
  192. var _this = this;
  193. var innerFunc = function () {
  194. if (_this.eventHub) {
  195. _this.eventHub.emit('sync', {
  196. event: 'snap',
  197. id: _this.id,
  198. target: 'whiteboard',
  199. data: _this.snap()
  200. });
  201. }
  202. };
  203. this.emitInterval = setInterval(function () {
  204. innerFunc();
  205. }, 5 * 1000);
  206. setTimeout(innerFunc, 500);
  207. };
  208. Whiteboard.prototype.onSnapshot = function (snap) {
  209. var _this = this;
  210. var id = snap.id, sources = snap.sources, pageIds = snap.pageIds, visiblePageIndex = snap.visiblePageIndex;
  211. if (!this.isInitialized && !this.isSyncing) {
  212. this.id = id;
  213. this.sources = sources;
  214. this.isSyncing = true;
  215. this.sources.forEach(function (source, i) {
  216. var page = new index_1.WhitePage({ imgSrc: source }, {
  217. mode: _this.mode,
  218. eventHub: _this.eventHub,
  219. parentContainer: _this.pagesContainer
  220. });
  221. page.id = pageIds[i];
  222. page.container.style.visibility = 'hidden';
  223. _this.pages.push(page);
  224. page.open();
  225. });
  226. this.initSiema();
  227. }
  228. this.isInitialized = true;
  229. this.isSyncing = false;
  230. this.onPageChange(visiblePageIndex);
  231. };
  232. return Whiteboard;
  233. }());
  234. exports.Whiteboard = Whiteboard;