ApiClient.js 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703
  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. * @param {String} applicationVersion
  12. */
  13. return function (serverProtocol, serverHostName, serverPortNumber, clientName, applicationVersion) {
  14. if (!serverProtocol) {
  15. throw new Error("Must supply a serverProtocol, e.g. http:");
  16. }
  17. if (!serverHostName) {
  18. throw new Error("Must supply serverHostName, e.g. 192.168.1.1 or myServerName");
  19. }
  20. if (!serverPortNumber) {
  21. throw new Error("Must supply a serverPortNumber");
  22. }
  23. var self = this;
  24. var deviceName = "Web Browser";
  25. var deviceId = MediaBrowser.SHA1(navigator.userAgent + (navigator.cpuClass || ""));
  26. var currentUserId;
  27. var webSocket;
  28. /**
  29. * Gets the server host name.
  30. */
  31. self.serverHostName = function () {
  32. return serverHostName;
  33. };
  34. /**
  35. * Gets the server port number.
  36. */
  37. self.serverPortNumber = function () {
  38. return serverPortNumber;
  39. };
  40. /**
  41. * Gets or sets the current user id.
  42. */
  43. self.currentUserId = function (val) {
  44. if (val !== undefined) {
  45. currentUserId = val;
  46. } else {
  47. return currentUserId;
  48. }
  49. };
  50. deviceName = (function () {
  51. var name = "";
  52. if ($.browser.chrome) {
  53. name = "Chrome";
  54. }
  55. else if ($.browser.safari) {
  56. name = "Safari";
  57. }
  58. else if ($.browser.webkit) {
  59. name = "WebKit";
  60. }
  61. else if ($.browser.msie) {
  62. name = "Internet Explorer";
  63. }
  64. else if ($.browser.opera) {
  65. name = "Opera";
  66. }
  67. else if ($.browser.firefox || $.browser.mozilla) {
  68. name = "Firefox";
  69. }
  70. if (name) {
  71. if ($.browser.version) {
  72. name += " " + $.browser.version;
  73. }
  74. }
  75. else {
  76. name = "Web Browser";
  77. }
  78. if ($.browser.ipad) {
  79. name += " Ipad";
  80. }
  81. else if ($.browser.iphone) {
  82. name += " Iphone";
  83. }
  84. else if ($.browser.android) {
  85. name += " Android";
  86. }
  87. return name;
  88. }());
  89. self.deviceId = function () {
  90. return deviceId;
  91. };
  92. self.encodeName = function (name) {
  93. name = name.split('/').join('-');
  94. name = name.split('?').join('-');
  95. var val = $.param({ name: name });
  96. return val.substring(val.indexOf('=') + 1).replace("'", '%27');
  97. };
  98. /**
  99. * Wraps around jQuery ajax methods to add additional info to the request.
  100. */
  101. self.ajax = function (request) {
  102. if (!request) {
  103. throw new Error("Request cannot be null");
  104. }
  105. if (clientName) {
  106. var auth = 'MediaBrowser Client="' + clientName + '", Device="' + deviceName + '", DeviceId="' + deviceId + '", Version="' + applicationVersion + '"';
  107. if (currentUserId) {
  108. auth += ', UserId="' + currentUserId + '"';
  109. }
  110. request.headers = {
  111. Authorization: auth
  112. };
  113. }
  114. return $.ajax(request);
  115. };
  116. /**
  117. * Creates an api url based on a handler name and query string parameters
  118. * @param {String} name
  119. * @param {Object} params
  120. */
  121. self.getUrl = function (name, params) {
  122. if (!name) {
  123. throw new Error("Url name cannot be empty");
  124. }
  125. var url = serverProtocol + "//" + serverHostName + ":" + serverPortNumber + "/mediabrowser/" + name;
  126. if (params) {
  127. url += "?" + $.param(params);
  128. }
  129. return url;
  130. };
  131. self.openWebSocket = function (port) {
  132. var url = "ws://" + serverHostName + ":" + port + "/mediabrowser";
  133. webSocket = new WebSocket(url);
  134. webSocket.onmessage = function (msg) {
  135. msg = JSON.parse(msg.data);
  136. $(self).trigger("websocketmessage", [msg]);
  137. };
  138. webSocket.onopen = function () {
  139. setTimeout(function () {
  140. self.sendWebSocketMessage("Identity", clientName + "|" + deviceId + "|" + applicationVersion);
  141. $(self).trigger("websocketopen");
  142. }, 500);
  143. };
  144. webSocket.onerror = function () {
  145. setTimeout(function () {
  146. $(self).trigger("websocketerror");
  147. }, 0);
  148. };
  149. webSocket.onclose = function () {
  150. setTimeout(function () {
  151. $(self).trigger("websocketclose");
  152. }, 0);
  153. };
  154. };
  155. self.sendWebSocketMessage = function (name, data) {
  156. var msg = { MessageType: name };
  157. if (data) {
  158. msg.Data = data;
  159. }
  160. msg = JSON.stringify(msg);
  161. webSocket.send(msg);
  162. };
  163. self.isWebSocketOpen = function () {
  164. return webSocket && webSocket.readyState === WebSocket.OPEN;
  165. };
  166. self.isWebSocketOpenOrConnecting = function () {
  167. return webSocket && (webSocket.readyState === WebSocket.OPEN || webSocket.readyState === WebSocket.CONNECTING);
  168. };
  169. /**
  170. * Gets an item from the server
  171. * Omit itemId to get the root folder.
  172. */
  173. self.getItem = function (userId, itemId) {
  174. if (!userId) {
  175. throw new Error("null userId");
  176. }
  177. var url = self.getUrl("Users/" + userId + "/Items/" + itemId);
  178. return self.ajax({
  179. type: "GET",
  180. url: url,
  181. dataType: "json"
  182. });
  183. };
  184. /**
  185. * Gets the root folder from the server
  186. */
  187. self.getRootFolder = function (userId) {
  188. if (!userId) {
  189. throw new Error("null userId");
  190. }
  191. var url = self.getUrl("Users/" + userId + "/Items/Root");
  192. return self.ajax({
  193. type: "GET",
  194. url: url,
  195. dataType: "json"
  196. });
  197. };
  198. self.getNotificationSummary = function (userId) {
  199. if (!userId) {
  200. throw new Error("null userId");
  201. }
  202. var url = self.getUrl("Notifications/" + userId + "/Summary");
  203. return self.ajax({
  204. type: "GET",
  205. url: url,
  206. dataType: "json"
  207. });
  208. };
  209. self.getNotifications = function (userId, options) {
  210. if (!userId) {
  211. throw new Error("null userId");
  212. }
  213. var url = self.getUrl("Notifications/" + userId, options || {});
  214. return self.ajax({
  215. type: "GET",
  216. url: url,
  217. dataType: "json"
  218. });
  219. };
  220. self.markNotificationsRead = function (userId, idList, isRead) {
  221. if (!userId) {
  222. throw new Error("null userId");
  223. }
  224. if (!idList || !idList.length) {
  225. throw new Error("null idList");
  226. }
  227. var suffix = isRead ? "Read" : "Unread";
  228. var params = {
  229. UserId: userId,
  230. Ids: idList.join(',')
  231. };
  232. var url = self.getUrl("Notifications/" + userId + "/" + suffix, params);
  233. return self.ajax({
  234. type: "POST",
  235. url: url
  236. });
  237. };
  238. /**
  239. * Gets the current server status
  240. */
  241. self.getSystemInfo = function () {
  242. var url = self.getUrl("System/Info");
  243. return self.ajax({
  244. type: "GET",
  245. url: url,
  246. dataType: "json"
  247. });
  248. };
  249. self.getInstantMixFromSong = function (itemId, options) {
  250. var url = self.getUrl("Songs/" + itemId + "/InstantMix", options);
  251. return self.ajax({
  252. type: "GET",
  253. url: url,
  254. dataType: "json"
  255. });
  256. };
  257. self.getInstantMixFromAlbum = function (itemId, options) {
  258. var url = self.getUrl("Albums/" + itemId + "/InstantMix", options);
  259. return self.ajax({
  260. type: "GET",
  261. url: url,
  262. dataType: "json"
  263. });
  264. };
  265. self.getInstantMixFromArtist = function (name, options) {
  266. var url = self.getUrl("Artists/" + self.encodeName(name) + "/InstantMix", options);
  267. return self.ajax({
  268. type: "GET",
  269. url: url,
  270. dataType: "json"
  271. });
  272. };
  273. self.getInstantMixFromMusicGenre = function (name, options) {
  274. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/InstantMix", options);
  275. return self.ajax({
  276. type: "GET",
  277. url: url,
  278. dataType: "json"
  279. });
  280. };
  281. self.getSimilarMovies = function (itemId, options) {
  282. var url = self.getUrl("Movies/" + itemId + "/Similar", options);
  283. return self.ajax({
  284. type: "GET",
  285. url: url,
  286. dataType: "json"
  287. });
  288. };
  289. self.getSimilarTrailers = function (itemId, options) {
  290. var url = self.getUrl("Trailers/" + itemId + "/Similar", options);
  291. return self.ajax({
  292. type: "GET",
  293. url: url,
  294. dataType: "json"
  295. });
  296. };
  297. self.getSimilarShows = function (itemId, options) {
  298. var url = self.getUrl("Shows/" + itemId + "/Similar", options);
  299. return self.ajax({
  300. type: "GET",
  301. url: url,
  302. dataType: "json"
  303. });
  304. };
  305. self.getSimilarAlbums = function (itemId, options) {
  306. var url = self.getUrl("Albums/" + itemId + "/Similar", options);
  307. return self.ajax({
  308. type: "GET",
  309. url: url,
  310. dataType: "json"
  311. });
  312. };
  313. self.getSimilarGames = function (itemId, options) {
  314. var url = self.getUrl("Games/" + itemId + "/Similar", options);
  315. return self.ajax({
  316. type: "GET",
  317. url: url,
  318. dataType: "json"
  319. });
  320. };
  321. /**
  322. * Gets all cultures known to the server
  323. */
  324. self.getCultures = function () {
  325. var url = self.getUrl("Localization/cultures");
  326. return self.ajax({
  327. type: "GET",
  328. url: url,
  329. dataType: "json"
  330. });
  331. };
  332. /**
  333. * Gets all countries known to the server
  334. */
  335. self.getCountries = function () {
  336. var url = self.getUrl("Localization/countries");
  337. return self.ajax({
  338. type: "GET",
  339. url: url,
  340. dataType: "json"
  341. });
  342. };
  343. /**
  344. * Gets plugin security info
  345. */
  346. self.getPluginSecurityInfo = function () {
  347. var url = self.getUrl("Plugins/SecurityInfo");
  348. return self.ajax({
  349. type: "GET",
  350. url: url,
  351. dataType: "json"
  352. });
  353. };
  354. /**
  355. * Gets the directory contents of a path on the server
  356. */
  357. self.getDirectoryContents = function (path, options) {
  358. if (!path) {
  359. throw new Error("null path");
  360. }
  361. options = options || {};
  362. options.path = path;
  363. var url = self.getUrl("Environment/DirectoryContents", options);
  364. return self.ajax({
  365. type: "GET",
  366. url: url,
  367. dataType: "json"
  368. });
  369. };
  370. /**
  371. * Gets a list of physical drives from the server
  372. */
  373. self.getDrives = function () {
  374. var url = self.getUrl("Environment/Drives");
  375. return self.ajax({
  376. type: "GET",
  377. url: url,
  378. dataType: "json"
  379. });
  380. };
  381. /**
  382. * Gets a list of network devices from the server
  383. */
  384. self.getNetworkDevices = function () {
  385. var url = self.getUrl("Environment/NetworkDevices");
  386. return self.ajax({
  387. type: "GET",
  388. url: url,
  389. dataType: "json"
  390. });
  391. };
  392. /**
  393. * Cancels a package installation
  394. */
  395. self.cancelPackageInstallation = function (installationId) {
  396. if (!installationId) {
  397. throw new Error("null installationId");
  398. }
  399. var url = self.getUrl("Packages/Installing/" + id);
  400. return self.ajax({
  401. type: "DELETE",
  402. url: url
  403. });
  404. };
  405. /**
  406. * Refreshes metadata for an item
  407. */
  408. self.refreshItem = function (itemId, force, recursive) {
  409. if (!itemId) {
  410. throw new Error("null itemId");
  411. }
  412. var url = self.getUrl("Items/" + itemId + "/Refresh", {
  413. forced: force || false,
  414. recursive: recursive || false
  415. });
  416. return self.ajax({
  417. type: "POST",
  418. url: url
  419. });
  420. };
  421. self.refreshArtist = function (name, force) {
  422. if (!name) {
  423. throw new Error("null name");
  424. }
  425. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Refresh", {
  426. forced: force || false
  427. });
  428. return self.ajax({
  429. type: "POST",
  430. url: url
  431. });
  432. };
  433. self.refreshGenre = function (name, force) {
  434. if (!name) {
  435. throw new Error("null name");
  436. }
  437. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Refresh", {
  438. forced: force || false
  439. });
  440. return self.ajax({
  441. type: "POST",
  442. url: url
  443. });
  444. };
  445. self.refreshMusicGenre = function (name, force) {
  446. if (!name) {
  447. throw new Error("null name");
  448. }
  449. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/Refresh", {
  450. forced: force || false
  451. });
  452. return self.ajax({
  453. type: "POST",
  454. url: url
  455. });
  456. };
  457. self.refreshGameGenre = function (name, force) {
  458. if (!name) {
  459. throw new Error("null name");
  460. }
  461. var url = self.getUrl("GameGenres/" + self.encodeName(name) + "/Refresh", {
  462. forced: force || false
  463. });
  464. return self.ajax({
  465. type: "POST",
  466. url: url
  467. });
  468. };
  469. self.refreshPerson = function (name, force) {
  470. if (!name) {
  471. throw new Error("null name");
  472. }
  473. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Refresh", {
  474. forced: force || false
  475. });
  476. return self.ajax({
  477. type: "POST",
  478. url: url
  479. });
  480. };
  481. self.refreshStudio = function (name, force) {
  482. if (!name) {
  483. throw new Error("null name");
  484. }
  485. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Refresh", {
  486. forced: force || false
  487. });
  488. return self.ajax({
  489. type: "POST",
  490. url: url
  491. });
  492. };
  493. /**
  494. * Installs or updates a new plugin
  495. */
  496. self.installPlugin = function (name, updateClass, version) {
  497. if (!name) {
  498. throw new Error("null name");
  499. }
  500. if (!updateClass) {
  501. throw new Error("null updateClass");
  502. }
  503. var options = {
  504. updateClass: updateClass
  505. };
  506. if (version) {
  507. options.version = version;
  508. }
  509. var url = self.getUrl("Packages/Installed/" + name, options);
  510. return self.ajax({
  511. type: "POST",
  512. url: url
  513. });
  514. };
  515. /**
  516. * Instructs the server to perform a restart.
  517. */
  518. self.restartServer = function () {
  519. var url = self.getUrl("System/Restart");
  520. return self.ajax({
  521. type: "POST",
  522. url: url
  523. });
  524. };
  525. /**
  526. * Gets information about an installable package
  527. */
  528. self.getPackageInfo = function (name) {
  529. if (!name) {
  530. throw new Error("null name");
  531. }
  532. var url = self.getUrl("Packages/" + name);
  533. return self.ajax({
  534. type: "GET",
  535. url: url,
  536. dataType: "json"
  537. });
  538. };
  539. /**
  540. * Gets the latest available application update (if any)
  541. */
  542. self.getAvailableApplicationUpdate = function () {
  543. var url = self.getUrl("Packages/Updates", { PackageType: "System" });
  544. return self.ajax({
  545. type: "GET",
  546. url: url,
  547. dataType: "json"
  548. });
  549. };
  550. /**
  551. * Gets the latest available plugin updates (if any)
  552. */
  553. self.getAvailablePluginUpdates = function () {
  554. var url = self.getUrl("Packages/Updates", { PackageType: "UserInstalled" });
  555. return self.ajax({
  556. type: "GET",
  557. url: url,
  558. dataType: "json"
  559. });
  560. };
  561. /**
  562. * Gets the virtual folder for a view. Specify a userId to get a user view, or omit for the default view.
  563. */
  564. self.getVirtualFolders = function (userId) {
  565. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  566. url = self.getUrl(url);
  567. return self.ajax({
  568. type: "GET",
  569. url: url,
  570. dataType: "json"
  571. });
  572. };
  573. /**
  574. * Gets all the paths of the locations in the physical root.
  575. */
  576. self.getPhysicalPaths = function () {
  577. var url = self.getUrl("Library/PhysicalPaths");
  578. return self.ajax({
  579. type: "GET",
  580. url: url,
  581. dataType: "json"
  582. });
  583. };
  584. /**
  585. * Gets the current server configuration
  586. */
  587. self.getServerConfiguration = function () {
  588. var url = self.getUrl("System/Configuration");
  589. return self.ajax({
  590. type: "GET",
  591. url: url,
  592. dataType: "json"
  593. });
  594. };
  595. /**
  596. * Gets the server's scheduled tasks
  597. */
  598. self.getScheduledTasks = function () {
  599. var url = self.getUrl("ScheduledTasks");
  600. return self.ajax({
  601. type: "GET",
  602. url: url,
  603. dataType: "json"
  604. });
  605. };
  606. /**
  607. * Starts a scheduled task
  608. */
  609. self.startScheduledTask = function (id) {
  610. if (!id) {
  611. throw new Error("null id");
  612. }
  613. var url = self.getUrl("ScheduledTasks/Running/" + id);
  614. return self.ajax({
  615. type: "POST",
  616. url: url
  617. });
  618. };
  619. /**
  620. * Gets a scheduled task
  621. */
  622. self.getScheduledTask = function (id) {
  623. if (!id) {
  624. throw new Error("null id");
  625. }
  626. var url = self.getUrl("ScheduledTasks/" + id);
  627. return self.ajax({
  628. type: "GET",
  629. url: url,
  630. dataType: "json"
  631. });
  632. };
  633. self.getNextUpEpisodes = function (options) {
  634. var url = self.getUrl("Shows/NextUp", options);
  635. return self.ajax({
  636. type: "GET",
  637. url: url,
  638. dataType: "json"
  639. });
  640. };
  641. /**
  642. * Stops a scheduled task
  643. */
  644. self.stopScheduledTask = function (id) {
  645. if (!id) {
  646. throw new Error("null id");
  647. }
  648. var url = self.getUrl("ScheduledTasks/Running/" + id);
  649. return self.ajax({
  650. type: "DELETE",
  651. url: url
  652. });
  653. };
  654. /**
  655. * Gets the configuration of a plugin
  656. * @param {String} Id
  657. */
  658. self.getPluginConfiguration = function (id) {
  659. if (!id) {
  660. throw new Error("null Id");
  661. }
  662. var url = self.getUrl("Plugins/" + id + "/Configuration");
  663. return self.ajax({
  664. type: "GET",
  665. url: url,
  666. dataType: "json"
  667. });
  668. };
  669. /**
  670. * Gets a list of plugins that are available to be installed
  671. */
  672. self.getAvailablePlugins = function (options) {
  673. options = $.extend({}, options || {});
  674. options.PackageType = "UserInstalled";
  675. var url = self.getUrl("Packages", options);
  676. return self.ajax({
  677. type: "GET",
  678. url: url,
  679. dataType: "json"
  680. });
  681. };
  682. /**
  683. * Uninstalls a plugin
  684. * @param {String} Id
  685. */
  686. self.uninstallPlugin = function (id) {
  687. if (!id) {
  688. throw new Error("null Id");
  689. }
  690. var url = self.getUrl("Plugins/" + id);
  691. return self.ajax({
  692. type: "DELETE",
  693. url: url
  694. });
  695. };
  696. /**
  697. * Removes a virtual folder from either the default view or a user view
  698. * @param {String} name
  699. */
  700. self.removeVirtualFolder = function (name, userId) {
  701. if (!name) {
  702. throw new Error("null name");
  703. }
  704. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  705. url += "/" + name;
  706. url = self.getUrl(url);
  707. return self.ajax({
  708. type: "DELETE",
  709. url: url
  710. });
  711. };
  712. /**
  713. * Adds a virtual folder to either the default view or a user view
  714. * @param {String} name
  715. */
  716. self.addVirtualFolder = function (name, type, userId) {
  717. if (!name) {
  718. throw new Error("null name");
  719. }
  720. var options = {};
  721. if (type) {
  722. options.collectionType = type;
  723. }
  724. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  725. url += "/" + name;
  726. url = self.getUrl(url, options);
  727. return self.ajax({
  728. type: "POST",
  729. url: url
  730. });
  731. };
  732. /**
  733. * Renames a virtual folder, within either the default view or a user view
  734. * @param {String} name
  735. */
  736. self.renameVirtualFolder = function (name, newName, userId) {
  737. if (!name) {
  738. throw new Error("null name");
  739. }
  740. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  741. url += "/" + name + "/Name";
  742. url = self.getUrl(url, { newName: newName });
  743. return self.ajax({
  744. type: "POST",
  745. url: url
  746. });
  747. };
  748. /**
  749. * Adds an additional mediaPath to an existing virtual folder, within either the default view or a user view
  750. * @param {String} name
  751. */
  752. self.addMediaPath = function (virtualFolderName, mediaPath, userId) {
  753. if (!virtualFolderName) {
  754. throw new Error("null virtualFolderName");
  755. }
  756. if (!mediaPath) {
  757. throw new Error("null mediaPath");
  758. }
  759. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  760. url += "/" + virtualFolderName + "/Paths";
  761. url = self.getUrl(url, { path: mediaPath });
  762. return self.ajax({
  763. type: "POST",
  764. url: url
  765. });
  766. };
  767. /**
  768. * Removes a media path from a virtual folder, within either the default view or a user view
  769. * @param {String} name
  770. */
  771. self.removeMediaPath = function (virtualFolderName, mediaPath, userId) {
  772. if (!virtualFolderName) {
  773. throw new Error("null virtualFolderName");
  774. }
  775. if (!mediaPath) {
  776. throw new Error("null mediaPath");
  777. }
  778. var url = userId ? "Users/" + userId + "/VirtualFolders" : "Library/VirtualFolders";
  779. url += "/" + virtualFolderName + "/Paths";
  780. url = self.getUrl(url, { path: mediaPath });
  781. return self.ajax({
  782. type: "DELETE",
  783. url: url
  784. });
  785. };
  786. /**
  787. * Deletes a user
  788. * @param {String} id
  789. */
  790. self.deleteUser = function (id) {
  791. if (!id) {
  792. throw new Error("null id");
  793. }
  794. var url = self.getUrl("Users/" + id);
  795. return self.ajax({
  796. type: "DELETE",
  797. url: url
  798. });
  799. };
  800. /**
  801. * Deletes a user image
  802. * @param {String} userId
  803. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  804. */
  805. self.deleteUserImage = function (userId, imageType, imageIndex) {
  806. if (!userId) {
  807. throw new Error("null userId");
  808. }
  809. if (!imageType) {
  810. throw new Error("null imageType");
  811. }
  812. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  813. if (imageIndex != null) {
  814. url += "/" + imageIndex;
  815. }
  816. return self.ajax({
  817. type: "DELETE",
  818. url: url
  819. });
  820. };
  821. self.deleteItemImage = function (itemId, itemType, itemName, imageType, imageIndex) {
  822. if (!imageType) {
  823. throw new Error("null imageType");
  824. }
  825. if (!itemType) {
  826. throw new Error("null itemType");
  827. }
  828. var url;
  829. if (itemType == "Artist") {
  830. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  831. }
  832. else if (itemType == "Genre") {
  833. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  834. }
  835. else if (itemType == "GameGenre") {
  836. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  837. }
  838. else if (itemType == "MusicGenre") {
  839. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  840. }
  841. else if (itemType == "Person") {
  842. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  843. }
  844. else if (itemType == "Studio") {
  845. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  846. }
  847. else {
  848. url = self.getUrl("Items/" + itemId + "/Images");
  849. }
  850. url += "/" + imageType;
  851. if (imageIndex != null) {
  852. url += "/" + imageIndex;
  853. }
  854. return self.ajax({
  855. type: "DELETE",
  856. url: url
  857. });
  858. };
  859. self.updateItemImageIndex = function (itemId, itemType, itemName, imageType, imageIndex, newIndex) {
  860. if (!imageType) {
  861. throw new Error("null imageType");
  862. }
  863. if (!itemType) {
  864. throw new Error("null itemType");
  865. }
  866. var url;
  867. var options = { newIndex: newIndex };
  868. if (itemType == "Artist") {
  869. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  870. }
  871. else if (itemType == "Genre") {
  872. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  873. }
  874. else if (itemType == "GameGenre") {
  875. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  876. }
  877. else if (itemType == "MusicGenre") {
  878. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  879. }
  880. else if (itemType == "Person") {
  881. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  882. }
  883. else if (itemType == "Studio") {
  884. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  885. }
  886. else {
  887. url = self.getUrl("Items/" + itemId + "/Images/" + imageType + "/" + imageIndex + "/Index", options);
  888. }
  889. return self.ajax({
  890. type: "POST",
  891. url: url
  892. });
  893. };
  894. self.getItemImageInfos = function (itemId, itemType, itemName) {
  895. if (!itemType) {
  896. throw new Error("null itemType");
  897. }
  898. var url;
  899. if (itemType == "Artist") {
  900. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  901. }
  902. else if (itemType == "Genre") {
  903. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  904. }
  905. else if (itemType == "GameGenre") {
  906. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  907. }
  908. else if (itemType == "MusicGenre") {
  909. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  910. }
  911. else if (itemType == "Person") {
  912. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  913. }
  914. else if (itemType == "Studio") {
  915. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  916. }
  917. else {
  918. url = self.getUrl("Items/" + itemId + "/Images");
  919. }
  920. return self.ajax({
  921. type: "GET",
  922. url: url,
  923. dataType: "json"
  924. });
  925. };
  926. self.getCriticReviews = function (itemId, options) {
  927. if (!itemId) {
  928. throw new Error("null itemId");
  929. }
  930. var url = self.getUrl("Items/" + itemId + "/CriticReviews", options);
  931. return self.ajax({
  932. type: "GET",
  933. url: url,
  934. dataType: "json"
  935. });
  936. };
  937. self.getSessions = function (options) {
  938. var url = self.getUrl("Sessions", options);
  939. return self.ajax({
  940. type: "GET",
  941. url: url,
  942. dataType: "json"
  943. });
  944. };
  945. /**
  946. * Uploads a user image
  947. * @param {String} userId
  948. * @param {String} imageType The type of image to delete, based on the server-side ImageType enum.
  949. * @param {Object} file The file from the input element
  950. */
  951. self.uploadUserImage = function (userId, imageType, file) {
  952. if (!userId) {
  953. throw new Error("null userId");
  954. }
  955. if (!imageType) {
  956. throw new Error("null imageType");
  957. }
  958. if (!file) {
  959. throw new Error("File must be an image.");
  960. }
  961. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  962. throw new Error("File must be an image.");
  963. }
  964. var deferred = $.Deferred();
  965. var reader = new FileReader();
  966. reader.onerror = function () {
  967. deferred.reject();
  968. };
  969. reader.onabort = function () {
  970. deferred.reject();
  971. };
  972. // Closure to capture the file information.
  973. reader.onload = function (e) {
  974. // Split by a comma to remove the url: prefix
  975. var data = e.target.result.split(',')[1];
  976. var url = self.getUrl("Users/" + userId + "/Images/" + imageType);
  977. self.ajax({
  978. type: "POST",
  979. url: url,
  980. data: data,
  981. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  982. }).done(function (result) {
  983. deferred.resolveWith(null, [result]);
  984. }).fail(function () {
  985. deferred.reject();
  986. });
  987. };
  988. // Read in the image file as a data URL.
  989. reader.readAsDataURL(file);
  990. return deferred.promise();
  991. };
  992. self.uploadItemImage = function (itemId, itemType, itemName, imageType, file) {
  993. if (!itemId) {
  994. throw new Error("null itemId");
  995. }
  996. if (!imageType) {
  997. throw new Error("null imageType");
  998. }
  999. if (!file) {
  1000. throw new Error("File must be an image.");
  1001. }
  1002. if (file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/jpeg") {
  1003. throw new Error("File must be an image.");
  1004. }
  1005. var url;
  1006. if (itemType == "Artist") {
  1007. url = self.getUrl("Artists/" + self.encodeName(itemName) + "/Images");
  1008. }
  1009. else if (itemType == "Genre") {
  1010. url = self.getUrl("Genres/" + self.encodeName(itemName) + "/Images");
  1011. }
  1012. else if (itemType == "GameGenre") {
  1013. url = self.getUrl("GameGenres/" + self.encodeName(itemName) + "/Images");
  1014. }
  1015. else if (itemType == "MusicGenre") {
  1016. url = self.getUrl("MusicGenres/" + self.encodeName(itemName) + "/Images");
  1017. }
  1018. else if (itemType == "Person") {
  1019. url = self.getUrl("Persons/" + self.encodeName(itemName) + "/Images");
  1020. }
  1021. else if (itemType == "Studio") {
  1022. url = self.getUrl("Studios/" + self.encodeName(itemName) + "/Images");
  1023. }
  1024. else {
  1025. url = self.getUrl("Items/" + itemId + "/Images");
  1026. }
  1027. url += "/" + imageType;
  1028. var deferred = $.Deferred();
  1029. var reader = new FileReader();
  1030. reader.onerror = function () {
  1031. deferred.reject();
  1032. };
  1033. reader.onabort = function () {
  1034. deferred.reject();
  1035. };
  1036. // Closure to capture the file information.
  1037. reader.onload = function (e) {
  1038. // Split by a comma to remove the url: prefix
  1039. var data = e.target.result.split(',')[1];
  1040. self.ajax({
  1041. type: "POST",
  1042. url: url,
  1043. data: data,
  1044. contentType: "image/" + file.name.substring(file.name.lastIndexOf('.') + 1)
  1045. }).done(function (result) {
  1046. deferred.resolveWith(null, [result]);
  1047. }).fail(function () {
  1048. deferred.reject();
  1049. });
  1050. };
  1051. // Read in the image file as a data URL.
  1052. reader.readAsDataURL(file);
  1053. return deferred.promise();
  1054. };
  1055. /**
  1056. * Gets the list of installed plugins on the server
  1057. */
  1058. self.getInstalledPlugins = function () {
  1059. var url = self.getUrl("Plugins");
  1060. return self.ajax({
  1061. type: "GET",
  1062. url: url,
  1063. dataType: "json"
  1064. });
  1065. };
  1066. /**
  1067. * Gets a user by id
  1068. * @param {String} id
  1069. */
  1070. self.getUser = function (id) {
  1071. if (!id) {
  1072. throw new Error("Must supply a userId");
  1073. }
  1074. var url = self.getUrl("Users/" + id);
  1075. return self.ajax({
  1076. type: "GET",
  1077. url: url,
  1078. dataType: "json"
  1079. });
  1080. };
  1081. /**
  1082. * Gets a studio
  1083. */
  1084. self.getStudio = function (name, userId) {
  1085. if (!name) {
  1086. throw new Error("null name");
  1087. }
  1088. var options = {};
  1089. if (userId) {
  1090. options.userId = userId;
  1091. }
  1092. var url = self.getUrl("Studios/" + self.encodeName(name), options);
  1093. return self.ajax({
  1094. type: "GET",
  1095. url: url,
  1096. dataType: "json"
  1097. });
  1098. };
  1099. /**
  1100. * Gets a genre
  1101. */
  1102. self.getGenre = function (name, userId) {
  1103. if (!name) {
  1104. throw new Error("null name");
  1105. }
  1106. var options = {};
  1107. if (userId) {
  1108. options.userId = userId;
  1109. }
  1110. var url = self.getUrl("Genres/" + self.encodeName(name), options);
  1111. return self.ajax({
  1112. type: "GET",
  1113. url: url,
  1114. dataType: "json"
  1115. });
  1116. };
  1117. self.getMusicGenre = function (name, userId) {
  1118. if (!name) {
  1119. throw new Error("null name");
  1120. }
  1121. var options = {};
  1122. if (userId) {
  1123. options.userId = userId;
  1124. }
  1125. var url = self.getUrl("MusicGenres/" + self.encodeName(name), options);
  1126. return self.ajax({
  1127. type: "GET",
  1128. url: url,
  1129. dataType: "json"
  1130. });
  1131. };
  1132. self.getGameGenre = function (name, userId) {
  1133. if (!name) {
  1134. throw new Error("null name");
  1135. }
  1136. var options = {};
  1137. if (userId) {
  1138. options.userId = userId;
  1139. }
  1140. var url = self.getUrl("GameGenres/" + self.encodeName(name), options);
  1141. return self.ajax({
  1142. type: "GET",
  1143. url: url,
  1144. dataType: "json"
  1145. });
  1146. };
  1147. /**
  1148. * Gets an artist
  1149. */
  1150. self.getArtist = function (name, userId) {
  1151. if (!name) {
  1152. throw new Error("null name");
  1153. }
  1154. var options = {};
  1155. if (userId) {
  1156. options.userId = userId;
  1157. }
  1158. var url = self.getUrl("Artists/" + self.encodeName(name), options);
  1159. return self.ajax({
  1160. type: "GET",
  1161. url: url,
  1162. dataType: "json"
  1163. });
  1164. };
  1165. /**
  1166. * Gets a year
  1167. */
  1168. self.getYear = function (yea, userId) {
  1169. if (!name) {
  1170. throw new Error("null name");
  1171. }
  1172. var options = {};
  1173. if (userId) {
  1174. options.userId = userId;
  1175. }
  1176. var url = self.getUrl("Years/" + self.encodeName(name), options);
  1177. return self.ajax({
  1178. type: "GET",
  1179. url: url,
  1180. dataType: "json"
  1181. });
  1182. };
  1183. /**
  1184. * Gets a Person
  1185. */
  1186. self.getPerson = function (name, userId) {
  1187. if (!name) {
  1188. throw new Error("null name");
  1189. }
  1190. var options = {};
  1191. if (userId) {
  1192. options.userId = userId;
  1193. }
  1194. var url = self.getUrl("Persons/" + self.encodeName(name), options);
  1195. return self.ajax({
  1196. type: "GET",
  1197. url: url,
  1198. dataType: "json"
  1199. });
  1200. };
  1201. self.getPublicUsers = function () {
  1202. var url = self.getUrl("users/public");
  1203. return self.ajax({
  1204. type: "GET",
  1205. url: url,
  1206. dataType: "json"
  1207. });
  1208. };
  1209. /**
  1210. * Gets all users from the server
  1211. */
  1212. self.getUsers = function (options) {
  1213. var url = self.getUrl("users", options || {});
  1214. return self.ajax({
  1215. type: "GET",
  1216. url: url,
  1217. dataType: "json"
  1218. });
  1219. };
  1220. /**
  1221. * Gets all available parental ratings from the server
  1222. */
  1223. self.getParentalRatings = function () {
  1224. var url = self.getUrl("Localization/ParentalRatings");
  1225. return self.ajax({
  1226. type: "GET",
  1227. url: url,
  1228. dataType: "json"
  1229. });
  1230. };
  1231. /**
  1232. * Gets a list of all available conrete BaseItem types from the server
  1233. */
  1234. self.getItemTypes = function (options) {
  1235. var url = self.getUrl("Library/ItemTypes", options);
  1236. return self.ajax({
  1237. type: "GET",
  1238. url: url,
  1239. dataType: "json"
  1240. });
  1241. };
  1242. /**
  1243. * Constructs a url for a user image
  1244. * @param {String} userId
  1245. * @param {Object} options
  1246. * Options supports the following properties:
  1247. * width - download the image at a fixed width
  1248. * height - download the image at a fixed height
  1249. * maxWidth - download the image at a maxWidth
  1250. * maxHeight - download the image at a maxHeight
  1251. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1252. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1253. */
  1254. self.getUserImageUrl = function (userId, options) {
  1255. if (!userId) {
  1256. throw new Error("null userId");
  1257. }
  1258. options = options || {
  1259. };
  1260. var url = "Users/" + userId + "/Images/" + options.type;
  1261. if (options.index != null) {
  1262. url += "/" + options.index;
  1263. }
  1264. // Don't put these on the query string
  1265. delete options.type;
  1266. delete options.index;
  1267. return self.getUrl(url, options);
  1268. };
  1269. /**
  1270. * Constructs a url for a person image
  1271. * @param {String} name
  1272. * @param {Object} options
  1273. * Options supports the following properties:
  1274. * width - download the image at a fixed width
  1275. * height - download the image at a fixed height
  1276. * maxWidth - download the image at a maxWidth
  1277. * maxHeight - download the image at a maxHeight
  1278. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1279. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1280. */
  1281. self.getPersonImageUrl = function (name, options) {
  1282. if (!name) {
  1283. throw new Error("null name");
  1284. }
  1285. options = options || {
  1286. };
  1287. var url = "Persons/" + self.encodeName(name) + "/Images/" + options.type;
  1288. if (options.index != null) {
  1289. url += "/" + options.index;
  1290. }
  1291. // Don't put these on the query string
  1292. delete options.type;
  1293. delete options.index;
  1294. return self.getUrl(url, options);
  1295. };
  1296. /**
  1297. * Constructs a url for a year image
  1298. * @param {String} year
  1299. * @param {Object} options
  1300. * Options supports the following properties:
  1301. * width - download the image at a fixed width
  1302. * height - download the image at a fixed height
  1303. * maxWidth - download the image at a maxWidth
  1304. * maxHeight - download the image at a maxHeight
  1305. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1306. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1307. */
  1308. self.getYearImageUrl = function (year, options) {
  1309. if (!year) {
  1310. throw new Error("null year");
  1311. }
  1312. options = options || {
  1313. };
  1314. var url = "Years/" + year + "/Images/" + options.type;
  1315. if (options.index != null) {
  1316. url += "/" + options.index;
  1317. }
  1318. // Don't put these on the query string
  1319. delete options.type;
  1320. delete options.index;
  1321. return self.getUrl(url, options);
  1322. };
  1323. /**
  1324. * Constructs a url for a genre image
  1325. * @param {String} name
  1326. * @param {Object} options
  1327. * Options supports the following properties:
  1328. * width - download the image at a fixed width
  1329. * height - download the image at a fixed height
  1330. * maxWidth - download the image at a maxWidth
  1331. * maxHeight - download the image at a maxHeight
  1332. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1333. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1334. */
  1335. self.getGenreImageUrl = function (name, options) {
  1336. if (!name) {
  1337. throw new Error("null name");
  1338. }
  1339. options = options || {
  1340. };
  1341. var url = "Genres/" + self.encodeName(name) + "/Images/" + options.type;
  1342. if (options.index != null) {
  1343. url += "/" + options.index;
  1344. }
  1345. // Don't put these on the query string
  1346. delete options.type;
  1347. delete options.index;
  1348. return self.getUrl(url, options);
  1349. };
  1350. /**
  1351. * Constructs a url for a genre image
  1352. * @param {String} name
  1353. * @param {Object} options
  1354. * Options supports the following properties:
  1355. * width - download the image at a fixed width
  1356. * height - download the image at a fixed height
  1357. * maxWidth - download the image at a maxWidth
  1358. * maxHeight - download the image at a maxHeight
  1359. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1360. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1361. */
  1362. self.getMusicGenreImageUrl = function (name, options) {
  1363. if (!name) {
  1364. throw new Error("null name");
  1365. }
  1366. options = options || {
  1367. };
  1368. var url = "MusicGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1369. if (options.index != null) {
  1370. url += "/" + options.index;
  1371. }
  1372. // Don't put these on the query string
  1373. delete options.type;
  1374. delete options.index;
  1375. return self.getUrl(url, options);
  1376. };
  1377. /**
  1378. * Constructs a url for a genre image
  1379. * @param {String} name
  1380. * @param {Object} options
  1381. * Options supports the following properties:
  1382. * width - download the image at a fixed width
  1383. * height - download the image at a fixed height
  1384. * maxWidth - download the image at a maxWidth
  1385. * maxHeight - download the image at a maxHeight
  1386. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1387. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1388. */
  1389. self.getGameGenreImageUrl = function (name, options) {
  1390. if (!name) {
  1391. throw new Error("null name");
  1392. }
  1393. options = options || {
  1394. };
  1395. var url = "GameGenres/" + self.encodeName(name) + "/Images/" + options.type;
  1396. if (options.index != null) {
  1397. url += "/" + options.index;
  1398. }
  1399. // Don't put these on the query string
  1400. delete options.type;
  1401. delete options.index;
  1402. return self.getUrl(url, options);
  1403. };
  1404. /**
  1405. * Constructs a url for a artist image
  1406. * @param {String} name
  1407. * @param {Object} options
  1408. * Options supports the following properties:
  1409. * width - download the image at a fixed width
  1410. * height - download the image at a fixed height
  1411. * maxWidth - download the image at a maxWidth
  1412. * maxHeight - download the image at a maxHeight
  1413. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1414. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1415. */
  1416. self.getArtistImageUrl = function (name, options) {
  1417. if (!name) {
  1418. throw new Error("null name");
  1419. }
  1420. options = options || {
  1421. };
  1422. var url = "Artists/" + self.encodeName(name) + "/Images/" + options.type;
  1423. if (options.index != null) {
  1424. url += "/" + options.index;
  1425. }
  1426. // Don't put these on the query string
  1427. delete options.type;
  1428. delete options.index;
  1429. return self.getUrl(url, options);
  1430. };
  1431. /**
  1432. * Constructs a url for a studio image
  1433. * @param {String} name
  1434. * @param {Object} options
  1435. * Options supports the following properties:
  1436. * width - download the image at a fixed width
  1437. * height - download the image at a fixed height
  1438. * maxWidth - download the image at a maxWidth
  1439. * maxHeight - download the image at a maxHeight
  1440. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1441. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1442. */
  1443. self.getStudioImageUrl = function (name, options) {
  1444. if (!name) {
  1445. throw new Error("null name");
  1446. }
  1447. options = options || {
  1448. };
  1449. var url = "Studios/" + self.encodeName(name) + "/Images/" + options.type;
  1450. if (options.index != null) {
  1451. url += "/" + options.index;
  1452. }
  1453. // Don't put these on the query string
  1454. delete options.type;
  1455. delete options.index;
  1456. return self.getUrl(url, options);
  1457. };
  1458. /**
  1459. * Constructs a url for an item image
  1460. * @param {String} itemId
  1461. * @param {Object} options
  1462. * Options supports the following properties:
  1463. * type - Primary, logo, backdrop, etc. See the server-side enum ImageType
  1464. * index - When downloading a backdrop, use this to specify which one (omitting is equivalent to zero)
  1465. * width - download the image at a fixed width
  1466. * height - download the image at a fixed height
  1467. * maxWidth - download the image at a maxWidth
  1468. * maxHeight - download the image at a maxHeight
  1469. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1470. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1471. */
  1472. self.getImageUrl = function (itemId, options) {
  1473. if (!itemId) {
  1474. throw new Error("itemId cannot be empty");
  1475. }
  1476. options = options || {
  1477. };
  1478. var url = "Items/" + itemId + "/Images/" + options.type;
  1479. if (options.index != null) {
  1480. url += "/" + options.index;
  1481. }
  1482. // Don't put these on the query string
  1483. delete options.type;
  1484. delete options.index;
  1485. return self.getUrl(url, options);
  1486. };
  1487. /**
  1488. * Constructs a url for an item logo image
  1489. * If the item doesn't have a logo, it will inherit a logo from a parent
  1490. * @param {Object} item A BaseItem
  1491. * @param {Object} options
  1492. * Options supports the following properties:
  1493. * width - download the image at a fixed width
  1494. * height - download the image at a fixed height
  1495. * maxWidth - download the image at a maxWidth
  1496. * maxHeight - download the image at a maxHeight
  1497. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1498. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1499. */
  1500. self.getLogoImageUrl = function (item, options) {
  1501. if (!item) {
  1502. throw new Error("null item");
  1503. }
  1504. options = options || {
  1505. };
  1506. options.imageType = "logo";
  1507. var logoItemId = item.HasLogo ? item.Id : item.ParentLogoItemId;
  1508. return logoItemId ? self.getImageUrl(logoItemId, options) : null;
  1509. };
  1510. /**
  1511. * Constructs an array of backdrop image url's for an item
  1512. * If the item doesn't have any backdrops, it will inherit them from a parent
  1513. * @param {Object} item A BaseItem
  1514. * @param {Object} options
  1515. * Options supports the following properties:
  1516. * width - download the image at a fixed width
  1517. * height - download the image at a fixed height
  1518. * maxWidth - download the image at a maxWidth
  1519. * maxHeight - download the image at a maxHeight
  1520. * quality - A scale of 0-100. This should almost always be omitted as the default will suffice.
  1521. * For best results do not specify both width and height together, as aspect ratio might be altered.
  1522. */
  1523. self.getBackdropImageUrl = function (item, options) {
  1524. if (!item) {
  1525. throw new Error("null item");
  1526. }
  1527. options = options || {
  1528. };
  1529. options.imageType = "backdrop";
  1530. var backdropItemId;
  1531. var backdropCount;
  1532. if (!item.BackdropCount) {
  1533. backdropItemId = item.ParentBackdropItemId;
  1534. backdropCount = item.ParentBackdropCount || 0;
  1535. } else {
  1536. backdropItemId = item.Id;
  1537. backdropCount = item.BackdropCount;
  1538. }
  1539. if (!backdropItemId) {
  1540. return [];
  1541. }
  1542. var files = [];
  1543. for (var i = 0; i < backdropCount; i++) {
  1544. options.imageIndex = i;
  1545. files[i] = self.getImageUrl(backdropItemId, options);
  1546. }
  1547. return files;
  1548. };
  1549. /**
  1550. * Authenticates a user
  1551. * @param {String} name
  1552. * @param {String} password
  1553. */
  1554. self.authenticateUserByName = function (name, password) {
  1555. if (!name) {
  1556. throw new Error("null name");
  1557. }
  1558. var url = self.getUrl("Users/authenticatebyname");
  1559. var postData = {
  1560. password: MediaBrowser.SHA1(password || ""),
  1561. Username: name
  1562. };
  1563. return self.ajax({
  1564. type: "POST",
  1565. url: url,
  1566. data: JSON.stringify(postData),
  1567. dataType: "json",
  1568. contentType: "application/json"
  1569. });
  1570. };
  1571. /**
  1572. * Authenticates a user
  1573. * @param {String} userId
  1574. * @param {String} password
  1575. */
  1576. self.authenticateUser = function (userId, password) {
  1577. if (!userId) {
  1578. throw new Error("null userId");
  1579. }
  1580. var url = self.getUrl("Users/" + userId + "/authenticate");
  1581. var postData = {
  1582. password: MediaBrowser.SHA1(password || "")
  1583. };
  1584. return self.ajax({
  1585. type: "POST",
  1586. url: url,
  1587. data: JSON.stringify(postData),
  1588. dataType: "json",
  1589. contentType: "application/json"
  1590. });
  1591. };
  1592. /**
  1593. * Updates a user's password
  1594. * @param {String} userId
  1595. * @param {String} currentPassword
  1596. * @param {String} newPassword
  1597. */
  1598. self.updateUserPassword = function (userId, currentPassword, newPassword) {
  1599. if (!userId) {
  1600. throw new Error("null userId");
  1601. }
  1602. var url = self.getUrl("Users/" + userId + "/Password");
  1603. var postData = {
  1604. };
  1605. postData.currentPassword = MediaBrowser.SHA1(currentPassword);
  1606. if (newPassword) {
  1607. postData.newPassword = newPassword;
  1608. }
  1609. return self.ajax({
  1610. type: "POST",
  1611. url: url,
  1612. data: postData
  1613. });
  1614. };
  1615. /**
  1616. * Resets a user's password
  1617. * @param {String} userId
  1618. */
  1619. self.resetUserPassword = function (userId) {
  1620. if (!userId) {
  1621. throw new Error("null userId");
  1622. }
  1623. var url = self.getUrl("Users/" + userId + "/Password");
  1624. var postData = {
  1625. };
  1626. postData.resetPassword = true;
  1627. return self.ajax({
  1628. type: "POST",
  1629. url: url,
  1630. data: postData
  1631. });
  1632. };
  1633. /**
  1634. * Updates the server's configuration
  1635. * @param {Object} configuration
  1636. */
  1637. self.updateServerConfiguration = function (configuration) {
  1638. if (!configuration) {
  1639. throw new Error("null configuration");
  1640. }
  1641. var url = self.getUrl("System/Configuration");
  1642. return self.ajax({
  1643. type: "POST",
  1644. url: url,
  1645. data: JSON.stringify(configuration),
  1646. contentType: "application/json"
  1647. });
  1648. };
  1649. self.updateItem = function (item) {
  1650. if (!item) {
  1651. throw new Error("null item");
  1652. }
  1653. var url = self.getUrl("Items/" + item.Id);
  1654. return self.ajax({
  1655. type: "POST",
  1656. url: url,
  1657. data: JSON.stringify(item),
  1658. contentType: "application/json"
  1659. });
  1660. };
  1661. self.updateArtist = function (item) {
  1662. if (!item) {
  1663. throw new Error("null item");
  1664. }
  1665. var url = self.getUrl("Artists/" + self.encodeName(item.Name));
  1666. return self.ajax({
  1667. type: "POST",
  1668. url: url,
  1669. data: JSON.stringify(item),
  1670. contentType: "application/json"
  1671. });
  1672. };
  1673. self.updatePerson = function (item) {
  1674. if (!item) {
  1675. throw new Error("null item");
  1676. }
  1677. var url = self.getUrl("Persons/" + self.encodeName(item.Name));
  1678. return self.ajax({
  1679. type: "POST",
  1680. url: url,
  1681. data: JSON.stringify(item),
  1682. contentType: "application/json"
  1683. });
  1684. };
  1685. self.updateStudio = function (item) {
  1686. if (!item) {
  1687. throw new Error("null item");
  1688. }
  1689. var url = self.getUrl("Studios/" + self.encodeName(item.Name));
  1690. return self.ajax({
  1691. type: "POST",
  1692. url: url,
  1693. data: JSON.stringify(item),
  1694. contentType: "application/json"
  1695. });
  1696. };
  1697. self.updateGenre = function (item) {
  1698. if (!item) {
  1699. throw new Error("null item");
  1700. }
  1701. var url = self.getUrl("Genres/" + self.encodeName(item.Name));
  1702. return self.ajax({
  1703. type: "POST",
  1704. url: url,
  1705. data: JSON.stringify(item),
  1706. contentType: "application/json"
  1707. });
  1708. };
  1709. self.updateMusicGenre = function (item) {
  1710. if (!item) {
  1711. throw new Error("null item");
  1712. }
  1713. var url = self.getUrl("MusicGenres/" + self.encodeName(item.Name));
  1714. return self.ajax({
  1715. type: "POST",
  1716. url: url,
  1717. data: JSON.stringify(item),
  1718. contentType: "application/json"
  1719. });
  1720. };
  1721. self.updateGameGenre = function (item) {
  1722. if (!item) {
  1723. throw new Error("null item");
  1724. }
  1725. var url = self.getUrl("GameGenres/" + self.encodeName(item.Name));
  1726. return self.ajax({
  1727. type: "POST",
  1728. url: url,
  1729. data: JSON.stringify(item),
  1730. contentType: "application/json"
  1731. });
  1732. };
  1733. /**
  1734. * Updates plugin security info
  1735. */
  1736. self.updatePluginSecurityInfo = function (info) {
  1737. var url = self.getUrl("Plugins/SecurityInfo");
  1738. return self.ajax({
  1739. type: "POST",
  1740. url: url,
  1741. data: JSON.stringify(info),
  1742. contentType: "application/json"
  1743. });
  1744. };
  1745. /**
  1746. * Creates a user
  1747. * @param {Object} user
  1748. */
  1749. self.createUser = function (user) {
  1750. if (!user) {
  1751. throw new Error("null user");
  1752. }
  1753. var url = self.getUrl("Users");
  1754. return self.ajax({
  1755. type: "POST",
  1756. url: url,
  1757. data: JSON.stringify(user),
  1758. dataType: "json",
  1759. contentType: "application/json"
  1760. });
  1761. };
  1762. /**
  1763. * Updates a user
  1764. * @param {Object} user
  1765. */
  1766. self.updateUser = function (user) {
  1767. if (!user) {
  1768. throw new Error("null user");
  1769. }
  1770. var url = self.getUrl("Users/" + user.Id);
  1771. return self.ajax({
  1772. type: "POST",
  1773. url: url,
  1774. data: JSON.stringify(user),
  1775. contentType: "application/json"
  1776. });
  1777. };
  1778. /**
  1779. * Updates the Triggers for a ScheduledTask
  1780. * @param {String} id
  1781. * @param {Object} triggers
  1782. */
  1783. self.updateScheduledTaskTriggers = function (id, triggers) {
  1784. if (!id) {
  1785. throw new Error("null id");
  1786. }
  1787. if (!triggers) {
  1788. throw new Error("null triggers");
  1789. }
  1790. var url = self.getUrl("ScheduledTasks/" + id + "/Triggers");
  1791. return self.ajax({
  1792. type: "POST",
  1793. url: url,
  1794. data: JSON.stringify(triggers),
  1795. contentType: "application/json"
  1796. });
  1797. };
  1798. /**
  1799. * Updates a plugin's configuration
  1800. * @param {String} Id
  1801. * @param {Object} configuration
  1802. */
  1803. self.updatePluginConfiguration = function (id, configuration) {
  1804. if (!id) {
  1805. throw new Error("null Id");
  1806. }
  1807. if (!configuration) {
  1808. throw new Error("null configuration");
  1809. }
  1810. var url = self.getUrl("Plugins/" + id + "/Configuration");
  1811. return self.ajax({
  1812. type: "POST",
  1813. url: url,
  1814. data: JSON.stringify(configuration),
  1815. contentType: "application/json"
  1816. });
  1817. };
  1818. self.getAncestorItems = function (itemId, userId) {
  1819. if (!itemId) {
  1820. throw new Error("null itemId");
  1821. }
  1822. var options = {};
  1823. if (userId) {
  1824. options.userId = userId;
  1825. }
  1826. var url = self.getUrl("Items/" + itemId + "/Ancestors", options);
  1827. return self.ajax({
  1828. type: "GET",
  1829. url: url,
  1830. dataType: "json"
  1831. });
  1832. };
  1833. /**
  1834. * Gets items based on a query, typically for children of a folder
  1835. * @param {String} userId
  1836. * @param {Object} options
  1837. * Options accepts the following properties:
  1838. * itemId - Localize the search to a specific folder (root if omitted)
  1839. * startIndex - Use for paging
  1840. * limit - Use to limit results to a certain number of items
  1841. * filter - Specify one or more ItemFilters, comma delimeted (see server-side enum)
  1842. * sortBy - Specify an ItemSortBy (comma-delimeted list see server-side enum)
  1843. * sortOrder - ascending/descending
  1844. * fields - additional fields to include aside from basic info. This is a comma delimited list. See server-side enum ItemFields.
  1845. * index - the name of the dynamic, localized index function
  1846. * dynamicSortBy - the name of the dynamic localized sort function
  1847. * recursive - Whether or not the query should be recursive
  1848. * searchTerm - search term to use as a filter
  1849. */
  1850. self.getItems = function (userId, options) {
  1851. if (!userId) {
  1852. throw new Error("null userId");
  1853. }
  1854. var url = self.getUrl("Users/" + userId + "/Items", options);
  1855. return self.ajax({
  1856. type: "GET",
  1857. url: url,
  1858. dataType: "json"
  1859. });
  1860. };
  1861. /**
  1862. Gets artists from an item
  1863. */
  1864. self.getArtists = function (userId, options) {
  1865. if (!userId) {
  1866. throw new Error("null userId");
  1867. }
  1868. options = options || {};
  1869. options.userId = userId;
  1870. var url = self.getUrl("Artists", options);
  1871. return self.ajax({
  1872. type: "GET",
  1873. url: url,
  1874. dataType: "json"
  1875. });
  1876. };
  1877. /**
  1878. Gets genres from an item
  1879. */
  1880. self.getGenres = function (userId, options) {
  1881. if (!userId) {
  1882. throw new Error("null userId");
  1883. }
  1884. options = options || {};
  1885. options.userId = userId;
  1886. var url = self.getUrl("Genres", options);
  1887. return self.ajax({
  1888. type: "GET",
  1889. url: url,
  1890. dataType: "json"
  1891. });
  1892. };
  1893. self.getMusicGenres = function (userId, options) {
  1894. if (!userId) {
  1895. throw new Error("null userId");
  1896. }
  1897. options = options || {};
  1898. options.userId = userId;
  1899. var url = self.getUrl("MusicGenres", options);
  1900. return self.ajax({
  1901. type: "GET",
  1902. url: url,
  1903. dataType: "json"
  1904. });
  1905. };
  1906. self.getGameGenres = function (userId, options) {
  1907. if (!userId) {
  1908. throw new Error("null userId");
  1909. }
  1910. options = options || {};
  1911. options.userId = userId;
  1912. var url = self.getUrl("GameGenres", options);
  1913. return self.ajax({
  1914. type: "GET",
  1915. url: url,
  1916. dataType: "json"
  1917. });
  1918. };
  1919. /**
  1920. Gets people from an item
  1921. */
  1922. self.getPeople = function (userId, options) {
  1923. if (!userId) {
  1924. throw new Error("null userId");
  1925. }
  1926. options = options || {};
  1927. options.userId = userId;
  1928. var url = self.getUrl("Persons", options);
  1929. return self.ajax({
  1930. type: "GET",
  1931. url: url,
  1932. dataType: "json"
  1933. });
  1934. };
  1935. /**
  1936. Gets studios from an item
  1937. */
  1938. self.getStudios = function (userId, options) {
  1939. if (!userId) {
  1940. throw new Error("null userId");
  1941. }
  1942. options = options || {};
  1943. options.userId = userId;
  1944. var url = self.getUrl("Studios", options);
  1945. return self.ajax({
  1946. type: "GET",
  1947. url: url,
  1948. dataType: "json"
  1949. });
  1950. };
  1951. /**
  1952. * Gets local trailers for an item
  1953. */
  1954. self.getLocalTrailers = function (userId, itemId) {
  1955. if (!userId) {
  1956. throw new Error("null userId");
  1957. }
  1958. if (!itemId) {
  1959. throw new Error("null itemId");
  1960. }
  1961. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/LocalTrailers");
  1962. return self.ajax({
  1963. type: "GET",
  1964. url: url,
  1965. dataType: "json"
  1966. });
  1967. };
  1968. self.getAdditionalVideoParts = function (userId, itemId) {
  1969. if (!itemId) {
  1970. throw new Error("null itemId");
  1971. }
  1972. var options = {};
  1973. if (userId) {
  1974. options.userId = userId;
  1975. }
  1976. var url = self.getUrl("Videos/" + itemId + "/AdditionalParts", options);
  1977. return self.ajax({
  1978. type: "GET",
  1979. url: url,
  1980. dataType: "json"
  1981. });
  1982. };
  1983. /**
  1984. * Gets theme songs for an item
  1985. */
  1986. self.getThemeSongs = function (userId, itemId) {
  1987. if (!itemId) {
  1988. throw new Error("null itemId");
  1989. }
  1990. var options = {};
  1991. if (userId) {
  1992. options.userId = userId;
  1993. }
  1994. var url = self.getUrl("Items/" + itemId + "/ThemeSongs", options);
  1995. return self.ajax({
  1996. type: "GET",
  1997. url: url,
  1998. dataType: "json"
  1999. });
  2000. };
  2001. self.getThemeVideos = function (userId, itemId) {
  2002. if (!itemId) {
  2003. throw new Error("null itemId");
  2004. }
  2005. var options = {};
  2006. if (userId) {
  2007. options.userId = userId;
  2008. }
  2009. var url = self.getUrl("Items/" + itemId + "/ThemeVideos", options);
  2010. return self.ajax({
  2011. type: "GET",
  2012. url: url,
  2013. dataType: "json"
  2014. });
  2015. };
  2016. self.getSearchHints = function (options) {
  2017. var url = self.getUrl("Search/Hints", options);
  2018. return self.ajax({
  2019. type: "GET",
  2020. url: url,
  2021. dataType: "json"
  2022. });
  2023. };
  2024. /**
  2025. * Gets special features for an item
  2026. */
  2027. self.getSpecialFeatures = function (userId, itemId) {
  2028. if (!userId) {
  2029. throw new Error("null userId");
  2030. }
  2031. if (!itemId) {
  2032. throw new Error("null itemId");
  2033. }
  2034. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/SpecialFeatures");
  2035. return self.ajax({
  2036. type: "GET",
  2037. url: url,
  2038. dataType: "json"
  2039. });
  2040. };
  2041. /**
  2042. * Marks an item as played or unplayed
  2043. * This should not be used to update playstate following playback.
  2044. * There are separate playstate check-in methods for that. This should be used for a
  2045. * separate option to reset playstate.
  2046. * @param {String} userId
  2047. * @param {String} itemId
  2048. * @param {Boolean} wasPlayed
  2049. */
  2050. self.updatePlayedStatus = function (userId, itemId, wasPlayed) {
  2051. if (!userId) {
  2052. throw new Error("null userId");
  2053. }
  2054. if (!itemId) {
  2055. throw new Error("null itemId");
  2056. }
  2057. var url = self.getUrl("Users/" + userId + "/PlayedItems/" + itemId);
  2058. var method = wasPlayed ? "POST" : "DELETE";
  2059. return self.ajax({
  2060. type: method,
  2061. url: url
  2062. });
  2063. };
  2064. /**
  2065. * Updates a user's favorite status for an item.
  2066. * @param {String} userId
  2067. * @param {String} itemId
  2068. * @param {Boolean} isFavorite
  2069. */
  2070. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  2071. if (!userId) {
  2072. throw new Error("null userId");
  2073. }
  2074. if (!itemId) {
  2075. throw new Error("null itemId");
  2076. }
  2077. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  2078. var method = isFavorite ? "POST" : "DELETE";
  2079. return self.ajax({
  2080. type: method,
  2081. url: url
  2082. });
  2083. };
  2084. /**
  2085. * Updates a user's personal rating for an item
  2086. * @param {String} userId
  2087. * @param {String} itemId
  2088. * @param {Boolean} likes
  2089. */
  2090. self.updateUserItemRating = function (userId, itemId, likes) {
  2091. if (!userId) {
  2092. throw new Error("null userId");
  2093. }
  2094. if (!itemId) {
  2095. throw new Error("null itemId");
  2096. }
  2097. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  2098. likes: likes
  2099. });
  2100. return self.ajax({
  2101. type: "POST",
  2102. url: url
  2103. });
  2104. };
  2105. /**
  2106. * Updates a user's favorite status for an item by name.
  2107. * @param {String} userId
  2108. * @param {String} name
  2109. * @param {Boolean} isFavorite
  2110. */
  2111. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  2112. if (!userId) {
  2113. throw new Error("null userId");
  2114. }
  2115. if (!name) {
  2116. throw new Error("null name");
  2117. }
  2118. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  2119. var method = isFavorite ? "POST" : "DELETE";
  2120. return self.ajax({
  2121. type: method,
  2122. url: url
  2123. });
  2124. };
  2125. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  2126. if (!userId) {
  2127. throw new Error("null userId");
  2128. }
  2129. if (!name) {
  2130. throw new Error("null name");
  2131. }
  2132. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  2133. var method = isFavorite ? "POST" : "DELETE";
  2134. return self.ajax({
  2135. type: method,
  2136. url: url
  2137. });
  2138. };
  2139. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  2140. if (!userId) {
  2141. throw new Error("null userId");
  2142. }
  2143. if (!name) {
  2144. throw new Error("null name");
  2145. }
  2146. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  2147. var method = isFavorite ? "POST" : "DELETE";
  2148. return self.ajax({
  2149. type: method,
  2150. url: url
  2151. });
  2152. };
  2153. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  2154. if (!userId) {
  2155. throw new Error("null userId");
  2156. }
  2157. if (!name) {
  2158. throw new Error("null name");
  2159. }
  2160. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  2161. var method = isFavorite ? "POST" : "DELETE";
  2162. return self.ajax({
  2163. type: method,
  2164. url: url
  2165. });
  2166. };
  2167. self.updateFavoriteMusicGenreStatus = function (userId, name, isFavorite) {
  2168. if (!userId) {
  2169. throw new Error("null userId");
  2170. }
  2171. if (!name) {
  2172. throw new Error("null name");
  2173. }
  2174. var url = self.getUrl("Users/" + userId + "/Favorites/MusicGenres/" + self.encodeName(name));
  2175. var method = isFavorite ? "POST" : "DELETE";
  2176. return self.ajax({
  2177. type: method,
  2178. url: url
  2179. });
  2180. };
  2181. self.updateFavoriteGameGenreStatus = function (userId, name, isFavorite) {
  2182. if (!userId) {
  2183. throw new Error("null userId");
  2184. }
  2185. if (!name) {
  2186. throw new Error("null name");
  2187. }
  2188. var url = self.getUrl("Users/" + userId + "/Favorites/GameGenres/" + self.encodeName(name));
  2189. var method = isFavorite ? "POST" : "DELETE";
  2190. return self.ajax({
  2191. type: method,
  2192. url: url
  2193. });
  2194. };
  2195. /**
  2196. * Updates a user's rating for an item by name.
  2197. * @param {String} userId
  2198. * @param {String} name
  2199. * @param {Boolean} likes
  2200. */
  2201. self.updateArtistRating = function (userId, name, likes) {
  2202. if (!userId) {
  2203. throw new Error("null userId");
  2204. }
  2205. if (!name) {
  2206. throw new Error("null name");
  2207. }
  2208. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  2209. likes: likes
  2210. });
  2211. return self.ajax({
  2212. type: "POST",
  2213. url: url
  2214. });
  2215. };
  2216. self.updatePersonRating = function (userId, name, likes) {
  2217. if (!userId) {
  2218. throw new Error("null userId");
  2219. }
  2220. if (!name) {
  2221. throw new Error("null name");
  2222. }
  2223. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  2224. likes: likes
  2225. });
  2226. return self.ajax({
  2227. type: "POST",
  2228. url: url
  2229. });
  2230. };
  2231. self.updateStudioRating = function (userId, name, likes) {
  2232. if (!userId) {
  2233. throw new Error("null userId");
  2234. }
  2235. if (!name) {
  2236. throw new Error("null name");
  2237. }
  2238. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  2239. likes: likes
  2240. });
  2241. return self.ajax({
  2242. type: "POST",
  2243. url: url
  2244. });
  2245. };
  2246. self.updateGenreRating = function (userId, name, likes) {
  2247. if (!userId) {
  2248. throw new Error("null userId");
  2249. }
  2250. if (!name) {
  2251. throw new Error("null name");
  2252. }
  2253. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  2254. likes: likes
  2255. });
  2256. return self.ajax({
  2257. type: "POST",
  2258. url: url
  2259. });
  2260. };
  2261. self.updateMusicGenreRating = function (userId, name, likes) {
  2262. if (!userId) {
  2263. throw new Error("null userId");
  2264. }
  2265. if (!name) {
  2266. throw new Error("null name");
  2267. }
  2268. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name), {
  2269. likes: likes
  2270. });
  2271. return self.ajax({
  2272. type: "POST",
  2273. url: url
  2274. });
  2275. };
  2276. self.updateGameGenreRating = function (userId, name, likes) {
  2277. if (!userId) {
  2278. throw new Error("null userId");
  2279. }
  2280. if (!name) {
  2281. throw new Error("null name");
  2282. }
  2283. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name), {
  2284. likes: likes
  2285. });
  2286. return self.ajax({
  2287. type: "POST",
  2288. url: url
  2289. });
  2290. };
  2291. /**
  2292. * Clears a user's rating for an item by name.
  2293. * @param {String} userId
  2294. * @param {String} name
  2295. */
  2296. self.clearArtistRating = function (userId, name) {
  2297. if (!userId) {
  2298. throw new Error("null userId");
  2299. }
  2300. if (!name) {
  2301. throw new Error("null name");
  2302. }
  2303. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  2304. return self.ajax({
  2305. type: "DELETE",
  2306. url: url
  2307. });
  2308. };
  2309. self.clearPersonRating = function (userId, name) {
  2310. if (!userId) {
  2311. throw new Error("null userId");
  2312. }
  2313. if (!name) {
  2314. throw new Error("null name");
  2315. }
  2316. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  2317. return self.ajax({
  2318. type: "DELETE",
  2319. url: url
  2320. });
  2321. };
  2322. self.clearStudioRating = function (userId, name) {
  2323. if (!userId) {
  2324. throw new Error("null userId");
  2325. }
  2326. if (!name) {
  2327. throw new Error("null name");
  2328. }
  2329. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  2330. return self.ajax({
  2331. type: "DELETE",
  2332. url: url
  2333. });
  2334. };
  2335. self.clearGenreRating = function (userId, name) {
  2336. if (!userId) {
  2337. throw new Error("null userId");
  2338. }
  2339. if (!name) {
  2340. throw new Error("null name");
  2341. }
  2342. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  2343. return self.ajax({
  2344. type: "DELETE",
  2345. url: url
  2346. });
  2347. };
  2348. self.clearMusicGenreRating = function (userId, name) {
  2349. if (!userId) {
  2350. throw new Error("null userId");
  2351. }
  2352. if (!name) {
  2353. throw new Error("null name");
  2354. }
  2355. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name));
  2356. return self.ajax({
  2357. type: "DELETE",
  2358. url: url
  2359. });
  2360. };
  2361. self.clearGameGenreRating = function (userId, name) {
  2362. if (!userId) {
  2363. throw new Error("null userId");
  2364. }
  2365. if (!name) {
  2366. throw new Error("null name");
  2367. }
  2368. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name));
  2369. return self.ajax({
  2370. type: "DELETE",
  2371. url: url
  2372. });
  2373. };
  2374. self.getItemCounts = function (userId) {
  2375. var options = {};
  2376. if (userId) {
  2377. options.userId = userId;
  2378. }
  2379. var url = self.getUrl("Items/Counts", options);
  2380. return self.ajax({
  2381. type: "GET",
  2382. url: url,
  2383. dataType: "json"
  2384. });
  2385. };
  2386. /**
  2387. Gets a variety of item counts that a person appears in
  2388. */
  2389. self.getPersonItemCounts = function (userId, name) {
  2390. if (!userId) {
  2391. throw new Error("null userId");
  2392. }
  2393. if (!name) {
  2394. throw new Error("null name");
  2395. }
  2396. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  2397. userId: userId
  2398. });
  2399. return self.ajax({
  2400. type: "GET",
  2401. url: url,
  2402. dataType: "json"
  2403. });
  2404. };
  2405. /**
  2406. Gets a variety of item counts that a genre appears in
  2407. */
  2408. self.getGenreItemCounts = function (userId, name) {
  2409. if (!userId) {
  2410. throw new Error("null userId");
  2411. }
  2412. if (!name) {
  2413. throw new Error("null name");
  2414. }
  2415. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  2416. userId: userId
  2417. });
  2418. return self.ajax({
  2419. type: "GET",
  2420. url: url,
  2421. dataType: "json"
  2422. });
  2423. };
  2424. self.getMusicGenreItemCounts = function (userId, name) {
  2425. if (!userId) {
  2426. throw new Error("null userId");
  2427. }
  2428. if (!name) {
  2429. throw new Error("null name");
  2430. }
  2431. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/Counts", {
  2432. userId: userId
  2433. });
  2434. return self.ajax({
  2435. type: "GET",
  2436. url: url,
  2437. dataType: "json"
  2438. });
  2439. };
  2440. self.getGameGenreItemCounts = function (userId, name) {
  2441. if (!userId) {
  2442. throw new Error("null userId");
  2443. }
  2444. if (!name) {
  2445. throw new Error("null name");
  2446. }
  2447. var url = self.getUrl("GameGenres/" + self.encodeName(name) + "/Counts", {
  2448. userId: userId
  2449. });
  2450. return self.ajax({
  2451. type: "GET",
  2452. url: url,
  2453. dataType: "json"
  2454. });
  2455. };
  2456. /**
  2457. Gets a variety of item counts that an artist appears in
  2458. */
  2459. self.getArtistItemCounts = function (userId, name) {
  2460. if (!userId) {
  2461. throw new Error("null userId");
  2462. }
  2463. if (!name) {
  2464. throw new Error("null name");
  2465. }
  2466. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  2467. userId: userId
  2468. });
  2469. return self.ajax({
  2470. type: "GET",
  2471. url: url,
  2472. dataType: "json"
  2473. });
  2474. };
  2475. /**
  2476. Gets a variety of item counts that a studio appears in
  2477. */
  2478. self.getStudioItemCounts = function (userId, name) {
  2479. if (!userId) {
  2480. throw new Error("null userId");
  2481. }
  2482. if (!name) {
  2483. throw new Error("null name");
  2484. }
  2485. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  2486. userId: userId
  2487. });
  2488. return self.ajax({
  2489. type: "GET",
  2490. url: url,
  2491. dataType: "json"
  2492. });
  2493. };
  2494. /**
  2495. * Clears a user's personal rating for an item
  2496. * @param {String} userId
  2497. * @param {String} itemId
  2498. */
  2499. self.clearUserItemRating = function (userId, itemId) {
  2500. if (!userId) {
  2501. throw new Error("null userId");
  2502. }
  2503. if (!itemId) {
  2504. throw new Error("null itemId");
  2505. }
  2506. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  2507. return self.ajax({
  2508. type: "DELETE",
  2509. url: url
  2510. });
  2511. };
  2512. /**
  2513. * Reports the user has started playing something
  2514. * @param {String} userId
  2515. * @param {String} itemId
  2516. */
  2517. self.reportPlaybackStart = function (userId, itemId) {
  2518. if (!userId) {
  2519. throw new Error("null userId");
  2520. }
  2521. if (!itemId) {
  2522. throw new Error("null itemId");
  2523. }
  2524. if (self.isWebSocketOpen()) {
  2525. var deferred = $.Deferred();
  2526. self.sendWebSocketMessage("PlaybackStart", itemId);
  2527. deferred.resolveWith(null, []);
  2528. return deferred.promise();
  2529. }
  2530. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  2531. return self.ajax({
  2532. type: "POST",
  2533. url: url
  2534. });
  2535. };
  2536. /**
  2537. * Reports progress viewing an item
  2538. * @param {String} userId
  2539. * @param {String} itemId
  2540. */
  2541. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused) {
  2542. if (!userId) {
  2543. throw new Error("null userId");
  2544. }
  2545. if (!itemId) {
  2546. throw new Error("null itemId");
  2547. }
  2548. if (self.isWebSocketOpen()) {
  2549. var deferred = $.Deferred();
  2550. self.sendWebSocketMessage("PlaybackProgress", itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused));
  2551. deferred.resolveWith(null, []);
  2552. return deferred.promise();
  2553. }
  2554. var params = {
  2555. };
  2556. if (positionTicks) {
  2557. params.positionTicks = positionTicks;
  2558. }
  2559. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  2560. return self.ajax({
  2561. type: "POST",
  2562. url: url
  2563. });
  2564. };
  2565. /**
  2566. * Reports a user has stopped playing an item
  2567. * @param {String} userId
  2568. * @param {String} itemId
  2569. */
  2570. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  2571. if (!userId) {
  2572. throw new Error("null userId");
  2573. }
  2574. if (!itemId) {
  2575. throw new Error("null itemId");
  2576. }
  2577. if (self.isWebSocketOpen()) {
  2578. var deferred = $.Deferred();
  2579. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  2580. deferred.resolveWith(null, []);
  2581. return deferred.promise();
  2582. }
  2583. var params = {
  2584. };
  2585. if (positionTicks) {
  2586. params.positionTicks = positionTicks;
  2587. }
  2588. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2589. return self.ajax({
  2590. type: "DELETE",
  2591. url: url
  2592. });
  2593. };
  2594. self.sendBrowseCommand = function (sessionId, options) {
  2595. if (!sessionId) {
  2596. throw new Error("null sessionId");
  2597. }
  2598. if (!options) {
  2599. throw new Error("null options");
  2600. }
  2601. var url = self.getUrl("Sessions/" + sessionId + "/Viewing", options);
  2602. return self.ajax({
  2603. type: "POST",
  2604. url: url
  2605. });
  2606. };
  2607. self.sendPlayCommand = function (sessionId, options) {
  2608. if (!sessionId) {
  2609. throw new Error("null sessionId");
  2610. }
  2611. if (!options) {
  2612. throw new Error("null options");
  2613. }
  2614. var url = self.getUrl("Sessions/" + sessionId + "/Playing", options);
  2615. return self.ajax({
  2616. type: "POST",
  2617. url: url
  2618. });
  2619. };
  2620. self.sendPlayStateCommand = function (sessionId, command, options) {
  2621. if (!sessionId) {
  2622. throw new Error("null sessionId");
  2623. }
  2624. if (!command) {
  2625. throw new Error("null command");
  2626. }
  2627. var url = self.getUrl("Sessions/" + sessionId + "/Playing/" + command, options || {});
  2628. return self.ajax({
  2629. type: "POST",
  2630. url: url
  2631. });
  2632. };
  2633. }
  2634. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  2635. /**
  2636. * Provides a friendly way to create an api client instance using information from the browser's current url
  2637. */
  2638. MediaBrowser.ApiClient.create = function (clientName, applicationVersion) {
  2639. var loc = window.location;
  2640. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName, applicationVersion);
  2641. };
  2642. /**
  2643. *
  2644. * Secure Hash Algorithm (SHA1)
  2645. * http://www.webtoolkit.info/
  2646. *
  2647. **/
  2648. MediaBrowser.SHA1 = function (msg) {
  2649. function rotate_left(n, s) {
  2650. var t4 = (n << s) | (n >>> (32 - s));
  2651. return t4;
  2652. }
  2653. function lsb_hex(val) {
  2654. var str = "";
  2655. var i;
  2656. var vh;
  2657. var vl;
  2658. for (i = 0; i <= 6; i += 2) {
  2659. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2660. vl = (val >>> (i * 4)) & 0x0f;
  2661. str += vh.toString(16) + vl.toString(16);
  2662. }
  2663. return str;
  2664. }
  2665. function cvt_hex(val) {
  2666. var str = "";
  2667. var i;
  2668. var v;
  2669. for (i = 7; i >= 0; i--) {
  2670. v = (val >>> (i * 4)) & 0x0f;
  2671. str += v.toString(16);
  2672. }
  2673. return str;
  2674. }
  2675. function Utf8Encode(string) {
  2676. string = string.replace(/\r\n/g, "\n");
  2677. var utftext = "";
  2678. for (var n = 0; n < string.length; n++) {
  2679. var c = string.charCodeAt(n);
  2680. if (c < 128) {
  2681. utftext += String.fromCharCode(c);
  2682. }
  2683. else if ((c > 127) && (c < 2048)) {
  2684. utftext += String.fromCharCode((c >> 6) | 192);
  2685. utftext += String.fromCharCode((c & 63) | 128);
  2686. }
  2687. else {
  2688. utftext += String.fromCharCode((c >> 12) | 224);
  2689. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2690. utftext += String.fromCharCode((c & 63) | 128);
  2691. }
  2692. }
  2693. return utftext;
  2694. }
  2695. var blockstart;
  2696. var i, j;
  2697. var W = new Array(80);
  2698. var H0 = 0x67452301;
  2699. var H1 = 0xEFCDAB89;
  2700. var H2 = 0x98BADCFE;
  2701. var H3 = 0x10325476;
  2702. var H4 = 0xC3D2E1F0;
  2703. var A, B, C, D, E;
  2704. var temp;
  2705. msg = Utf8Encode(msg);
  2706. var msg_len = msg.length;
  2707. var word_array = new Array();
  2708. for (i = 0; i < msg_len - 3; i += 4) {
  2709. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2710. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2711. word_array.push(j);
  2712. }
  2713. switch (msg_len % 4) {
  2714. case 0:
  2715. i = 0x080000000;
  2716. break;
  2717. case 1:
  2718. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2719. break;
  2720. case 2:
  2721. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2722. break;
  2723. case 3:
  2724. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2725. break;
  2726. }
  2727. word_array.push(i);
  2728. while ((word_array.length % 16) != 14) word_array.push(0);
  2729. word_array.push(msg_len >>> 29);
  2730. word_array.push((msg_len << 3) & 0x0ffffffff);
  2731. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2732. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2733. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2734. A = H0;
  2735. B = H1;
  2736. C = H2;
  2737. D = H3;
  2738. E = H4;
  2739. for (i = 0; i <= 19; i++) {
  2740. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2741. E = D;
  2742. D = C;
  2743. C = rotate_left(B, 30);
  2744. B = A;
  2745. A = temp;
  2746. }
  2747. for (i = 20; i <= 39; i++) {
  2748. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2749. E = D;
  2750. D = C;
  2751. C = rotate_left(B, 30);
  2752. B = A;
  2753. A = temp;
  2754. }
  2755. for (i = 40; i <= 59; i++) {
  2756. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2757. E = D;
  2758. D = C;
  2759. C = rotate_left(B, 30);
  2760. B = A;
  2761. A = temp;
  2762. }
  2763. for (i = 60; i <= 79; i++) {
  2764. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2765. E = D;
  2766. D = C;
  2767. C = rotate_left(B, 30);
  2768. B = A;
  2769. A = temp;
  2770. }
  2771. H0 = (H0 + A) & 0x0ffffffff;
  2772. H1 = (H1 + B) & 0x0ffffffff;
  2773. H2 = (H2 + C) & 0x0ffffffff;
  2774. H3 = (H3 + D) & 0x0ffffffff;
  2775. H4 = (H4 + E) & 0x0ffffffff;
  2776. }
  2777. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2778. return temp.toLowerCase();
  2779. };
  2780. (function (jQuery, window, undefined) {
  2781. "use strict";
  2782. var matched, browser;
  2783. jQuery.uaMatch = function (ua) {
  2784. ua = ua.toLowerCase();
  2785. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2786. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2787. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2788. /(msie) ([\w.]+)/.exec(ua) ||
  2789. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2790. [];
  2791. var platform_match = /(ipad)/.exec(ua) ||
  2792. /(iphone)/.exec(ua) ||
  2793. /(android)/.exec(ua) ||
  2794. [];
  2795. var browser = match[1] || "";
  2796. if (ua.indexOf("like gecko") != -1 && ua.indexOf('webkit') == -1 && ua.indexOf('opera') == -1) {
  2797. browser = "msie";
  2798. }
  2799. return {
  2800. browser: browser,
  2801. version: match[2] || "0",
  2802. platform: platform_match[0] || ""
  2803. };
  2804. };
  2805. matched = jQuery.uaMatch(window.navigator.userAgent);
  2806. browser = {};
  2807. if (matched.browser) {
  2808. browser[matched.browser] = true;
  2809. browser.version = matched.version;
  2810. }
  2811. if (matched.platform) {
  2812. browser[matched.platform] = true
  2813. }
  2814. // Chrome is Webkit, but Webkit is also Safari.
  2815. if (browser.chrome) {
  2816. browser.webkit = true;
  2817. } else if (browser.webkit) {
  2818. browser.safari = true;
  2819. }
  2820. jQuery.browser = browser;
  2821. })(jQuery, window);