No Description

index.js 7.4KB

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