ApiClient.js 73 KB

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