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.

Utilities.js 8.0KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Utilities.js
  3. * Keynote HTML Player
  4. *
  5. * Responsibility: Tungwei Cheng
  6. * Copyright (c) 2009-2013 Apple Inc. All rights reserved.
  7. */
  8. var s = Class.create({
  9. initialize: function(){}
  10. });
  11. function getMobileOSVersionInfo() {
  12. var match = navigator.userAgent.match(/iPhone OS ([\d_]+)/) || navigator.userAgent.match(/iPad OS ([\d_]+)/) || navigator.userAgent.match(/CPU OS ([\d_]+)/);
  13. var versionInfo = { major: 0, minor: 0, point: 0 };
  14. if (match) {
  15. var release = match[1].split('_');
  16. versionInfo.major = parseInt(release[0]);
  17. if (release.length > 1) {
  18. versionInfo.minor = parseInt(release[1]);
  19. }
  20. if (release.length > 2) {
  21. versionInfo.point = parseInt(release[2]);
  22. }
  23. }
  24. return versionInfo;
  25. }
  26. function isMobileSafari() {
  27. if (navigator.userAgent.indexOf('iPod') != -1) {
  28. return true;
  29. }
  30. else if (navigator.userAgent.indexOf('iPhone') != -1) {
  31. return true;
  32. }
  33. else if (navigator.userAgent.indexOf('iPad') != -1) {
  34. return true;
  35. }
  36. else {
  37. return false;
  38. }
  39. }
  40. function isiPad() {
  41. return (navigator.userAgent.indexOf('iPad') != -1);
  42. }
  43. function getUrlParameter(paramterName) {
  44. paramterName = paramterName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  45. var regExpPattern = "[\\?&]" + paramterName + "=([^&#]*)";
  46. var regExp = new RegExp(regExpPattern);
  47. var results = regExp.exec(window.location.href);
  48. if (results == null) {
  49. return "";
  50. } else {
  51. return results[1];
  52. }
  53. }
  54. function setElementProperty(node, propertyName, propertyValue) {
  55. if (browserPrefix == "ms") {
  56. node.style[propertyName] = propertyValue;
  57. } else {
  58. node.style.setProperty(propertyName, propertyValue, null);
  59. }
  60. }
  61. function setElementOpaque(element) {
  62. element.style.opacity = 1;
  63. }
  64. function setElementTransparent(element) {
  65. element.style.opacity = 0;
  66. }
  67. function setElementPosition(element, top, left, width, height) {
  68. if (element == null) {
  69. window.console.log("null element passed to setElementPosition " + top + ", " + left + ", " + width + ", " + height);
  70. return;
  71. }
  72. element.style.top = top + "px";
  73. element.style.left = left + "px";
  74. element.style.width = width + "px";
  75. element.style.height = height + "px";
  76. }
  77. function setElementRect(element, rect) {
  78. if (element == null) {
  79. return;
  80. }
  81. element.style.top = rect.y;
  82. element.style.left = rect.x;
  83. element.style.width = rect.width;
  84. element.style.height = rect.height;
  85. }
  86. function centerElementInDiv (element, elementWidth, elementHeight, divWidth, divHeight) {
  87. if (element == null) {
  88. return;
  89. }
  90. var top = (divHeight - elementHeight) / 2;
  91. var left = (divWidth - elementWidth) / 2;
  92. setElementPosition( element, top, left, elementWidth, elementHeight );
  93. }
  94. function showElement(element) {
  95. if (element == null) {
  96. return;
  97. }
  98. element.style.visibility = "visible";
  99. }
  100. function hideElement(element) {
  101. if (element == null) {
  102. return;
  103. }
  104. element.style.visibility = "hidden";
  105. }
  106. function runInNextEventLoop(codeBlock) {
  107. setTimeout(codeBlock, 100);
  108. }
  109. function ensureScaleFactorNotZero(scaleFactor) {
  110. // Mobile Safari doesn't like scale values of 0, force them to be 0.01
  111. if (scaleFactor == 0) {
  112. return 0.000001;
  113. } else {
  114. return scaleFactor;
  115. }
  116. }
  117. function scaleSizeWithinSize(sourceWidth, sourceHeight, destinationWidth, destinationHeight) {
  118. var scaledSize = {};
  119. var sourceAspectRatio = sourceWidth / sourceHeight;
  120. var destinationAspectRatio = destinationWidth / destinationHeight;
  121. if (sourceAspectRatio > destinationAspectRatio) {
  122. scaledSize.width = destinationWidth;
  123. scaledSize.height = sourceHeight * ( destinationWidth / sourceWidth );
  124. } else if (sourceAspectRatio < destinationAspectRatio) {
  125. scaledSize.width = sourceWidth * ( destinationHeight / sourceHeight );
  126. scaledSize.height = destinationHeight
  127. } else {
  128. scaledSize.width = destinationWidth;
  129. scaledSize.height = destinationHeight
  130. }
  131. return scaledSize;
  132. }
  133. function parseTransformMatrix(transformMatrix) {
  134. var parsedMatrix = [1,0,0,1,0,0];
  135. if (transformMatrix.indexOf( "matrix(" ) == 0) {
  136. var arrayString = transformMatrix.substring(7, transformMatrix.length - 1);
  137. parsedMatrix = arrayString.split(",");
  138. }
  139. return parsedMatrix;
  140. }
  141. function escapeTextureId(textureId) {
  142. var escapedTextureId = textureId.replace( /\./g, "-" );
  143. return escapedTextureId;
  144. }
  145. function unEscapeTextureId(textureId) {
  146. var escapedTextureId = textureId.replace( /\-/g, "." );
  147. return escapedTextureId;
  148. }
  149. var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
  150. var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  151. function LZ(x) {return(x<0||x>9?"":"0")+x;}
  152. Object.extend(Date.prototype, {
  153. // ------------------------------------------------------------------
  154. // formatDate (date_object, format)
  155. // Returns a date in the output format specified.
  156. // The format string uses the same abbreviations as in getDateFromFormat()
  157. //
  158. // ------------------------------------------------------------------
  159. format: function(format) {
  160. format=format+"";
  161. var date = this ;
  162. var result="";
  163. var i_format=0;
  164. var c="";
  165. var token="";
  166. var y=date.getFullYear()+"";
  167. var M=date.getMonth()+1;
  168. var d=date.getDate();
  169. var E=date.getDay();
  170. var H=date.getHours();
  171. var m=date.getMinutes();
  172. var s=date.getSeconds();
  173. var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
  174. // Convert real date parts into formatted versions
  175. var value=new Object();
  176. if (y.length < 4) {
  177. y=""+(y-0+1900);
  178. }
  179. value["y"]=""+y;
  180. value["yyyy"]=y;
  181. value["yy"]=y.substring(2,4);
  182. value["M"]=M;
  183. value["MM"]=LZ(M);
  184. value["MMM"]=MONTH_NAMES[M-1];
  185. value["NNN"]=MONTH_NAMES[M+11];
  186. value["d"]=d;
  187. value["dd"]=LZ(d);
  188. value["E"]=DAY_NAMES[E+7];
  189. value["EE"]=DAY_NAMES[E];
  190. value["H"]=H;
  191. value["HH"]=LZ(H);
  192. if (H==0) {
  193. value["h"]=12;
  194. } else if (H>12) {
  195. value["h"]=H-12;
  196. } else {
  197. value["h"]=H;
  198. }
  199. value["hh"]=LZ(value["h"]);
  200. if (H>11) {
  201. value["K"]=H-12;
  202. } else {
  203. value["K"]=H;
  204. }
  205. value["k"]=H+1;
  206. value["KK"]=LZ(value["K"]);
  207. value["kk"]=LZ(value["k"]);
  208. if (H > 11) {
  209. value["a"]="PM";
  210. } else {
  211. value["a"]="AM";
  212. }
  213. value["m"]=m;
  214. value["mm"]=LZ(m);
  215. value["s"]=s;
  216. value["ss"]=LZ(s);
  217. while (i_format < format.length) {
  218. c=format.charAt(i_format);
  219. token="";
  220. while ((format.charAt(i_format)==c) && (i_format < format.length)) {
  221. token += format.charAt(i_format++);
  222. }
  223. if (value[token] != null) {
  224. result=result + value[token];
  225. } else {
  226. result=result + token;
  227. }
  228. }
  229. return result;
  230. }
  231. });
  232. function getHecklerElementsByTagName(xml, tagName) {
  233. return getElementsByTagNameNS(xml, tagName, 'urn:iwork:property', 'X:');
  234. }
  235. function getElementsByTagNameNS(xml, tagName, ns, prefix) {
  236. var nodes = null;
  237. if (xml.getElementsByTagNameNS) {
  238. nodes = xml.getElementsByTagNameNS(ns, tagName);
  239. } else {
  240. // IE7 Does not support getElementsByTagNameNS
  241. // So we have to do this silly IE7 workaround and prefix the Heckler
  242. // namespace to everything
  243. nodes = xml.getElementsByTagName(prefix + tagName);
  244. }
  245. return nodes;
  246. }