No Description

index.js 7.8KB

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