You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreziPlayer.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. (function() {
  17. "use strict";
  18. var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  19. window.PreziPlayer = (function() {
  20. PreziPlayer.API_VERSION = 1;
  21. PreziPlayer.CURRENT_STEP = 'currentStep';
  22. PreziPlayer.CURRENT_ANIMATION_STEP = 'currentAnimationStep';
  23. PreziPlayer.CURRENT_OBJECT = 'currentObject';
  24. PreziPlayer.STATUS_LOADING = 'loading';
  25. PreziPlayer.STATUS_READY = 'ready';
  26. PreziPlayer.STATUS_CONTENT_READY = 'contentready';
  27. PreziPlayer.EVENT_CURRENT_STEP = "currentStepChange";
  28. PreziPlayer.EVENT_CURRENT_ANIMATION_STEP = "currentAnimationStepChange";
  29. PreziPlayer.EVENT_CURRENT_OBJECT = "currentObjectChange";
  30. PreziPlayer.EVENT_STATUS = "statusChange";
  31. PreziPlayer.EVENT_PLAYING = "isAutoPlayingChange";
  32. PreziPlayer.EVENT_IS_MOVING = "isMovingChange";
  33. PreziPlayer.domain = "https://prezi.com";
  34. PreziPlayer.path = "/player/";
  35. PreziPlayer.players = {};
  36. PreziPlayer.binded_methods = ['changesHandler'];
  37. PreziPlayer.createMultiplePlayers = function(optionArray){
  38. for(var i=0; i<optionArray.length; i++) {
  39. var optionSet = optionArray[i];
  40. new PreziPlayer(optionSet.id, optionSet);
  41. };
  42. };
  43. PreziPlayer.messageReceived = function(event){
  44. var message, item, player;
  45. try {
  46. message = JSON.parse(event.data);
  47. if (message.id && (player = PreziPlayer.players[message.id])) {
  48. if (player.options.debug === true) {
  49. if (console && console.log)
  50. console.log('received', message);
  51. }
  52. if (message.type === "changes") {
  53. player.changesHandler(message);
  54. }
  55. for (var i = 0; i < player.callbacks.length; i++) {
  56. item = player.callbacks[i];
  57. if (item && message.type === item.event) {
  58. item.callback(message);
  59. }
  60. }
  61. }
  62. } catch (e) { }
  63. };
  64. function PreziPlayer(id, options) {
  65. var params, paramString = "", _this = this;
  66. if (PreziPlayer.players[id]){
  67. PreziPlayer.players[id].destroy();
  68. }
  69. for(var i=0; i<PreziPlayer.binded_methods.length; i++) {
  70. var method_name = PreziPlayer.binded_methods[i];
  71. _this[method_name] = __bind(_this[method_name], _this);
  72. };
  73. options = options || {};
  74. this.options = options;
  75. this.values = {'status': PreziPlayer.STATUS_LOADING};
  76. this.values[PreziPlayer.CURRENT_STEP] = 0;
  77. this.values[PreziPlayer.CURRENT_ANIMATION_STEP] = 0;
  78. this.values[PreziPlayer.CURRENT_OBJECT] = null;
  79. this.callbacks = [];
  80. this.id = id;
  81. this.embedTo = document.getElementById(id);
  82. if (!this.embedTo) {
  83. throw "The element id is not available.";
  84. }
  85. this.iframe = document.createElement('iframe');
  86. params = [
  87. { name: 'oid', value: options.preziId },
  88. { name: 'explorable', value: options.explorable ? 1 : 0 },
  89. { name: 'controls', value: options.controls ? 1 : 0 }
  90. ];
  91. for(var i=0; i<params.length; i++) {
  92. var param = params[i];
  93. paramString += (i===0 ? "?" : "&") + param.name + "=" + param.value;
  94. };
  95. this.iframe.src = PreziPlayer.domain + PreziPlayer.path + paramString;
  96. this.iframe.frameBorder = 0;
  97. this.iframe.scrolling = "no";
  98. this.iframe.width = options.width || 640;
  99. this.iframe.height = options.height || 480;
  100. this.embedTo.innerHTML = '';
  101. // JITSI: IN CASE SOMETHING GOES WRONG.
  102. try {
  103. this.embedTo.appendChild(this.iframe);
  104. }
  105. catch (err) {
  106. console.log("CATCH ERROR");
  107. }
  108. // JITSI: Increase interval from 200 to 500, which fixes prezi
  109. // crashes for us.
  110. this.initPollInterval = setInterval(function(){
  111. _this.sendMessage({'action': 'init'});
  112. }, 500);
  113. PreziPlayer.players[id] = this;
  114. }
  115. PreziPlayer.prototype.changesHandler = function(message) {
  116. var key, value, j, item;
  117. if (this.initPollInterval) {
  118. clearInterval(this.initPollInterval);
  119. this.initPollInterval = false;
  120. }
  121. for (key in message.data) {
  122. if (message.data.hasOwnProperty(key)){
  123. value = message.data[key];
  124. this.values[key] = value;
  125. for (j=0; j<this.callbacks.length; j++) {
  126. item = this.callbacks[j];
  127. if (item && item.event === key + "Change"){
  128. item.callback({type: item.event, value: value});
  129. }
  130. }
  131. }
  132. }
  133. };
  134. PreziPlayer.prototype.destroy = function() {
  135. if (this.initPollInterval) {
  136. clearInterval(this.initPollInterval);
  137. this.initPollInterval = false;
  138. }
  139. this.embedTo.innerHTML = '';
  140. };
  141. PreziPlayer.prototype.sendMessage = function(message) {
  142. if (this.options.debug === true) {
  143. if (console && console.log) console.log('sent', message);
  144. }
  145. message.version = PreziPlayer.API_VERSION;
  146. message.id = this.id;
  147. return this.iframe.contentWindow.postMessage(JSON.stringify(message), '*');
  148. };
  149. PreziPlayer.prototype.nextStep = /* nextStep is DEPRECATED */
  150. PreziPlayer.prototype.flyToNextStep = function() {
  151. return this.sendMessage({
  152. 'action': 'present',
  153. 'data': ['moveToNextStep']
  154. });
  155. };
  156. PreziPlayer.prototype.previousStep = /* previousStep is DEPRECATED */
  157. PreziPlayer.prototype.flyToPreviousStep = function() {
  158. return this.sendMessage({
  159. 'action': 'present',
  160. 'data': ['moveToPrevStep']
  161. });
  162. };
  163. PreziPlayer.prototype.toStep = /* toStep is DEPRECATED */
  164. PreziPlayer.prototype.flyToStep = function(step, animation_step) {
  165. var obj = this;
  166. // check animation_step
  167. if (animation_step > 0 &&
  168. obj.values.animationCountOnSteps &&
  169. obj.values.animationCountOnSteps[step] <= animation_step) {
  170. animation_step = obj.values.animationCountOnSteps[step];
  171. }
  172. // jump to animation steps by calling flyToNextStep()
  173. function doAnimationSteps() {
  174. if (obj.values.isMoving == true) {
  175. setTimeout(doAnimationSteps, 100); // wait until the flight ends
  176. return;
  177. }
  178. while (animation_step-- > 0) {
  179. obj.flyToNextStep(); // do the animation steps
  180. }
  181. }
  182. setTimeout(doAnimationSteps, 200); // 200ms is the internal "reporting" time
  183. // jump to the step
  184. return this.sendMessage({
  185. 'action': 'present',
  186. 'data': ['moveToStep', step]
  187. });
  188. };
  189. PreziPlayer.prototype.toObject = /* toObject is DEPRECATED */
  190. PreziPlayer.prototype.flyToObject = function(objectId) {
  191. return this.sendMessage({
  192. 'action': 'present',
  193. 'data': ['moveToObject', objectId]
  194. });
  195. };
  196. PreziPlayer.prototype.play = function(defaultDelay) {
  197. return this.sendMessage({
  198. 'action': 'present',
  199. 'data': ['startAutoPlay', defaultDelay]
  200. });
  201. };
  202. PreziPlayer.prototype.stop = function() {
  203. return this.sendMessage({
  204. 'action': 'present',
  205. 'data': ['stopAutoPlay']
  206. });
  207. };
  208. PreziPlayer.prototype.pause = function(defaultDelay) {
  209. return this.sendMessage({
  210. 'action': 'present',
  211. 'data': ['pauseAutoPlay', defaultDelay]
  212. });
  213. };
  214. PreziPlayer.prototype.getCurrentStep = function() {
  215. return this.values.currentStep;
  216. };
  217. PreziPlayer.prototype.getCurrentAnimationStep = function() {
  218. return this.values.currentAnimationStep;
  219. };
  220. PreziPlayer.prototype.getCurrentObject = function() {
  221. return this.values.currentObject;
  222. };
  223. PreziPlayer.prototype.getStatus = function() {
  224. return this.values.status;
  225. };
  226. PreziPlayer.prototype.isPlaying = function() {
  227. return this.values.isAutoPlaying;
  228. };
  229. PreziPlayer.prototype.getStepCount = function() {
  230. return this.values.stepCount;
  231. };
  232. PreziPlayer.prototype.getAnimationCountOnSteps = function() {
  233. return this.values.animationCountOnSteps;
  234. };
  235. PreziPlayer.prototype.getTitle = function() {
  236. return this.values.title;
  237. };
  238. PreziPlayer.prototype.setDimensions = function(dims) {
  239. for (var parameter in dims) {
  240. this.iframe[parameter] = dims[parameter];
  241. }
  242. }
  243. PreziPlayer.prototype.getDimensions = function() {
  244. return {
  245. width: parseInt(this.iframe.width, 10),
  246. height: parseInt(this.iframe.height, 10)
  247. }
  248. }
  249. PreziPlayer.prototype.on = function(event, callback) {
  250. this.callbacks.push({
  251. event: event,
  252. callback: callback
  253. });
  254. };
  255. PreziPlayer.prototype.off = function(event, callback) {
  256. var j, item;
  257. if (event === undefined) {
  258. this.callbacks = [];
  259. }
  260. j = this.callbacks.length;
  261. while (j--) {
  262. item = this.callbacks[j];
  263. if (item && item.event === event && (callback === undefined || item.callback === callback)){
  264. this.callbacks.splice(j, 1);
  265. }
  266. }
  267. };
  268. if (window.addEventListener) {
  269. window.addEventListener('message', PreziPlayer.messageReceived, false);
  270. } else {
  271. window.attachEvent('onmessage', PreziPlayer.messageReceived);
  272. }
  273. return PreziPlayer;
  274. })();
  275. })();
  276. module.exports = PreziPlayer;