ApiClient.js 72 KB

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