ApiClient.js 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765
  1. if (!window.MediaBrowser) {
  2. window.MediaBrowser = {};
  3. }
  4. MediaBrowser.ApiClient = function ($, navigator, JSON, WebSocket, setTimeout) {
  5. /**
  6. * Creates a new api client instance
  7. * @param {String} serverProtocol
  8. * @param {String} serverHostName
  9. * @param {String} serverPortNumber
  10. * @param {String} clientName
  11. */
  12. return function (serverProtocol, serverHostName, serverPortNumber, clientName) {
  13. if (!serverProtocol) {
  14. throw new Error("Must supply a serverProtocol, e.g. http:");
  15. }
  16. if (!serverHostName) {
  17. throw new Error("Must supply serverHostName, e.g. 192.168.1.1 or myServerName");
  18. }
  19. if (!serverPortNumber) {
  20. throw new Error("Must supply a serverPortNumber");
  21. }
  22. var self = this;
  23. var deviceName = "Web Browser";
  24. var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  25. var currentUserId;
  26. var webSocket;
  27. /**
  28. * Gets the server host name.
  29. */
  30. self.serverHostName = function () {
  31. return serverHostName;
  32. };
  33. /**
  34. * Gets the server port number.
  35. */
  36. self.serverPortNumber = function () {
  37. return serverPortNumber;
  38. };
  39. /**
  40. * Gets or sets the current user id.
  41. */
  42. self.currentUserId = function (val) {
  43. if (val !== undefined) {
  44. currentUserId = val;
  45. } else {
  46. return currentUserId;
  47. }
  48. };
  49. deviceName = (function () {
  50. var name = "";
  51. if ($.browser.chrome) {
  52. name = "Chrome";
  53. }
  54. else if ($.browser.safari) {
  55. name = "Safari";
  56. }
  57. else if ($.browser.webkit) {
  58. name = "WebKit";
  59. }
  60. else if ($.browser.msie) {
  61. name = "Internet Explorer";
  62. }
  63. else if ($.browser.firefox || $.browser.mozilla) {
  64. name = "Firefox";
  65. }
  66. else if ($.browser.opera) {
  67. name = "Opera";
  68. }
  69. else {
  70. name = "Web Browser";
  71. }
  72. if ($.browser.ipad) {
  73. name += " Ipad";
  74. }
  75. else if ($.browser.iphone) {
  76. name += " Iphone";
  77. }
  78. else if ($.browser.android) {
  79. name += " Android";
  80. }
  81. return name;
  82. }());
  83. self.deviceId = function () {
  84. return deviceId;
  85. };
  86. self.encodeName = function (name) {
  87. name = name.split('/').join('-');
  88. name = name.split('?').join('-');
  89. var val = $.param({ name: name });
  90. return val.substring(val.indexOf('=') + 1).replace("'", '%27');
  91. };
  92. /**
  93. * Wraps around jQuery ajax methods to add additional info to the request.
  94. */
  95. self.ajax = function (request) {
  96. if (!request) {
  97. throw new Error("Request cannot be null");
  98. }
  99. if (clientName) {
  100. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '"';
  101. if (currentUserId) {
  102. auth += ', UserId="' + currentUserId + '"';
  103. }
  104. request.headers = {
  105. Authorization: auth
  106. };
  107. }
  108. return $.ajax(request);
  109. };
  110. /**
  111. * Creates an api url based on a handler name and query string parameters
  112. * @param {String} name
  113. * @param {Object} params
  114. */
  115. self.getUrl = function (name, params) {
  116. if (!name) {
  117. throw new Error("Url name cannot be empty");
  118. }
  119. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  120. if (params) {
  121. url += "?" + $.param(params);
  122. }
  123. return url;
  124. };
  125. self.openWebSocket = function (port) {
  126. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  127. webSocket = new WebSocket(url);
  128. webSocket.onmessage = function (msg) {
  129. msg = JSON.parse(msg.data);
  130. $(self).trigger("websocketmessage", [msg]);
  131. };
  132. webSocket.onopen = function () {
  133. setTimeout(function () {
  134. self.sendWebSocketMessage("Identity", clientName + "|" + deviceId);
  135. $(self).trigger("websocketopen");
  136. }, 500);
  137. };
  138. webSocket.onerror = function () {
  139. setTimeout(function () {
  140. $(self).trigger("websocketerror");
  141. }, 0);
  142. };
  143. webSocket.onclose = function () {
  144. setTimeout(function () {
  145. $(self).trigger("websocketclose");
  146. }, 0);
  147. };
  148. };
  149. self.sendWebSocketMessage = function (name, data) {
  150. var msg = { MessageType: name };
  151. if (data) {
  152. msg.Data = data;
  153. }
  154. msg = JSON.stringify(msg);
  155. webSocket.send(msg);
  156. };
  157. self.isWebSocketOpen = function () {
  158. return webSocket && webSocket.readyState === WebSocket.OPEN;
  159. };
  160. self.isWebSocketOpenOrConnecting = function () {
  161. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  162. };
  163. /**
  164. * Gets an item from the server
  165. * Omit itemId to get the root folder.
  166. */
  167. self.getItem = function (userId, itemId) {
  168. if (!userId) {
  169. throw new Error("null userId");
  170. }
  171. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  172. return self.ajax({
  173. type: "GET",
  174. url: url,
  175. dataType: "json"
  176. });
  177. };
  178. /**
  179. * Gets the root folder from the server
  180. */
  181. self.getRootFolder = function (userId) {
  182. if (!userId) {
  183. throw new Error("null userId");
  184. }
  185. var url = self.getUrl("Users/" + userId + "/Items/Root");
  186. return self.ajax({
  187. type: "GET",
  188. url: url,
  189. dataType: "json"
  190. });
  191. };
  192. /**
  193. * Gets the current server status
  194. */
  195. self.getSystemInfo = function () {
  196. var url = self.getUrl("System/Info");
  197. return self.ajax({
  198. type: "GET",
  199. url: url,
  200. dataType: "json"
  201. });
  202. };
  203. /**
  204. * Gets all cultures known to the server
  205. */
  206. self.getCultures = function () {
  207. var url = self.getUrl("Localization/cultures");
  208. return self.ajax({
  209. type: "GET",
  210. url: url,
  211. dataType: "json"
  212. });
  213. };
  214. /**
  215. * Gets all countries known to the server
  216. */
  217. self.getCountries = function () {
  218. var url = self.getUrl("Localization/countries");
  219. return self.ajax({
  220. type: "GET",
  221. url: url,
  222. dataType: "json"
  223. });
  224. };
  225. /**
  226. * Gets plugin security info
  227. */
  228. self.getPluginSecurityInfo = function () {
  229. var url = self.getUrl("Plugins/SecurityInfo");
  230. return self.ajax({
  231. type: "GET",
  232. url: url,
  233. dataType: "json"
  234. });
  235. };
  236. /**
  237. * Gets the directory contents of a path on the server
  238. */
  239. self.getDirectoryContents = function (path, options) {
  240. if (!path) {
  241. throw new Error("null path");
  242. }
  243. options = options || {};
  244. options.path = path;
  245. var url = self.getUrl("Environment/DirectoryContents", options);
  246. return self.ajax({
  247. type: "GET",
  248. url: url,
  249. dataType: "json"
  250. });
  251. };
  252. /**
  253. * Gets a list of physical drives from the server
  254. */
  255. self.getDrives = function () {
  256. var url = self.getUrl("Environment/Drives");
  257. return self.ajax({
  258. type: "GET",
  259. url: url,
  260. dataType: "json"
  261. });
  262. };
  263. /**
  264. * Gets a list of network devices from the server
  265. */
  266. self.getNetworkDevices = function () {
  267. var url = self.getUrl("Environment/NetworkDevices");
  268. return self.ajax({
  269. type: "GET",
  270. url: url,
  271. dataType: "json"
  272. });
  273. };
  274. /**
  275. * Cancels a package installation
  276. */
  277. self.cancelPackageInstallation = function (installationId) {
  278. if (!installationId) {
  279. throw new Error("null installationId");
  280. }
  281. var url = self.getUrl("Packages/Installing/" + id);
  282. return self.ajax({
  283. type: "DELETE",
  284. url: url
  285. });
  286. };
  287. /**
  288. * Installs or updates a new plugin
  289. */
  290. self.installPlugin = function (name, updateClass, version) {
  291. if (!name) {
  292. throw new Error("null name");
  293. }
  294. if (!updateClass) {
  295. throw new Error("null updateClass");
  296. }
  297. var options = {
  298. updateClass: updateClass
  299. };
  300. if (version) {
  301. options.version = version;
  302. }
  303. var url = self.getUrl("Packages/Installed/" + name, options);
  304. return self.ajax({
  305. type: "POST",
  306. url: url
  307. });
  308. };
  309. /**
  310. * Instructs the server to perform a pending kernel reload or app restart.
  311. * If a restart is not currently required, nothing will happen.
  312. */
  313. self.performPendingRestart = function () {
  314. var url = self.getUrl("System/Restart");
  315. return self.ajax({
  316. type: "POST",
  317. url: url
  318. });
  319. };
  320. /**
  321. * Gets information about an installable package
  322. */
  323. self.getPackageInfo = function (name) {
  324. if (!name) {
  325. throw new Error("null name");
  326. }
  327. var url = self.getUrl("Packages/" + name);
  328. return self.ajax({
  329. type: "GET",
  330. url: url,
  331. dataType: "json"
  332. });
  333. };
  334. /**
  335. * Gets the latest available application update (if any)
  336. */
  337. self.getAvailableApplicationUpdate = function () {
  338. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  339. return self.ajax({
  340. type: "GET",
  341. url: url,
  342. dataType: "json"
  343. });
  344. };
  345. /**
  346. * Gets the latest available plugin updates (if any)
  347. */
  348. self.getAvailablePluginUpdates = function () {
  349. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  350. return self.ajax({
  351. type: "GET",
  352. url: url,
  353. dataType: "json"
  354. });
  355. };
  356. /**
  357. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  358. */
  359. self.getVirtualFolders = function (userId) {
  360. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  361. url = self.getUrl(url);
  362. return self.ajax({
  363. type: "GET",
  364. url: url,
  365. dataType: "json"
  366. });
  367. };
  368. /**
  369. * Gets all the paths of the locations in the physical root.
  370. */
  371. self.getPhysicalPaths = function () {
  372. var url = self.getUrl("Library/PhysicalPaths");
  373. return self.ajax({
  374. type: "GET",
  375. url: url,
  376. dataType: "json"
  377. });
  378. };
  379. /**
  380. * Gets the current server configuration
  381. */
  382. self.getServerConfiguration = function () {
  383. var url = self.getUrl("System/Configuration");
  384. return self.ajax({
  385. type: "GET",
  386. url: url,
  387. dataType: "json"
  388. });
  389. };
  390. /**
  391. * Gets the server's scheduled tasks
  392. */
  393. self.getScheduledTasks = function () {
  394. var url = self.getUrl("ScheduledTasks");
  395. return self.ajax({
  396. type: "GET",
  397. url: url,
  398. dataType: "json"
  399. });
  400. };
  401. /**
  402. * Starts a scheduled task
  403. */
  404. self.startScheduledTask = function (id) {
  405. if (!id) {
  406. throw new Error("null id");
  407. }
  408. var url = self.getUrl("ScheduledTasks/Running/" + id);
  409. return self.ajax({
  410. type: "POST",
  411. url: url
  412. });
  413. };
  414. /**
  415. * Gets a scheduled task
  416. */
  417. self.getScheduledTask = function (id) {
  418. if (!id) {
  419. throw new Error("null id");
  420. }
  421. var url = self.getUrl("ScheduledTasks/" + id);
  422. return self.ajax({
  423. type: "GET",
  424. url: url,
  425. dataType: "json"
  426. });
  427. };
  428. self.getNextUpEpisodes = function (options) {
  429. var url = self.getUrl("Shows/NextUp", options);
  430. return self.ajax({
  431. type: "GET",
  432. url: url,
  433. dataType: "json"
  434. });
  435. };
  436. /**
  437. * Stops a scheduled task
  438. */
  439. self.stopScheduledTask = function (id) {
  440. if (!id) {
  441. throw new Error("null id");
  442. }
  443. var url = self.getUrl("ScheduledTasks/Running/" + id);
  444. return self.ajax({
  445. type: "DELETE",
  446. url: url
  447. });
  448. };
  449. /**
  450. * Gets the configuration of a plugin
  451. * @param {String} Id
  452. */
  453. self.getPluginConfiguration = function (id) {
  454. if (!id) {
  455. throw new Error("null Id");
  456. }
  457. var url = self.getUrl("Plugins/" + id + "/Configuration");
  458. return self.ajax({
  459. type: "GET",
  460. url: url,
  461. dataType: "json"
  462. });
  463. };
  464. /**
  465. * Gets a list of plugins that are available to be installed
  466. */
  467. self.getAvailablePlugins = function (options) {
  468. options = $.extend({}, options || {});
  469. options.PackageType = "UserInstalled";
  470. var url = self.getUrl("Packages", options);
  471. return self.ajax({
  472. type: "GET",
  473. url: url,
  474. dataType: "json"
  475. });
  476. };
  477. /**
  478. * Uninstalls a plugin
  479. * @param {String} Id
  480. */
  481. self.uninstallPlugin = function (id) {
  482. if (!id) {
  483. throw new Error("null Id");
  484. }
  485. var url = self.getUrl("Plugins/" + id);
  486. return self.ajax({
  487. type: "DELETE",
  488. url: url
  489. });
  490. };
  491. /**
  492. * Removes a virtual folder from either the default view or a user view
  493. * @param {String} name
  494. */
  495. self.removeVirtualFolder = function (name, userId) {
  496. if (!name) {
  497. throw new Error("null name");
  498. }
  499. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  500. url += "/" + name;
  501. url = self.getUrl(url);
  502. return self.ajax({
  503. type: "DELETE",
  504. url: url
  505. });
  506. };
  507. /**
  508. * Adds a virtual folder to either the default view or a user view
  509. * @param {String} name
  510. */
  511. self.addVirtualFolder = function (name, userId) {
  512. if (!name) {
  513. throw new Error("null name");
  514. }
  515. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  516. url += "/" + name;
  517. url = self.getUrl(url);
  518. return self.ajax({
  519. type: "POST",
  520. url: url
  521. });
  522. };
  523. /**
  524. * Renames a virtual folder, within either the default view or a user view
  525. * @param {String} name
  526. */
  527. self.renameVirtualFolder = function (name, newName, userId) {
  528. if (!name) {
  529. throw new Error("null name");
  530. }
  531. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  532. url += "/" + name + "/Name";
  533. url = self.getUrl(url, { newName: newName });
  534. return self.ajax({
  535. type: "POST",
  536. url: url
  537. });
  538. };
  539. /**
  540. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  541. * @param {String} name
  542. */
  543. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  544. if (!virtualFolderName) {
  545. throw new Error("null virtualFolderName");
  546. }
  547. if (!mediaPath) {
  548. throw new Error("null mediaPath");
  549. }
  550. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  551. url += "/" + virtualFolderName + "/Paths";
  552. url = self.getUrl(url, { path: mediaPath });
  553. return self.ajax({
  554. type: "POST",
  555. url: url
  556. });
  557. };
  558. /**
  559. * Removes a media path from a virtual folder, within either the default view or a user view
  560. * @param {String} name
  561. */
  562. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  563. if (!virtualFolderName) {
  564. throw new Error("null virtualFolderName");
  565. }
  566. if (!mediaPath) {
  567. throw new Error("null mediaPath");
  568. }
  569. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  570. url += "/" + virtualFolderName + "/Paths";
  571. url = self.getUrl(url, { path: mediaPath });
  572. return self.ajax({
  573. type: "DELETE",
  574. url: url
  575. });
  576. };
  577. /**
  578. * Deletes a user
  579. * @param {String} id
  580. */
  581. self.deleteUser = function (id) {
  582. if (!id) {
  583. throw new Error("null id");
  584. }
  585. var url = self.getUrl("Users/" + id);
  586. return self.ajax({
  587. type: "DELETE",
  588. url: url
  589. });
  590. };
  591. /**
  592. * Deletes a user image
  593. * @param {String} userId
  594. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  595. */
  596. self.deleteUserImage = function (userId, imageType, imageIndex) {
  597. if (!userId) {
  598. throw new Error("null userId");
  599. }
  600. if (!imageType) {
  601. throw new Error("null imageType");
  602. }
  603. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  604. if (imageIndex != null) {
  605. url += "/" + imageIndex;
  606. }
  607. return self.ajax({
  608. type: "DELETE",
  609. url: url
  610. });
  611. };
  612. self.deleteItemImage = function (itemId, imageType, imageIndex) {
  613. if (!itemId) {
  614. throw new Error("null itemId");
  615. }
  616. if (!imageType) {
  617. throw new Error("null imageType");
  618. }
  619. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  620. if (imageIndex != null) {
  621. url += "/" + imageIndex;
  622. }
  623. return self.ajax({
  624. type: "DELETE",
  625. url: url
  626. });
  627. };
  628. self.updateItemImageIndex = function (itemId, imageType, imageIndex, newIndex) {
  629. if (!itemId) {
  630. throw new Error("null itemId");
  631. }
  632. if (!imageType) {
  633. throw new Error("null imageType");
  634. }
  635. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", { newIndex: newIndex });
  636. return self.ajax({
  637. type: "POST",
  638. url: url
  639. });
  640. };
  641. self.getItemImageInfos = function (itemId) {
  642. if (!itemId) {
  643. throw new Error("null itemId");
  644. }
  645. var url = self.getUrl("Items/" + itemId + "/Images");
  646. return self.ajax({
  647. type: "GET",
  648. url: url,
  649. dataType: "json"
  650. });
  651. };
  652. self.getCriticReviews = function (itemId, options) {
  653. if (!itemId) {
  654. throw new Error("null itemId");
  655. }
  656. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  657. return self.ajax({
  658. type: "GET",
  659. url: url,
  660. dataType: "json"
  661. });
  662. };
  663. self.getSessions = function (options) {
  664. var url = self.getUrl("Sessions", options);
  665. return self.ajax({
  666. type: "GET",
  667. url: url,
  668. dataType: "json"
  669. });
  670. };
  671. /**
  672. * Uploads a user image
  673. * @param {String} userId
  674. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  675. * @param {Object} file The file from the input element
  676. */
  677. self.uploadUserImage = function (userId, imageType, file) {
  678. if (!userId) {
  679. throw new Error("null userId");
  680. }
  681. if (!imageType) {
  682. throw new Error("null imageType");
  683. }
  684. if (!file) {
  685. throw new Error("File must be an image.");
  686. }
  687. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  688. throw new Error("File must be an image.");
  689. }
  690. var deferred = $.Deferred();
  691. var reader = new FileReader();
  692. reader.onerror = function () {
  693. deferred.reject();
  694. };
  695. reader.onabort = function () {
  696. deferred.reject();
  697. };
  698. // Closure to capture the file information.
  699. reader.onload = function (e) {
  700. var data = window.btoa(e.target.result);
  701. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  702. self.ajax({
  703. type: "POST",
  704. url: url,
  705. data: data,
  706. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  707. }).done(function (result) {
  708. deferred.resolveWith(null, [result]);
  709. }).fail(function () {
  710. deferred.reject();
  711. });
  712. };
  713. // Read in the image file as a data URL.
  714. reader.readAsBinaryString(file);
  715. return deferred.promise();
  716. };
  717. self.uploadItemImage = function (itemId, imageType, file) {
  718. if (!itemId) {
  719. throw new Error("null itemId");
  720. }
  721. if (!imageType) {
  722. throw new Error("null imageType");
  723. }
  724. if (!file) {
  725. throw new Error("File must be an image.");
  726. }
  727. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  728. throw new Error("File must be an image.");
  729. }
  730. var deferred = $.Deferred();
  731. var reader = new FileReader();
  732. reader.onerror = function () {
  733. deferred.reject();
  734. };
  735. reader.onabort = function () {
  736. deferred.reject();
  737. };
  738. // Closure to capture the file information.
  739. reader.onload = function (e) {
  740. var data = window.btoa(e.target.result);
  741. var url = self.getUrl("Items/" + itemId + "/Images/" + imageType);
  742. self.ajax({
  743. type: "POST",
  744. url: url,
  745. data: data,
  746. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  747. }).done(function (result) {
  748. deferred.resolveWith(null, [result]);
  749. }).fail(function () {
  750. deferred.reject();
  751. });
  752. };
  753. // Read in the image file as a data URL.
  754. reader.readAsBinaryString(file);
  755. return deferred.promise();
  756. };
  757. /**
  758. * Gets the list of installed plugins on the server
  759. */
  760. self.getInstalledPlugins = function () {
  761. var url = self.getUrl("Plugins");
  762. return self.ajax({
  763. type: "GET",
  764. url: url,
  765. dataType: "json"
  766. });
  767. };
  768. /**
  769. * Gets a user by id
  770. * @param {String} id
  771. */
  772. self.getUser = function (id) {
  773. if (!id) {
  774. throw new Error("Must supply a userId");
  775. }
  776. var url = self.getUrl("Users/" + id);
  777. return self.ajax({
  778. type: "GET",
  779. url: url,
  780. dataType: "json"
  781. });
  782. };
  783. /**
  784. * Gets a studio
  785. */
  786. self.getStudio = function (name, userId) {
  787. if (!name) {
  788. throw new Error("null name");
  789. }
  790. var options = {};
  791. if (userId) {
  792. options.userId = userId;
  793. }
  794. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  795. return self.ajax({
  796. type: "GET",
  797. url: url,
  798. dataType: "json"
  799. });
  800. };
  801. /**
  802. * Gets a genre
  803. */
  804. self.getGenre = function (name, userId) {
  805. if (!name) {
  806. throw new Error("null name");
  807. }
  808. var options = {};
  809. if (userId) {
  810. options.userId = userId;
  811. }
  812. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  813. return self.ajax({
  814. type: "GET",
  815. url: url,
  816. dataType: "json"
  817. });
  818. };
  819. /**
  820. * Gets an artist
  821. */
  822. self.getArtist = function (name, userId) {
  823. if (!name) {
  824. throw new Error("null name");
  825. }
  826. var options = {};
  827. if (userId) {
  828. options.userId = userId;
  829. }
  830. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  831. return self.ajax({
  832. type: "GET",
  833. url: url,
  834. dataType: "json"
  835. });
  836. };
  837. /**
  838. * Gets a year
  839. */
  840. self.getYear = function (yea, userId) {
  841. if (!name) {
  842. throw new Error("null name");
  843. }
  844. var options = {};
  845. if (userId) {
  846. options.userId = userId;
  847. }
  848. var url = self.getUrl("Years/" + self.encodeName(name), options);
  849. return self.ajax({
  850. type: "GET",
  851. url: url,
  852. dataType: "json"
  853. });
  854. };
  855. /**
  856. * Gets a Person
  857. */
  858. self.getPerson = function (name, userId) {
  859. if (!name) {
  860. throw new Error("null name");
  861. }
  862. var options = {};
  863. if (userId) {
  864. options.userId = userId;
  865. }
  866. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  867. return self.ajax({
  868. type: "GET",
  869. url: url,
  870. dataType: "json"
  871. });
  872. };
  873. /**
  874. * Gets weather info
  875. * @param {String} location - us zip code / city, state, country / city, country
  876. * Omit location to get weather info using stored server configuration value
  877. */
  878. self.getWeatherInfo = function (location) {
  879. var url = self.getUrl("weather", {
  880. location: location
  881. });
  882. return self.ajax({
  883. type: "GET",
  884. url: url,
  885. dataType: "json"
  886. });
  887. };
  888. /**
  889. * Gets all users from the server
  890. */
  891. self.getUsers = function () {
  892. var url = self.getUrl("users");
  893. return self.ajax({
  894. type: "GET",
  895. url: url,
  896. dataType: "json"
  897. });
  898. };
  899. /**
  900. * Gets all available parental ratings from the server
  901. */
  902. self.getParentalRatings = function () {
  903. var url = self.getUrl("Localization/ParentalRatings");
  904. return self.ajax({
  905. type: "GET",
  906. url: url,
  907. dataType: "json"
  908. });
  909. };
  910. /**
  911. * Gets a list of all available conrete BaseItem types from the server
  912. */
  913. self.getItemTypes = function (options) {
  914. var url = self.getUrl("Library/ItemTypes", options);
  915. return self.ajax({
  916. type: "GET",
  917. url: url,
  918. dataType: "json"
  919. });
  920. };
  921. /**
  922. * Constructs a url for a user image
  923. * @param {String} userId
  924. * @param {Object} options
  925. * Options supports the following properties:
  926. * width - download the image at a fixed width
  927. * height - download the image at a fixed height
  928. * maxWidth - download the image at a maxWidth
  929. * maxHeight - download the image at a maxHeight
  930. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  931. * For best results do not specify both width and height together, as aspect ratio might be altered.
  932. */
  933. self.getUserImageUrl = function (userId, options) {
  934. if (!userId) {
  935. throw new Error("null userId");
  936. }
  937. options = options || {
  938. };
  939. var url = "Users/" + userId + "/Images/" + options.type;
  940. if (options.index != null) {
  941. url += "/" + options.index;
  942. }
  943. // Don't put these on the query string
  944. delete options.type;
  945. delete options.index;
  946. return self.getUrl(url, options);
  947. };
  948. /**
  949. * Constructs a url for a person image
  950. * @param {String} name
  951. * @param {Object} options
  952. * Options supports the following properties:
  953. * width - download the image at a fixed width
  954. * height - download the image at a fixed height
  955. * maxWidth - download the image at a maxWidth
  956. * maxHeight - download the image at a maxHeight
  957. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  958. * For best results do not specify both width and height together, as aspect ratio might be altered.
  959. */
  960. self.getPersonImageUrl = function (name, options) {
  961. if (!name) {
  962. throw new Error("null name");
  963. }
  964. options = options || {
  965. };
  966. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  967. if (options.index != null) {
  968. url += "/" + options.index;
  969. }
  970. // Don't put these on the query string
  971. delete options.type;
  972. delete options.index;
  973. return self.getUrl(url, options);
  974. };
  975. /**
  976. * Constructs a url for a year image
  977. * @param {String} year
  978. * @param {Object} options
  979. * Options supports the following properties:
  980. * width - download the image at a fixed width
  981. * height - download the image at a fixed height
  982. * maxWidth - download the image at a maxWidth
  983. * maxHeight - download the image at a maxHeight
  984. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  985. * For best results do not specify both width and height together, as aspect ratio might be altered.
  986. */
  987. self.getYearImageUrl = function (year, options) {
  988. if (!year) {
  989. throw new Error("null year");
  990. }
  991. options = options || {
  992. };
  993. var url = "Years/" + year + "/Images/" + options.type;
  994. if (options.index != null) {
  995. url += "/" + options.index;
  996. }
  997. // Don't put these on the query string
  998. delete options.type;
  999. delete options.index;
  1000. return self.getUrl(url, options);
  1001. };
  1002. /**
  1003. * Constructs a url for a genre image
  1004. * @param {String} name
  1005. * @param {Object} options
  1006. * Options supports the following properties:
  1007. * width - download the image at a fixed width
  1008. * height - download the image at a fixed height
  1009. * maxWidth - download the image at a maxWidth
  1010. * maxHeight - download the image at a maxHeight
  1011. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1012. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1013. */
  1014. self.getGenreImageUrl = function (name, options) {
  1015. if (!name) {
  1016. throw new Error("null name");
  1017. }
  1018. options = options || {
  1019. };
  1020. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1021. if (options.index != null) {
  1022. url += "/" + options.index;
  1023. }
  1024. // Don't put these on the query string
  1025. delete options.type;
  1026. delete options.index;
  1027. return self.getUrl(url, options);
  1028. };
  1029. /**
  1030. * Constructs a url for a artist image
  1031. * @param {String} name
  1032. * @param {Object} options
  1033. * Options supports the following properties:
  1034. * width - download the image at a fixed width
  1035. * height - download the image at a fixed height
  1036. * maxWidth - download the image at a maxWidth
  1037. * maxHeight - download the image at a maxHeight
  1038. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1039. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1040. */
  1041. self.getArtistImageUrl = function (name, options) {
  1042. if (!name) {
  1043. throw new Error("null name");
  1044. }
  1045. options = options || {
  1046. };
  1047. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1048. if (options.index != null) {
  1049. url += "/" + options.index;
  1050. }
  1051. // Don't put these on the query string
  1052. delete options.type;
  1053. delete options.index;
  1054. return self.getUrl(url, options);
  1055. };
  1056. /**
  1057. * Constructs a url for a studio image
  1058. * @param {String} name
  1059. * @param {Object} options
  1060. * Options supports the following properties:
  1061. * width - download the image at a fixed width
  1062. * height - download the image at a fixed height
  1063. * maxWidth - download the image at a maxWidth
  1064. * maxHeight - download the image at a maxHeight
  1065. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1066. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1067. */
  1068. self.getStudioImageUrl = function (name, options) {
  1069. if (!name) {
  1070. throw new Error("null name");
  1071. }
  1072. options = options || {
  1073. };
  1074. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1075. if (options.index != null) {
  1076. url += "/" + options.index;
  1077. }
  1078. // Don't put these on the query string
  1079. delete options.type;
  1080. delete options.index;
  1081. return self.getUrl(url, options);
  1082. };
  1083. /**
  1084. * Constructs a url for an item image
  1085. * @param {String} itemId
  1086. * @param {Object} options
  1087. * Options supports the following properties:
  1088. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1089. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1090. * width - download the image at a fixed width
  1091. * height - download the image at a fixed height
  1092. * maxWidth - download the image at a maxWidth
  1093. * maxHeight - download the image at a maxHeight
  1094. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1095. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1096. */
  1097. self.getImageUrl = function (itemId, options) {
  1098. if (!itemId) {
  1099. throw new Error("itemId cannot be empty");
  1100. }
  1101. options = options || {
  1102. };
  1103. var url = "Items/" + itemId + "/Images/" + options.type;
  1104. if (options.index != null) {
  1105. url += "/" + options.index;
  1106. }
  1107. // Don't put these on the query string
  1108. delete options.type;
  1109. delete options.index;
  1110. return self.getUrl(url, options);
  1111. };
  1112. /**
  1113. * Constructs a url for an item logo image
  1114. * If the item doesn't have a logo, it will inherit a logo from a parent
  1115. * @param {Object} item A BaseItem
  1116. * @param {Object} options
  1117. * Options supports the following properties:
  1118. * width - download the image at a fixed width
  1119. * height - download the image at a fixed height
  1120. * maxWidth - download the image at a maxWidth
  1121. * maxHeight - download the image at a maxHeight
  1122. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1123. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1124. */
  1125. self.getLogoImageUrl = function (item, options) {
  1126. if (!item) {
  1127. throw new Error("null item");
  1128. }
  1129. options = options || {
  1130. };
  1131. options.imageType = "logo";
  1132. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1133. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1134. };
  1135. /**
  1136. * Constructs an array of backdrop image url's for an item
  1137. * If the item doesn't have any backdrops, it will inherit them from a parent
  1138. * @param {Object} item A BaseItem
  1139. * @param {Object} options
  1140. * Options supports the following properties:
  1141. * width - download the image at a fixed width
  1142. * height - download the image at a fixed height
  1143. * maxWidth - download the image at a maxWidth
  1144. * maxHeight - download the image at a maxHeight
  1145. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1146. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1147. */
  1148. self.getBackdropImageUrl = function (item, options) {
  1149. if (!item) {
  1150. throw new Error("null item");
  1151. }
  1152. options = options || {
  1153. };
  1154. options.imageType = "backdrop";
  1155. var backdropItemId;
  1156. var backdropCount;
  1157. if (!item.BackdropCount) {
  1158. backdropItemId = item.ParentBackdropItemId;
  1159. backdropCount = item.ParentBackdropCount || 0;
  1160. } else {
  1161. backdropItemId = item.Id;
  1162. backdropCount = item.BackdropCount;
  1163. }
  1164. if (!backdropItemId) {
  1165. return [];
  1166. }
  1167. var files = [];
  1168. for (var i = 0; i < backdropCount; i++) {
  1169. options.imageIndex = i;
  1170. files[i] = self.getImageUrl(backdropItemId, options);
  1171. }
  1172. return files;
  1173. };
  1174. /**
  1175. * Authenticates a user
  1176. * @param {String} userId
  1177. * @param {String} password
  1178. */
  1179. self.authenticateUser = function (userId, password) {
  1180. if (!userId) {
  1181. throw new Error("null userId");
  1182. }
  1183. var url = self.getUrl("Users/" + userId + "/authenticate");
  1184. var postData = {
  1185. password: MediaBrowser.SHA1(password || "")
  1186. };
  1187. return self.ajax({
  1188. type: "POST",
  1189. url: url,
  1190. data: JSON.stringify(postData),
  1191. contentType: "application/json"
  1192. });
  1193. };
  1194. /**
  1195. * Updates a user's password
  1196. * @param {String} userId
  1197. * @param {String} currentPassword
  1198. * @param {String} newPassword
  1199. */
  1200. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1201. if (!userId) {
  1202. throw new Error("null userId");
  1203. }
  1204. var url = self.getUrl("Users/" + userId + "/Password");
  1205. var postData = {
  1206. };
  1207. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1208. if (newPassword) {
  1209. postData.newPassword = newPassword;
  1210. }
  1211. return self.ajax({
  1212. type: "POST",
  1213. url: url,
  1214. data: postData
  1215. });
  1216. };
  1217. /**
  1218. * Resets a user's password
  1219. * @param {String} userId
  1220. */
  1221. self.resetUserPassword = function (userId) {
  1222. if (!userId) {
  1223. throw new Error("null userId");
  1224. }
  1225. var url = self.getUrl("Users/" + userId + "/Password");
  1226. var postData = {
  1227. };
  1228. postData.resetPassword = true;
  1229. return self.ajax({
  1230. type: "POST",
  1231. url: url,
  1232. data: postData
  1233. });
  1234. };
  1235. /**
  1236. * Updates the server's configuration
  1237. * @param {Object} configuration
  1238. */
  1239. self.updateServerConfiguration = function (configuration) {
  1240. if (!configuration) {
  1241. throw new Error("null configuration");
  1242. }
  1243. var url = self.getUrl("System/Configuration");
  1244. return self.ajax({
  1245. type: "POST",
  1246. url: url,
  1247. data: JSON.stringify(configuration),
  1248. contentType: "application/json"
  1249. });
  1250. };
  1251. /**
  1252. * Updates plugin security info
  1253. */
  1254. self.updatePluginSecurityInfo = function (info) {
  1255. var url = self.getUrl("Plugins/SecurityInfo");
  1256. return self.ajax({
  1257. type: "POST",
  1258. url: url,
  1259. data: JSON.stringify(info),
  1260. contentType: "application/json"
  1261. });
  1262. };
  1263. /**
  1264. * Creates a user
  1265. * @param {Object} user
  1266. */
  1267. self.createUser = function (user) {
  1268. if (!user) {
  1269. throw new Error("null user");
  1270. }
  1271. var url = self.getUrl("Users");
  1272. return self.ajax({
  1273. type: "POST",
  1274. url: url,
  1275. data: JSON.stringify(user),
  1276. dataType: "json",
  1277. contentType: "application/json"
  1278. });
  1279. };
  1280. /**
  1281. * Updates a user
  1282. * @param {Object} user
  1283. */
  1284. self.updateUser = function (user) {
  1285. if (!user) {
  1286. throw new Error("null user");
  1287. }
  1288. var url = self.getUrl("Users/" + user.Id);
  1289. return self.ajax({
  1290. type: "POST",
  1291. url: url,
  1292. data: JSON.stringify(user),
  1293. contentType: "application/json"
  1294. });
  1295. };
  1296. /**
  1297. * Updates the Triggers for a ScheduledTask
  1298. * @param {String} id
  1299. * @param {Object} triggers
  1300. */
  1301. self.updateScheduledTaskTriggers = function (id, triggers) {
  1302. if (!id) {
  1303. throw new Error("null id");
  1304. }
  1305. if (!triggers) {
  1306. throw new Error("null triggers");
  1307. }
  1308. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1309. return self.ajax({
  1310. type: "POST",
  1311. url: url,
  1312. data: JSON.stringify(triggers),
  1313. contentType: "application/json"
  1314. });
  1315. };
  1316. /**
  1317. * Updates a plugin's configuration
  1318. * @param {String} Id
  1319. * @param {Object} configuration
  1320. */
  1321. self.updatePluginConfiguration = function (id, configuration) {
  1322. if (!id) {
  1323. throw new Error("null Id");
  1324. }
  1325. if (!configuration) {
  1326. throw new Error("null configuration");
  1327. }
  1328. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1329. return self.ajax({
  1330. type: "POST",
  1331. url: url,
  1332. data: JSON.stringify(configuration),
  1333. contentType: "application/json"
  1334. });
  1335. };
  1336. /**
  1337. * Gets items based on a query, typically for children of a folder
  1338. * @param {String} userId
  1339. * @param {Object} options
  1340. * Options accepts the following properties:
  1341. * itemId - Localize the search to a specific folder (root if omitted)
  1342. * startIndex - Use for paging
  1343. * limit - Use to limit results to a certain number of items
  1344. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1345. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1346. * sortOrder - ascending/descending
  1347. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1348. * index - the name of the dynamic, localized index function
  1349. * dynamicSortBy - the name of the dynamic localized sort function
  1350. * recursive - Whether or not the query should be recursive
  1351. * searchTerm - search term to use as a filter
  1352. */
  1353. self.getItems = function (userId, options) {
  1354. if (!userId) {
  1355. throw new Error("null userId");
  1356. }
  1357. var url = self.getUrl("Users/" + userId + "/Items", options);
  1358. return self.ajax({
  1359. type: "GET",
  1360. url: url,
  1361. dataType: "json"
  1362. });
  1363. };
  1364. /**
  1365. Gets artists from an item
  1366. */
  1367. self.getArtists = function (userId, options) {
  1368. if (!userId) {
  1369. throw new Error("null userId");
  1370. }
  1371. options = options || {};
  1372. options.userId = userId;
  1373. var url = self.getUrl("Artists", options);
  1374. return self.ajax({
  1375. type: "GET",
  1376. url: url,
  1377. dataType: "json"
  1378. });
  1379. };
  1380. /**
  1381. Gets genres from an item
  1382. */
  1383. self.getGenres = function (userId, options) {
  1384. if (!userId) {
  1385. throw new Error("null userId");
  1386. }
  1387. options = options || {};
  1388. options.userId = userId;
  1389. var url = self.getUrl("Genres", options);
  1390. return self.ajax({
  1391. type: "GET",
  1392. url: url,
  1393. dataType: "json"
  1394. });
  1395. };
  1396. /**
  1397. Gets people from an item
  1398. */
  1399. self.getPeople = function (userId, options) {
  1400. if (!userId) {
  1401. throw new Error("null userId");
  1402. }
  1403. options = options || {};
  1404. options.userId = userId;
  1405. var url = self.getUrl("Persons", options);
  1406. return self.ajax({
  1407. type: "GET",
  1408. url: url,
  1409. dataType: "json"
  1410. });
  1411. };
  1412. /**
  1413. Gets studios from an item
  1414. */
  1415. self.getStudios = function (userId, options) {
  1416. if (!userId) {
  1417. throw new Error("null userId");
  1418. }
  1419. options = options || {};
  1420. options.userId = userId;
  1421. var url = self.getUrl("Studios", options);
  1422. return self.ajax({
  1423. type: "GET",
  1424. url: url,
  1425. dataType: "json"
  1426. });
  1427. };
  1428. /**
  1429. * Gets local trailers for an item
  1430. */
  1431. self.getLocalTrailers = function (userId, itemId) {
  1432. if (!userId) {
  1433. throw new Error("null userId");
  1434. }
  1435. if (!itemId) {
  1436. throw new Error("null itemId");
  1437. }
  1438. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1439. return self.ajax({
  1440. type: "GET",
  1441. url: url,
  1442. dataType: "json"
  1443. });
  1444. };
  1445. /**
  1446. * Gets theme songs for an item
  1447. */
  1448. self.getThemeSongs = function (userId, itemId) {
  1449. if (!userId) {
  1450. throw new Error("null userId");
  1451. }
  1452. if (!itemId) {
  1453. throw new Error("null itemId");
  1454. }
  1455. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/ThemeSongs");
  1456. return self.ajax({
  1457. type: "GET",
  1458. url: url,
  1459. dataType: "json"
  1460. });
  1461. };
  1462. self.getThemeVideos = function (userId, itemId) {
  1463. if (!userId) {
  1464. throw new Error("null userId");
  1465. }
  1466. if (!itemId) {
  1467. throw new Error("null itemId");
  1468. }
  1469. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/ThemeVideos");
  1470. return self.ajax({
  1471. type: "GET",
  1472. url: url,
  1473. dataType: "json"
  1474. });
  1475. };
  1476. self.getSearchHints = function (options) {
  1477. var url = self.getUrl("Search/Hints", options);
  1478. return self.ajax({
  1479. type: "GET",
  1480. url: url,
  1481. dataType: "json"
  1482. });
  1483. };
  1484. /**
  1485. * Gets special features for an item
  1486. */
  1487. self.getSpecialFeatures = function (userId, itemId) {
  1488. if (!userId) {
  1489. throw new Error("null userId");
  1490. }
  1491. if (!itemId) {
  1492. throw new Error("null itemId");
  1493. }
  1494. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  1495. return self.ajax({
  1496. type: "GET",
  1497. url: url,
  1498. dataType: "json"
  1499. });
  1500. };
  1501. /**
  1502. * Marks an item as played or unplayed
  1503. * This should not be used to update playstate following playback.
  1504. * There are separate playstate check-in methods for that. This should be used for a
  1505. * separate option to reset playstate.
  1506. * @param {String} userId
  1507. * @param {String} itemId
  1508. * @param {Boolean} wasPlayed
  1509. */
  1510. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  1511. if (!userId) {
  1512. throw new Error("null userId");
  1513. }
  1514. if (!itemId) {
  1515. throw new Error("null itemId");
  1516. }
  1517. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  1518. var method = wasPlayed ? "POST" : "DELETE";
  1519. return self.ajax({
  1520. type: method,
  1521. url: url
  1522. });
  1523. };
  1524. /**
  1525. * Updates a user's favorite status for an item.
  1526. * @param {String} userId
  1527. * @param {String} itemId
  1528. * @param {Boolean} isFavorite
  1529. */
  1530. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  1531. if (!userId) {
  1532. throw new Error("null userId");
  1533. }
  1534. if (!itemId) {
  1535. throw new Error("null itemId");
  1536. }
  1537. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  1538. var method = isFavorite ? "POST" : "DELETE";
  1539. return self.ajax({
  1540. type: method,
  1541. url: url
  1542. });
  1543. };
  1544. /**
  1545. * Updates a user's personal rating for an item
  1546. * @param {String} userId
  1547. * @param {String} itemId
  1548. * @param {Boolean} likes
  1549. */
  1550. self.updateUserItemRating = function (userId, itemId, likes) {
  1551. if (!userId) {
  1552. throw new Error("null userId");
  1553. }
  1554. if (!itemId) {
  1555. throw new Error("null itemId");
  1556. }
  1557. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  1558. likes: likes
  1559. });
  1560. return self.ajax({
  1561. type: "POST",
  1562. url: url
  1563. });
  1564. };
  1565. /**
  1566. * Updates a user's favorite status for an item by name.
  1567. * @param {String} userId
  1568. * @param {String} name
  1569. * @param {Boolean} isFavorite
  1570. */
  1571. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  1572. if (!userId) {
  1573. throw new Error("null userId");
  1574. }
  1575. if (!name) {
  1576. throw new Error("null name");
  1577. }
  1578. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  1579. var method = isFavorite ? "POST" : "DELETE";
  1580. return self.ajax({
  1581. type: method,
  1582. url: url
  1583. });
  1584. };
  1585. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  1586. if (!userId) {
  1587. throw new Error("null userId");
  1588. }
  1589. if (!name) {
  1590. throw new Error("null name");
  1591. }
  1592. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  1593. var method = isFavorite ? "POST" : "DELETE";
  1594. return self.ajax({
  1595. type: method,
  1596. url: url
  1597. });
  1598. };
  1599. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  1600. if (!userId) {
  1601. throw new Error("null userId");
  1602. }
  1603. if (!name) {
  1604. throw new Error("null name");
  1605. }
  1606. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  1607. var method = isFavorite ? "POST" : "DELETE";
  1608. return self.ajax({
  1609. type: method,
  1610. url: url
  1611. });
  1612. };
  1613. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  1614. if (!userId) {
  1615. throw new Error("null userId");
  1616. }
  1617. if (!name) {
  1618. throw new Error("null name");
  1619. }
  1620. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  1621. var method = isFavorite ? "POST" : "DELETE";
  1622. return self.ajax({
  1623. type: method,
  1624. url: url
  1625. });
  1626. };
  1627. /**
  1628. * Updates a user's rating for an item by name.
  1629. * @param {String} userId
  1630. * @param {String} name
  1631. * @param {Boolean} likes
  1632. */
  1633. self.updateArtistRating = function (userId, name, likes) {
  1634. if (!userId) {
  1635. throw new Error("null userId");
  1636. }
  1637. if (!name) {
  1638. throw new Error("null name");
  1639. }
  1640. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  1641. likes: likes
  1642. });
  1643. return self.ajax({
  1644. type: "POST",
  1645. url: url
  1646. });
  1647. };
  1648. self.updatePersonRating = function (userId, name, likes) {
  1649. if (!userId) {
  1650. throw new Error("null userId");
  1651. }
  1652. if (!name) {
  1653. throw new Error("null name");
  1654. }
  1655. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  1656. likes: likes
  1657. });
  1658. return self.ajax({
  1659. type: "POST",
  1660. url: url
  1661. });
  1662. };
  1663. self.updateStudioRating = function (userId, name, likes) {
  1664. if (!userId) {
  1665. throw new Error("null userId");
  1666. }
  1667. if (!name) {
  1668. throw new Error("null name");
  1669. }
  1670. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  1671. likes: likes
  1672. });
  1673. return self.ajax({
  1674. type: "POST",
  1675. url: url
  1676. });
  1677. };
  1678. self.updateGenreRating = function (userId, name, likes) {
  1679. if (!userId) {
  1680. throw new Error("null userId");
  1681. }
  1682. if (!name) {
  1683. throw new Error("null name");
  1684. }
  1685. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  1686. likes: likes
  1687. });
  1688. return self.ajax({
  1689. type: "POST",
  1690. url: url
  1691. });
  1692. };
  1693. /**
  1694. * Clears a user's rating for an item by name.
  1695. * @param {String} userId
  1696. * @param {String} name
  1697. */
  1698. self.clearArtistRating = function (userId, name) {
  1699. if (!userId) {
  1700. throw new Error("null userId");
  1701. }
  1702. if (!name) {
  1703. throw new Error("null name");
  1704. }
  1705. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  1706. return self.ajax({
  1707. type: "DELETE",
  1708. url: url
  1709. });
  1710. };
  1711. self.clearPersonRating = function (userId, name) {
  1712. if (!userId) {
  1713. throw new Error("null userId");
  1714. }
  1715. if (!name) {
  1716. throw new Error("null name");
  1717. }
  1718. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  1719. return self.ajax({
  1720. type: "DELETE",
  1721. url: url
  1722. });
  1723. };
  1724. self.clearStudioRating = function (userId, name) {
  1725. if (!userId) {
  1726. throw new Error("null userId");
  1727. }
  1728. if (!name) {
  1729. throw new Error("null name");
  1730. }
  1731. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  1732. return self.ajax({
  1733. type: "DELETE",
  1734. url: url
  1735. });
  1736. };
  1737. self.clearGenreRating = function (userId, name) {
  1738. if (!userId) {
  1739. throw new Error("null userId");
  1740. }
  1741. if (!name) {
  1742. throw new Error("null name");
  1743. }
  1744. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  1745. return self.ajax({
  1746. type: "DELETE",
  1747. url: url
  1748. });
  1749. };
  1750. self.getItemCounts = function (userId) {
  1751. var options = {};
  1752. if (userId) {
  1753. options.userId = userId;
  1754. }
  1755. var url = self.getUrl("Items/Counts", options);
  1756. return self.ajax({
  1757. type: "GET",
  1758. url: url,
  1759. dataType: "json"
  1760. });
  1761. };
  1762. /**
  1763. Gets a variety of item counts that a person appears in
  1764. */
  1765. self.getPersonItemCounts = function (userId, name) {
  1766. if (!userId) {
  1767. throw new Error("null userId");
  1768. }
  1769. if (!name) {
  1770. throw new Error("null name");
  1771. }
  1772. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  1773. userId: userId
  1774. });
  1775. return self.ajax({
  1776. type: "GET",
  1777. url: url,
  1778. dataType: "json"
  1779. });
  1780. };
  1781. /**
  1782. Gets a variety of item counts that a genre appears in
  1783. */
  1784. self.getGenreItemCounts = function (userId, name) {
  1785. if (!userId) {
  1786. throw new Error("null userId");
  1787. }
  1788. if (!name) {
  1789. throw new Error("null name");
  1790. }
  1791. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  1792. userId: userId
  1793. });
  1794. return self.ajax({
  1795. type: "GET",
  1796. url: url,
  1797. dataType: "json"
  1798. });
  1799. };
  1800. /**
  1801. Gets a variety of item counts that an artist appears in
  1802. */
  1803. self.getArtistItemCounts = function (userId, name) {
  1804. if (!userId) {
  1805. throw new Error("null userId");
  1806. }
  1807. if (!name) {
  1808. throw new Error("null name");
  1809. }
  1810. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  1811. userId: userId
  1812. });
  1813. return self.ajax({
  1814. type: "GET",
  1815. url: url,
  1816. dataType: "json"
  1817. });
  1818. };
  1819. /**
  1820. Gets a variety of item counts that a studio appears in
  1821. */
  1822. self.getStudioItemCounts = function (userId, name) {
  1823. if (!userId) {
  1824. throw new Error("null userId");
  1825. }
  1826. if (!name) {
  1827. throw new Error("null name");
  1828. }
  1829. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  1830. userId: userId
  1831. });
  1832. return self.ajax({
  1833. type: "GET",
  1834. url: url,
  1835. dataType: "json"
  1836. });
  1837. };
  1838. /**
  1839. * Clears a user's personal rating for an item
  1840. * @param {String} userId
  1841. * @param {String} itemId
  1842. */
  1843. self.clearUserItemRating = function (userId, itemId) {
  1844. if (!userId) {
  1845. throw new Error("null userId");
  1846. }
  1847. if (!itemId) {
  1848. throw new Error("null itemId");
  1849. }
  1850. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1851. return self.ajax({
  1852. type: "DELETE",
  1853. url: url
  1854. });
  1855. };
  1856. /**
  1857. * Reports the user has started playing something
  1858. * @param {String} userId
  1859. * @param {String} itemId
  1860. */
  1861. self.reportPlaybackStart = function (userId, itemId) {
  1862. if (!userId) {
  1863. throw new Error("null userId");
  1864. }
  1865. if (!itemId) {
  1866. throw new Error("null itemId");
  1867. }
  1868. if (self.isWebSocketOpen()) {
  1869. var deferred = $.Deferred();
  1870. self.sendWebSocketMessage("PlaybackStart", itemId);
  1871. deferred.resolveWith(null, []);
  1872. return deferred.promise();
  1873. }
  1874. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1875. return self.ajax({
  1876. type: "POST",
  1877. url: url
  1878. });
  1879. };
  1880. /**
  1881. * Reports progress viewing an item
  1882. * @param {String} userId
  1883. * @param {String} itemId
  1884. */
  1885. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  1886. if (!userId) {
  1887. throw new Error("null userId");
  1888. }
  1889. if (!itemId) {
  1890. throw new Error("null itemId");
  1891. }
  1892. if (self.isWebSocketOpen()) {
  1893. var deferred = $.Deferred();
  1894. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  1895. deferred.resolveWith(null, []);
  1896. return deferred.promise();
  1897. }
  1898. var params = {
  1899. };
  1900. if (positionTicks) {
  1901. params.positionTicks = positionTicks;
  1902. }
  1903. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1904. return self.ajax({
  1905. type: "POST",
  1906. url: url
  1907. });
  1908. };
  1909. /**
  1910. * Reports a user has stopped playing an item
  1911. * @param {String} userId
  1912. * @param {String} itemId
  1913. */
  1914. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1915. if (!userId) {
  1916. throw new Error("null userId");
  1917. }
  1918. if (!itemId) {
  1919. throw new Error("null itemId");
  1920. }
  1921. if (self.isWebSocketOpen()) {
  1922. var deferred = $.Deferred();
  1923. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  1924. deferred.resolveWith(null, []);
  1925. return deferred.promise();
  1926. }
  1927. var params = {
  1928. };
  1929. if (positionTicks) {
  1930. params.positionTicks = positionTicks;
  1931. }
  1932. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1933. return self.ajax({
  1934. type: "DELETE",
  1935. url: url
  1936. });
  1937. };
  1938. }
  1939. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  1940. /**
  1941. * Provides a friendly way to create an api client instance using information from the browser's current url
  1942. */
  1943. MediaBrowser.ApiClient.create = function (clientName) {
  1944. var loc = window.location;
  1945. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1946. };
  1947. /**
  1948. *
  1949. * Secure Hash Algorithm (SHA1)
  1950. * http://www.webtoolkit.info/
  1951. *
  1952. **/
  1953. MediaBrowser.SHA1 = function (msg) {
  1954. function rotate_left(n, s) {
  1955. var t4 = (n << s) | (n >>> (32 - s));
  1956. return t4;
  1957. }
  1958. function lsb_hex(val) {
  1959. var str = "";
  1960. var i;
  1961. var vh;
  1962. var vl;
  1963. for (i = 0; i <= 6; i += 2) {
  1964. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1965. vl = (val >>> (i * 4)) & 0x0f;
  1966. str += vh.toString(16) + vl.toString(16);
  1967. }
  1968. return str;
  1969. }
  1970. function cvt_hex(val) {
  1971. var str = "";
  1972. var i;
  1973. var v;
  1974. for (i = 7; i >= 0; i--) {
  1975. v = (val >>> (i * 4)) & 0x0f;
  1976. str += v.toString(16);
  1977. }
  1978. return str;
  1979. }
  1980. function Utf8Encode(string) {
  1981. string = string.replace(/\r\n/g, "\n");
  1982. var utftext = "";
  1983. for (var n = 0; n < string.length; n++) {
  1984. var c = string.charCodeAt(n);
  1985. if (c < 128) {
  1986. utftext += String.fromCharCode(c);
  1987. }
  1988. else if ((c > 127) && (c < 2048)) {
  1989. utftext += String.fromCharCode((c >> 6) | 192);
  1990. utftext += String.fromCharCode((c & 63) | 128);
  1991. }
  1992. else {
  1993. utftext += String.fromCharCode((c >> 12) | 224);
  1994. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1995. utftext += String.fromCharCode((c & 63) | 128);
  1996. }
  1997. }
  1998. return utftext;
  1999. }
  2000. var blockstart;
  2001. var i, j;
  2002. var W = new Array(80);
  2003. var H0 = 0x67452301;
  2004. var H1 = 0xEFCDAB89;
  2005. var H2 = 0x98BADCFE;
  2006. var H3 = 0x10325476;
  2007. var H4 = 0xC3D2E1F0;
  2008. var A, B, C, D, E;
  2009. var temp;
  2010. msg = Utf8Encode(msg);
  2011. var msg_len = msg.length;
  2012. var word_array = new Array();
  2013. for (i = 0; i < msg_len - 3; i += 4) {
  2014. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2015. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2016. word_array.push(j);
  2017. }
  2018. switch (msg_len % 4) {
  2019. case 0:
  2020. i = 0x080000000;
  2021. break;
  2022. case 1:
  2023. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2024. break;
  2025. case 2:
  2026. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2027. break;
  2028. case 3:
  2029. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2030. break;
  2031. }
  2032. word_array.push(i);
  2033. while ((word_array.length % 16) != 14) word_array.push(0);
  2034. word_array.push(msg_len >>> 29);
  2035. word_array.push((msg_len << 3) & 0x0ffffffff);
  2036. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2037. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2038. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2039. A = H0;
  2040. B = H1;
  2041. C = H2;
  2042. D = H3;
  2043. E = H4;
  2044. for (i = 0; i <= 19; i++) {
  2045. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2046. E = D;
  2047. D = C;
  2048. C = rotate_left(B, 30);
  2049. B = A;
  2050. A = temp;
  2051. }
  2052. for (i = 20; i <= 39; i++) {
  2053. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2054. E = D;
  2055. D = C;
  2056. C = rotate_left(B, 30);
  2057. B = A;
  2058. A = temp;
  2059. }
  2060. for (i = 40; i <= 59; i++) {
  2061. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2062. E = D;
  2063. D = C;
  2064. C = rotate_left(B, 30);
  2065. B = A;
  2066. A = temp;
  2067. }
  2068. for (i = 60; i <= 79; i++) {
  2069. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2070. E = D;
  2071. D = C;
  2072. C = rotate_left(B, 30);
  2073. B = A;
  2074. A = temp;
  2075. }
  2076. H0 = (H0 + A) & 0x0ffffffff;
  2077. H1 = (H1 + B) & 0x0ffffffff;
  2078. H2 = (H2 + C) & 0x0ffffffff;
  2079. H3 = (H3 + D) & 0x0ffffffff;
  2080. H4 = (H4 + E) & 0x0ffffffff;
  2081. }
  2082. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2083. return temp.toLowerCase();
  2084. };
  2085. (function (jQuery, window, undefined) {
  2086. "use strict";
  2087. var matched, browser;
  2088. jQuery.uaMatch = function (ua) {
  2089. ua = ua.toLowerCase();
  2090. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2091. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2092. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2093. /(msie) ([\w.]+)/.exec(ua) ||
  2094. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2095. [];
  2096. var platform_match = /(ipad)/.exec(ua) ||
  2097. /(iphone)/.exec(ua) ||
  2098. /(android)/.exec(ua) ||
  2099. [];
  2100. return {
  2101. browser: match[1] || "",
  2102. version: match[2] || "0",
  2103. platform: platform_match[0] || ""
  2104. };
  2105. };
  2106. matched = jQuery.uaMatch(window.navigator.userAgent);
  2107. browser = {};
  2108. if (matched.browser) {
  2109. browser[matched.browser] = true;
  2110. browser.version = matched.version;
  2111. }
  2112. if (matched.platform) {
  2113. browser[matched.platform] = true
  2114. }
  2115. // Chrome is Webkit, but Webkit is also Safari.
  2116. if (browser.chrome) {
  2117. browser.webkit = true;
  2118. } else if (browser.webkit) {
  2119. browser.safari = true;
  2120. }
  2121. jQuery.browser = browser;
  2122. })(jQuery, window);