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.

prezi_player.js 11KB

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