ApiClient.js 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748
  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. /**
  1751. Gets a variety of item counts that a person appears in
  1752. */
  1753. self.getPersonItemCounts = function (userId, name) {
  1754. if (!userId) {
  1755. throw new Error("null userId");
  1756. }
  1757. if (!name) {
  1758. throw new Error("null name");
  1759. }
  1760. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  1761. userId: userId
  1762. });
  1763. return self.ajax({
  1764. type: "GET",
  1765. url: url,
  1766. dataType: "json"
  1767. });
  1768. };
  1769. /**
  1770. Gets a variety of item counts that a genre appears in
  1771. */
  1772. self.getGenreItemCounts = function (userId, name) {
  1773. if (!userId) {
  1774. throw new Error("null userId");
  1775. }
  1776. if (!name) {
  1777. throw new Error("null name");
  1778. }
  1779. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  1780. userId: userId
  1781. });
  1782. return self.ajax({
  1783. type: "GET",
  1784. url: url,
  1785. dataType: "json"
  1786. });
  1787. };
  1788. /**
  1789. Gets a variety of item counts that an artist appears in
  1790. */
  1791. self.getArtistItemCounts = function (userId, name) {
  1792. if (!userId) {
  1793. throw new Error("null userId");
  1794. }
  1795. if (!name) {
  1796. throw new Error("null name");
  1797. }
  1798. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  1799. userId: userId
  1800. });
  1801. return self.ajax({
  1802. type: "GET",
  1803. url: url,
  1804. dataType: "json"
  1805. });
  1806. };
  1807. /**
  1808. Gets a variety of item counts that a studio appears in
  1809. */
  1810. self.getStudioItemCounts = function (userId, name) {
  1811. if (!userId) {
  1812. throw new Error("null userId");
  1813. }
  1814. if (!name) {
  1815. throw new Error("null name");
  1816. }
  1817. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  1818. userId: userId
  1819. });
  1820. return self.ajax({
  1821. type: "GET",
  1822. url: url,
  1823. dataType: "json"
  1824. });
  1825. };
  1826. /**
  1827. * Clears a user's personal rating for an item
  1828. * @param {String} userId
  1829. * @param {String} itemId
  1830. */
  1831. self.clearUserItemRating = function (userId, itemId) {
  1832. if (!userId) {
  1833. throw new Error("null userId");
  1834. }
  1835. if (!itemId) {
  1836. throw new Error("null itemId");
  1837. }
  1838. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  1839. return self.ajax({
  1840. type: "DELETE",
  1841. url: url
  1842. });
  1843. };
  1844. /**
  1845. * Reports the user has started playing something
  1846. * @param {String} userId
  1847. * @param {String} itemId
  1848. */
  1849. self.reportPlaybackStart = function (userId, itemId) {
  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("PlaybackStart", itemId);
  1859. deferred.resolveWith(null, []);
  1860. return deferred.promise();
  1861. }
  1862. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  1863. return self.ajax({
  1864. type: "POST",
  1865. url: url
  1866. });
  1867. };
  1868. /**
  1869. * Reports progress viewing an item
  1870. * @param {String} userId
  1871. * @param {String} itemId
  1872. */
  1873. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  1874. if (!userId) {
  1875. throw new Error("null userId");
  1876. }
  1877. if (!itemId) {
  1878. throw new Error("null itemId");
  1879. }
  1880. if (self.isWebSocketOpen()) {
  1881. var deferred = $.Deferred();
  1882. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  1883. deferred.resolveWith(null, []);
  1884. return deferred.promise();
  1885. }
  1886. var params = {
  1887. };
  1888. if (positionTicks) {
  1889. params.positionTicks = positionTicks;
  1890. }
  1891. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  1892. return self.ajax({
  1893. type: "POST",
  1894. url: url
  1895. });
  1896. };
  1897. /**
  1898. * Reports a user has stopped playing an item
  1899. * @param {String} userId
  1900. * @param {String} itemId
  1901. */
  1902. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  1903. if (!userId) {
  1904. throw new Error("null userId");
  1905. }
  1906. if (!itemId) {
  1907. throw new Error("null itemId");
  1908. }
  1909. if (self.isWebSocketOpen()) {
  1910. var deferred = $.Deferred();
  1911. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  1912. deferred.resolveWith(null, []);
  1913. return deferred.promise();
  1914. }
  1915. var params = {
  1916. };
  1917. if (positionTicks) {
  1918. params.positionTicks = positionTicks;
  1919. }
  1920. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  1921. return self.ajax({
  1922. type: "DELETE",
  1923. url: url
  1924. });
  1925. };
  1926. }
  1927. }(jQuery, navigator, JSON, window.WebSocket, setTimeout);
  1928. /**
  1929. * Provides a friendly way to create an api client instance using information from the browser's current url
  1930. */
  1931. MediaBrowser.ApiClient.create = function (clientName) {
  1932. var loc = window.location;
  1933. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName);
  1934. };
  1935. /**
  1936. *
  1937. * Secure Hash Algorithm (SHA1)
  1938. * http://www.webtoolkit.info/
  1939. *
  1940. **/
  1941. MediaBrowser.SHA1 = function (msg) {
  1942. function rotate_left(n, s) {
  1943. var t4 = (n << s) | (n >>> (32 - s));
  1944. return t4;
  1945. }
  1946. function lsb_hex(val) {
  1947. var str = "";
  1948. var i;
  1949. var vh;
  1950. var vl;
  1951. for (i = 0; i <= 6; i += 2) {
  1952. vh = (val >>> (i * 4 + 4)) & 0x0f;
  1953. vl = (val >>> (i * 4)) & 0x0f;
  1954. str += vh.toString(16) + vl.toString(16);
  1955. }
  1956. return str;
  1957. }
  1958. function cvt_hex(val) {
  1959. var str = "";
  1960. var i;
  1961. var v;
  1962. for (i = 7; i >= 0; i--) {
  1963. v = (val >>> (i * 4)) & 0x0f;
  1964. str += v.toString(16);
  1965. }
  1966. return str;
  1967. }
  1968. function Utf8Encode(string) {
  1969. string = string.replace(/\r\n/g, "\n");
  1970. var utftext = "";
  1971. for (var n = 0; n < string.length; n++) {
  1972. var c = string.charCodeAt(n);
  1973. if (c < 128) {
  1974. utftext += String.fromCharCode(c);
  1975. }
  1976. else if ((c > 127) && (c < 2048)) {
  1977. utftext += String.fromCharCode((c >> 6) | 192);
  1978. utftext += String.fromCharCode((c & 63) | 128);
  1979. }
  1980. else {
  1981. utftext += String.fromCharCode((c >> 12) | 224);
  1982. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  1983. utftext += String.fromCharCode((c & 63) | 128);
  1984. }
  1985. }
  1986. return utftext;
  1987. }
  1988. var blockstart;
  1989. var i, j;
  1990. var W = new Array(80);
  1991. var H0 = 0x67452301;
  1992. var H1 = 0xEFCDAB89;
  1993. var H2 = 0x98BADCFE;
  1994. var H3 = 0x10325476;
  1995. var H4 = 0xC3D2E1F0;
  1996. var A, B, C, D, E;
  1997. var temp;
  1998. msg = Utf8Encode(msg);
  1999. var msg_len = msg.length;
  2000. var word_array = new Array();
  2001. for (i = 0; i < msg_len - 3; i += 4) {
  2002. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2003. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2004. word_array.push(j);
  2005. }
  2006. switch (msg_len % 4) {
  2007. case 0:
  2008. i = 0x080000000;
  2009. break;
  2010. case 1:
  2011. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2012. break;
  2013. case 2:
  2014. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2015. break;
  2016. case 3:
  2017. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2018. break;
  2019. }
  2020. word_array.push(i);
  2021. while ((word_array.length % 16) != 14) word_array.push(0);
  2022. word_array.push(msg_len >>> 29);
  2023. word_array.push((msg_len << 3) & 0x0ffffffff);
  2024. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2025. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2026. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2027. A = H0;
  2028. B = H1;
  2029. C = H2;
  2030. D = H3;
  2031. E = H4;
  2032. for (i = 0; i <= 19; i++) {
  2033. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2034. E = D;
  2035. D = C;
  2036. C = rotate_left(B, 30);
  2037. B = A;
  2038. A = temp;
  2039. }
  2040. for (i = 20; i <= 39; i++) {
  2041. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2042. E = D;
  2043. D = C;
  2044. C = rotate_left(B, 30);
  2045. B = A;
  2046. A = temp;
  2047. }
  2048. for (i = 40; i <= 59; i++) {
  2049. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2050. E = D;
  2051. D = C;
  2052. C = rotate_left(B, 30);
  2053. B = A;
  2054. A = temp;
  2055. }
  2056. for (i = 60; i <= 79; i++) {
  2057. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2058. E = D;
  2059. D = C;
  2060. C = rotate_left(B, 30);
  2061. B = A;
  2062. A = temp;
  2063. }
  2064. H0 = (H0 + A) & 0x0ffffffff;
  2065. H1 = (H1 + B) & 0x0ffffffff;
  2066. H2 = (H2 + C) & 0x0ffffffff;
  2067. H3 = (H3 + D) & 0x0ffffffff;
  2068. H4 = (H4 + E) & 0x0ffffffff;
  2069. }
  2070. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2071. return temp.toLowerCase();
  2072. };
  2073. (function (jQuery, window, undefined) {
  2074. "use strict";
  2075. var matched, browser;
  2076. jQuery.uaMatch = function (ua) {
  2077. ua = ua.toLowerCase();
  2078. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2079. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2080. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2081. /(msie) ([\w.]+)/.exec(ua) ||
  2082. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2083. [];
  2084. var platform_match = /(ipad)/.exec(ua) ||
  2085. /(iphone)/.exec(ua) ||
  2086. /(android)/.exec(ua) ||
  2087. [];
  2088. return {
  2089. browser: match[1] || "",
  2090. version: match[2] || "0",
  2091. platform: platform_match[0] || ""
  2092. };
  2093. };
  2094. matched = jQuery.uaMatch(window.navigator.userAgent);
  2095. browser = {};
  2096. if (matched.browser) {
  2097. browser[matched.browser] = true;
  2098. browser.version = matched.version;
  2099. }
  2100. if (matched.platform) {
  2101. browser[matched.platform] = true
  2102. }
  2103. // Chrome is Webkit, but Webkit is also Safari.
  2104. if (browser.chrome) {
  2105. browser.webkit = true;
  2106. } else if (browser.webkit) {
  2107. browser.safari = true;
  2108. }
  2109. jQuery.browser = browser;
  2110. })(jQuery, window);