ApiClient.js 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765
  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. dataType: "json"
  2063. });
  2064. };
  2065. /**
  2066. * Updates a user's favorite status for an item.
  2067. * @param {String} userId
  2068. * @param {String} itemId
  2069. * @param {Boolean} isFavorite
  2070. */
  2071. self.updateFavoriteStatus = function (userId, itemId, isFavorite) {
  2072. if (!userId) {
  2073. throw new Error("null userId");
  2074. }
  2075. if (!itemId) {
  2076. throw new Error("null itemId");
  2077. }
  2078. var url = self.getUrl("Users/" + userId + "/FavoriteItems/" + itemId);
  2079. var method = isFavorite ? "POST" : "DELETE";
  2080. return self.ajax({
  2081. type: method,
  2082. url: url,
  2083. dataType: "json"
  2084. });
  2085. };
  2086. /**
  2087. * Updates a user's personal rating for an item
  2088. * @param {String} userId
  2089. * @param {String} itemId
  2090. * @param {Boolean} likes
  2091. */
  2092. self.updateUserItemRating = function (userId, itemId, likes) {
  2093. if (!userId) {
  2094. throw new Error("null userId");
  2095. }
  2096. if (!itemId) {
  2097. throw new Error("null itemId");
  2098. }
  2099. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating", {
  2100. likes: likes
  2101. });
  2102. return self.ajax({
  2103. type: "POST",
  2104. url: url,
  2105. dataType: "json"
  2106. });
  2107. };
  2108. /**
  2109. * Updates a user's favorite status for an item by name.
  2110. * @param {String} userId
  2111. * @param {String} name
  2112. * @param {Boolean} isFavorite
  2113. */
  2114. self.updateFavoriteArtistStatus = function (userId, name, isFavorite) {
  2115. if (!userId) {
  2116. throw new Error("null userId");
  2117. }
  2118. if (!name) {
  2119. throw new Error("null name");
  2120. }
  2121. var url = self.getUrl("Users/" + userId + "/Favorites/Artists/" + self.encodeName(name));
  2122. var method = isFavorite ? "POST" : "DELETE";
  2123. return self.ajax({
  2124. type: method,
  2125. url: url,
  2126. dataType: "json"
  2127. });
  2128. };
  2129. self.updateFavoritePersonStatus = function (userId, name, isFavorite) {
  2130. if (!userId) {
  2131. throw new Error("null userId");
  2132. }
  2133. if (!name) {
  2134. throw new Error("null name");
  2135. }
  2136. var url = self.getUrl("Users/" + userId + "/Favorites/Persons/" + self.encodeName(name));
  2137. var method = isFavorite ? "POST" : "DELETE";
  2138. return self.ajax({
  2139. type: method,
  2140. url: url,
  2141. dataType: "json"
  2142. });
  2143. };
  2144. self.updateFavoriteStudioStatus = function (userId, name, isFavorite) {
  2145. if (!userId) {
  2146. throw new Error("null userId");
  2147. }
  2148. if (!name) {
  2149. throw new Error("null name");
  2150. }
  2151. var url = self.getUrl("Users/" + userId + "/Favorites/Studios/" + self.encodeName(name));
  2152. var method = isFavorite ? "POST" : "DELETE";
  2153. return self.ajax({
  2154. type: method,
  2155. url: url,
  2156. dataType: "json"
  2157. });
  2158. };
  2159. self.updateFavoriteGenreStatus = function (userId, name, isFavorite) {
  2160. if (!userId) {
  2161. throw new Error("null userId");
  2162. }
  2163. if (!name) {
  2164. throw new Error("null name");
  2165. }
  2166. var url = self.getUrl("Users/" + userId + "/Favorites/Genres/" + self.encodeName(name));
  2167. var method = isFavorite ? "POST" : "DELETE";
  2168. return self.ajax({
  2169. type: method,
  2170. url: url,
  2171. dataType: "json"
  2172. });
  2173. };
  2174. self.updateFavoriteMusicGenreStatus = function (userId, name, isFavorite) {
  2175. if (!userId) {
  2176. throw new Error("null userId");
  2177. }
  2178. if (!name) {
  2179. throw new Error("null name");
  2180. }
  2181. var url = self.getUrl("Users/" + userId + "/Favorites/MusicGenres/" + self.encodeName(name));
  2182. var method = isFavorite ? "POST" : "DELETE";
  2183. return self.ajax({
  2184. type: method,
  2185. url: url,
  2186. dataType: "json"
  2187. });
  2188. };
  2189. self.updateFavoriteGameGenreStatus = function (userId, name, isFavorite) {
  2190. if (!userId) {
  2191. throw new Error("null userId");
  2192. }
  2193. if (!name) {
  2194. throw new Error("null name");
  2195. }
  2196. var url = self.getUrl("Users/" + userId + "/Favorites/GameGenres/" + self.encodeName(name));
  2197. var method = isFavorite ? "POST" : "DELETE";
  2198. return self.ajax({
  2199. type: method,
  2200. url: url,
  2201. dataType: "json"
  2202. });
  2203. };
  2204. /**
  2205. * Updates a user's rating for an item by name.
  2206. * @param {String} userId
  2207. * @param {String} name
  2208. * @param {Boolean} likes
  2209. */
  2210. self.updateArtistRating = function (userId, name, likes) {
  2211. if (!userId) {
  2212. throw new Error("null userId");
  2213. }
  2214. if (!name) {
  2215. throw new Error("null name");
  2216. }
  2217. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name), {
  2218. likes: likes
  2219. });
  2220. return self.ajax({
  2221. type: "POST",
  2222. url: url,
  2223. dataType: "json"
  2224. });
  2225. };
  2226. self.updatePersonRating = function (userId, name, likes) {
  2227. if (!userId) {
  2228. throw new Error("null userId");
  2229. }
  2230. if (!name) {
  2231. throw new Error("null name");
  2232. }
  2233. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name), {
  2234. likes: likes
  2235. });
  2236. return self.ajax({
  2237. type: "POST",
  2238. url: url,
  2239. dataType: "json"
  2240. });
  2241. };
  2242. self.updateStudioRating = function (userId, name, likes) {
  2243. if (!userId) {
  2244. throw new Error("null userId");
  2245. }
  2246. if (!name) {
  2247. throw new Error("null name");
  2248. }
  2249. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name), {
  2250. likes: likes
  2251. });
  2252. return self.ajax({
  2253. type: "POST",
  2254. url: url,
  2255. dataType: "json"
  2256. });
  2257. };
  2258. self.updateGenreRating = function (userId, name, likes) {
  2259. if (!userId) {
  2260. throw new Error("null userId");
  2261. }
  2262. if (!name) {
  2263. throw new Error("null name");
  2264. }
  2265. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name), {
  2266. likes: likes
  2267. });
  2268. return self.ajax({
  2269. type: "POST",
  2270. url: url,
  2271. dataType: "json"
  2272. });
  2273. };
  2274. self.updateMusicGenreRating = function (userId, name, likes) {
  2275. if (!userId) {
  2276. throw new Error("null userId");
  2277. }
  2278. if (!name) {
  2279. throw new Error("null name");
  2280. }
  2281. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name), {
  2282. likes: likes
  2283. });
  2284. return self.ajax({
  2285. type: "POST",
  2286. url: url,
  2287. dataType: "json"
  2288. });
  2289. };
  2290. self.updateGameGenreRating = function (userId, name, likes) {
  2291. if (!userId) {
  2292. throw new Error("null userId");
  2293. }
  2294. if (!name) {
  2295. throw new Error("null name");
  2296. }
  2297. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name), {
  2298. likes: likes
  2299. });
  2300. return self.ajax({
  2301. type: "POST",
  2302. url: url,
  2303. dataType: "json"
  2304. });
  2305. };
  2306. /**
  2307. * Clears a user's rating for an item by name.
  2308. * @param {String} userId
  2309. * @param {String} name
  2310. */
  2311. self.clearArtistRating = function (userId, name) {
  2312. if (!userId) {
  2313. throw new Error("null userId");
  2314. }
  2315. if (!name) {
  2316. throw new Error("null name");
  2317. }
  2318. var url = self.getUrl("Users/" + userId + "/Ratings/Artists/" + self.encodeName(name));
  2319. return self.ajax({
  2320. type: "DELETE",
  2321. url: url,
  2322. dataType: "json"
  2323. });
  2324. };
  2325. self.clearPersonRating = function (userId, name) {
  2326. if (!userId) {
  2327. throw new Error("null userId");
  2328. }
  2329. if (!name) {
  2330. throw new Error("null name");
  2331. }
  2332. var url = self.getUrl("Users/" + userId + "/Ratings/Persons/" + self.encodeName(name));
  2333. return self.ajax({
  2334. type: "DELETE",
  2335. url: url,
  2336. dataType: "json"
  2337. });
  2338. };
  2339. self.clearStudioRating = function (userId, name) {
  2340. if (!userId) {
  2341. throw new Error("null userId");
  2342. }
  2343. if (!name) {
  2344. throw new Error("null name");
  2345. }
  2346. var url = self.getUrl("Users/" + userId + "/Ratings/Studios/" + self.encodeName(name));
  2347. return self.ajax({
  2348. type: "DELETE",
  2349. url: url,
  2350. dataType: "json"
  2351. });
  2352. };
  2353. self.clearGenreRating = function (userId, name) {
  2354. if (!userId) {
  2355. throw new Error("null userId");
  2356. }
  2357. if (!name) {
  2358. throw new Error("null name");
  2359. }
  2360. var url = self.getUrl("Users/" + userId + "/Ratings/Genres/" + self.encodeName(name));
  2361. return self.ajax({
  2362. type: "DELETE",
  2363. url: url,
  2364. dataType: "json"
  2365. });
  2366. };
  2367. self.clearMusicGenreRating = function (userId, name) {
  2368. if (!userId) {
  2369. throw new Error("null userId");
  2370. }
  2371. if (!name) {
  2372. throw new Error("null name");
  2373. }
  2374. var url = self.getUrl("Users/" + userId + "/Ratings/MusicGenres/" + self.encodeName(name));
  2375. return self.ajax({
  2376. type: "DELETE",
  2377. url: url,
  2378. dataType: "json"
  2379. });
  2380. };
  2381. self.clearGameGenreRating = function (userId, name) {
  2382. if (!userId) {
  2383. throw new Error("null userId");
  2384. }
  2385. if (!name) {
  2386. throw new Error("null name");
  2387. }
  2388. var url = self.getUrl("Users/" + userId + "/Ratings/GameGenres/" + self.encodeName(name));
  2389. return self.ajax({
  2390. type: "DELETE",
  2391. url: url,
  2392. dataType: "json"
  2393. });
  2394. };
  2395. self.getItemCounts = function (userId) {
  2396. var options = {};
  2397. if (userId) {
  2398. options.userId = userId;
  2399. }
  2400. var url = self.getUrl("Items/Counts", options);
  2401. return self.ajax({
  2402. type: "GET",
  2403. url: url,
  2404. dataType: "json"
  2405. });
  2406. };
  2407. /**
  2408. Gets a variety of item counts that a person appears in
  2409. */
  2410. self.getPersonItemCounts = function (userId, name) {
  2411. if (!userId) {
  2412. throw new Error("null userId");
  2413. }
  2414. if (!name) {
  2415. throw new Error("null name");
  2416. }
  2417. var url = self.getUrl("Persons/" + self.encodeName(name) + "/Counts", {
  2418. userId: userId
  2419. });
  2420. return self.ajax({
  2421. type: "GET",
  2422. url: url,
  2423. dataType: "json"
  2424. });
  2425. };
  2426. /**
  2427. Gets a variety of item counts that a genre appears in
  2428. */
  2429. self.getGenreItemCounts = function (userId, name) {
  2430. if (!userId) {
  2431. throw new Error("null userId");
  2432. }
  2433. if (!name) {
  2434. throw new Error("null name");
  2435. }
  2436. var url = self.getUrl("Genres/" + self.encodeName(name) + "/Counts", {
  2437. userId: userId
  2438. });
  2439. return self.ajax({
  2440. type: "GET",
  2441. url: url,
  2442. dataType: "json"
  2443. });
  2444. };
  2445. self.getMusicGenreItemCounts = function (userId, name) {
  2446. if (!userId) {
  2447. throw new Error("null userId");
  2448. }
  2449. if (!name) {
  2450. throw new Error("null name");
  2451. }
  2452. var url = self.getUrl("MusicGenres/" + self.encodeName(name) + "/Counts", {
  2453. userId: userId
  2454. });
  2455. return self.ajax({
  2456. type: "GET",
  2457. url: url,
  2458. dataType: "json"
  2459. });
  2460. };
  2461. self.getGameGenreItemCounts = function (userId, name) {
  2462. if (!userId) {
  2463. throw new Error("null userId");
  2464. }
  2465. if (!name) {
  2466. throw new Error("null name");
  2467. }
  2468. var url = self.getUrl("GameGenres/" + self.encodeName(name) + "/Counts", {
  2469. userId: userId
  2470. });
  2471. return self.ajax({
  2472. type: "GET",
  2473. url: url,
  2474. dataType: "json"
  2475. });
  2476. };
  2477. /**
  2478. Gets a variety of item counts that an artist appears in
  2479. */
  2480. self.getArtistItemCounts = function (userId, name) {
  2481. if (!userId) {
  2482. throw new Error("null userId");
  2483. }
  2484. if (!name) {
  2485. throw new Error("null name");
  2486. }
  2487. var url = self.getUrl("Artists/" + self.encodeName(name) + "/Counts", {
  2488. userId: userId
  2489. });
  2490. return self.ajax({
  2491. type: "GET",
  2492. url: url,
  2493. dataType: "json"
  2494. });
  2495. };
  2496. /**
  2497. Gets a variety of item counts that a studio appears in
  2498. */
  2499. self.getStudioItemCounts = function (userId, name) {
  2500. if (!userId) {
  2501. throw new Error("null userId");
  2502. }
  2503. if (!name) {
  2504. throw new Error("null name");
  2505. }
  2506. var url = self.getUrl("Studios/" + self.encodeName(name) + "/Counts", {
  2507. userId: userId
  2508. });
  2509. return self.ajax({
  2510. type: "GET",
  2511. url: url,
  2512. dataType: "json"
  2513. });
  2514. };
  2515. /**
  2516. * Clears a user's personal rating for an item
  2517. * @param {String} userId
  2518. * @param {String} itemId
  2519. */
  2520. self.clearUserItemRating = function (userId, itemId) {
  2521. if (!userId) {
  2522. throw new Error("null userId");
  2523. }
  2524. if (!itemId) {
  2525. throw new Error("null itemId");
  2526. }
  2527. var url = self.getUrl("Users/" + userId + "/Items/" + itemId + "/Rating");
  2528. return self.ajax({
  2529. type: "DELETE",
  2530. url: url,
  2531. dataType: "json"
  2532. });
  2533. };
  2534. /**
  2535. * Reports the user has started playing something
  2536. * @param {String} userId
  2537. * @param {String} itemId
  2538. */
  2539. self.reportPlaybackStart = function (userId, itemId) {
  2540. if (!userId) {
  2541. throw new Error("null userId");
  2542. }
  2543. if (!itemId) {
  2544. throw new Error("null itemId");
  2545. }
  2546. if (self.isWebSocketOpen()) {
  2547. var deferred = $.Deferred();
  2548. self.sendWebSocketMessage("PlaybackStart", itemId);
  2549. deferred.resolveWith(null, []);
  2550. return deferred.promise();
  2551. }
  2552. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId);
  2553. return self.ajax({
  2554. type: "POST",
  2555. url: url
  2556. });
  2557. };
  2558. /**
  2559. * Reports progress viewing an item
  2560. * @param {String} userId
  2561. * @param {String} itemId
  2562. */
  2563. self.reportPlaybackProgress = function (userId, itemId, positionTicks, isPaused, isMuted) {
  2564. if (!userId) {
  2565. throw new Error("null userId");
  2566. }
  2567. if (!itemId) {
  2568. throw new Error("null itemId");
  2569. }
  2570. if (self.isWebSocketOpen()) {
  2571. var deferred = $.Deferred();
  2572. var msgData = itemId + "|" + (positionTicks == null ? "" : positionTicks) + "|" + (isPaused == null ? "" : isPaused) + "|" + (isMuted == null ? "" : isMuted);
  2573. self.sendWebSocketMessage("PlaybackProgress", msgData);
  2574. deferred.resolveWith(null, []);
  2575. return deferred.promise();
  2576. }
  2577. var params = {
  2578. isPaused: isPaused,
  2579. isMuted: isMuted
  2580. };
  2581. if (positionTicks) {
  2582. params.positionTicks = positionTicks;
  2583. }
  2584. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId + "/Progress", params);
  2585. return self.ajax({
  2586. type: "POST",
  2587. url: url
  2588. });
  2589. };
  2590. /**
  2591. * Reports a user has stopped playing an item
  2592. * @param {String} userId
  2593. * @param {String} itemId
  2594. */
  2595. self.reportPlaybackStopped = function (userId, itemId, positionTicks) {
  2596. if (!userId) {
  2597. throw new Error("null userId");
  2598. }
  2599. if (!itemId) {
  2600. throw new Error("null itemId");
  2601. }
  2602. if (self.isWebSocketOpen()) {
  2603. var deferred = $.Deferred();
  2604. self.sendWebSocketMessage("PlaybackStopped", itemId + "|" + (positionTicks == null ? "" : positionTicks));
  2605. deferred.resolveWith(null, []);
  2606. return deferred.promise();
  2607. }
  2608. var params = {
  2609. };
  2610. if (positionTicks) {
  2611. params.positionTicks = positionTicks;
  2612. }
  2613. var url = self.getUrl("Users/" + userId + "/PlayingItems/" + itemId, params);
  2614. return self.ajax({
  2615. type: "DELETE",
  2616. url: url
  2617. });
  2618. };
  2619. self.sendBrowseCommand = function (sessionId, options) {
  2620. if (!sessionId) {
  2621. throw new Error("null sessionId");
  2622. }
  2623. if (!options) {
  2624. throw new Error("null options");
  2625. }
  2626. var url = self.getUrl("Sessions/" + sessionId + "/Viewing", options);
  2627. return self.ajax({
  2628. type: "POST",
  2629. url: url
  2630. });
  2631. };
  2632. self.sendPlayCommand = function (sessionId, options) {
  2633. if (!sessionId) {
  2634. throw new Error("null sessionId");
  2635. }
  2636. if (!options) {
  2637. throw new Error("null options");
  2638. }
  2639. var url = self.getUrl("Sessions/" + sessionId + "/Playing", options);
  2640. return self.ajax({
  2641. type: "POST",
  2642. url: url
  2643. });
  2644. };
  2645. self.sendSystemCommand = function (sessionId, command) {
  2646. if (!sessionId) {
  2647. throw new Error("null sessionId");
  2648. }
  2649. if (!command) {
  2650. throw new Error("null command");
  2651. }
  2652. var url = self.getUrl("Sessions/" + sessionId + "/System/" + command);
  2653. return self.ajax({
  2654. type: "POST",
  2655. url: url
  2656. });
  2657. };
  2658. self.sendMessageCommand = function (sessionId, options) {
  2659. if (!sessionId) {
  2660. throw new Error("null sessionId");
  2661. }
  2662. if (!options) {
  2663. throw new Error("null options");
  2664. }
  2665. var url = self.getUrl("Sessions/" + sessionId + "/Message", options);
  2666. return self.ajax({
  2667. type: "POST",
  2668. url: url
  2669. });
  2670. };
  2671. self.sendPlayStateCommand = function (sessionId, command, options) {
  2672. if (!sessionId) {
  2673. throw new Error("null sessionId");
  2674. }
  2675. if (!command) {
  2676. throw new Error("null command");
  2677. }
  2678. var url = self.getUrl("Sessions/" + sessionId + "/Playing/" + command, options || {});
  2679. return self.ajax({
  2680. type: "POST",
  2681. url: url
  2682. });
  2683. };
  2684. }
  2685. }(jQuery, navigator, window.JSON, window.WebSocket, setTimeout);
  2686. /**
  2687. * Provides a friendly way to create an api client instance using information from the browser's current url
  2688. */
  2689. MediaBrowser.ApiClient.create = function (clientName, applicationVersion) {
  2690. var loc = window.location;
  2691. return new MediaBrowser.ApiClient(loc.protocol, loc.hostname, loc.port, clientName, applicationVersion);
  2692. };
  2693. /**
  2694. *
  2695. * Secure Hash Algorithm (SHA1)
  2696. * http://www.webtoolkit.info/
  2697. *
  2698. **/
  2699. MediaBrowser.SHA1 = function (msg) {
  2700. function rotate_left(n, s) {
  2701. var t4 = (n << s) | (n >>> (32 - s));
  2702. return t4;
  2703. }
  2704. function lsb_hex(val) {
  2705. var str = "";
  2706. var i;
  2707. var vh;
  2708. var vl;
  2709. for (i = 0; i <= 6; i += 2) {
  2710. vh = (val >>> (i * 4 + 4)) & 0x0f;
  2711. vl = (val >>> (i * 4)) & 0x0f;
  2712. str += vh.toString(16) + vl.toString(16);
  2713. }
  2714. return str;
  2715. }
  2716. function cvt_hex(val) {
  2717. var str = "";
  2718. var i;
  2719. var v;
  2720. for (i = 7; i >= 0; i--) {
  2721. v = (val >>> (i * 4)) & 0x0f;
  2722. str += v.toString(16);
  2723. }
  2724. return str;
  2725. }
  2726. function Utf8Encode(string) {
  2727. string = string.replace(/\r\n/g, "\n");
  2728. var utftext = "";
  2729. for (var n = 0; n < string.length; n++) {
  2730. var c = string.charCodeAt(n);
  2731. if (c < 128) {
  2732. utftext += String.fromCharCode(c);
  2733. }
  2734. else if ((c > 127) && (c < 2048)) {
  2735. utftext += String.fromCharCode((c >> 6) | 192);
  2736. utftext += String.fromCharCode((c & 63) | 128);
  2737. }
  2738. else {
  2739. utftext += String.fromCharCode((c >> 12) | 224);
  2740. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  2741. utftext += String.fromCharCode((c & 63) | 128);
  2742. }
  2743. }
  2744. return utftext;
  2745. }
  2746. var blockstart;
  2747. var i, j;
  2748. var W = new Array(80);
  2749. var H0 = 0x67452301;
  2750. var H1 = 0xEFCDAB89;
  2751. var H2 = 0x98BADCFE;
  2752. var H3 = 0x10325476;
  2753. var H4 = 0xC3D2E1F0;
  2754. var A, B, C, D, E;
  2755. var temp;
  2756. msg = Utf8Encode(msg);
  2757. var msg_len = msg.length;
  2758. var word_array = new Array();
  2759. for (i = 0; i < msg_len - 3; i += 4) {
  2760. j = msg.charCodeAt(i) << 24 | msg.charCodeAt(i + 1) << 16 |
  2761. msg.charCodeAt(i + 2) << 8 | msg.charCodeAt(i + 3);
  2762. word_array.push(j);
  2763. }
  2764. switch (msg_len % 4) {
  2765. case 0:
  2766. i = 0x080000000;
  2767. break;
  2768. case 1:
  2769. i = msg.charCodeAt(msg_len - 1) << 24 | 0x0800000;
  2770. break;
  2771. case 2:
  2772. i = msg.charCodeAt(msg_len - 2) << 24 | msg.charCodeAt(msg_len - 1) << 16 | 0x08000;
  2773. break;
  2774. case 3:
  2775. i = msg.charCodeAt(msg_len - 3) << 24 | msg.charCodeAt(msg_len - 2) << 16 | msg.charCodeAt(msg_len - 1) << 8 | 0x80;
  2776. break;
  2777. }
  2778. word_array.push(i);
  2779. while ((word_array.length % 16) != 14) word_array.push(0);
  2780. word_array.push(msg_len >>> 29);
  2781. word_array.push((msg_len << 3) & 0x0ffffffff);
  2782. for (blockstart = 0; blockstart < word_array.length; blockstart += 16) {
  2783. for (i = 0; i < 16; i++) W[i] = word_array[blockstart + i];
  2784. for (i = 16; i <= 79; i++) W[i] = rotate_left(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
  2785. A = H0;
  2786. B = H1;
  2787. C = H2;
  2788. D = H3;
  2789. E = H4;
  2790. for (i = 0; i <= 19; i++) {
  2791. temp = (rotate_left(A, 5) + ((B & C) | (~B & D)) + E + W[i] + 0x5A827999) & 0x0ffffffff;
  2792. E = D;
  2793. D = C;
  2794. C = rotate_left(B, 30);
  2795. B = A;
  2796. A = temp;
  2797. }
  2798. for (i = 20; i <= 39; i++) {
  2799. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0x6ED9EBA1) & 0x0ffffffff;
  2800. E = D;
  2801. D = C;
  2802. C = rotate_left(B, 30);
  2803. B = A;
  2804. A = temp;
  2805. }
  2806. for (i = 40; i <= 59; i++) {
  2807. temp = (rotate_left(A, 5) + ((B & C) | (B & D) | (C & D)) + E + W[i] + 0x8F1BBCDC) & 0x0ffffffff;
  2808. E = D;
  2809. D = C;
  2810. C = rotate_left(B, 30);
  2811. B = A;
  2812. A = temp;
  2813. }
  2814. for (i = 60; i <= 79; i++) {
  2815. temp = (rotate_left(A, 5) + (B ^ C ^ D) + E + W[i] + 0xCA62C1D6) & 0x0ffffffff;
  2816. E = D;
  2817. D = C;
  2818. C = rotate_left(B, 30);
  2819. B = A;
  2820. A = temp;
  2821. }
  2822. H0 = (H0 + A) & 0x0ffffffff;
  2823. H1 = (H1 + B) & 0x0ffffffff;
  2824. H2 = (H2 + C) & 0x0ffffffff;
  2825. H3 = (H3 + D) & 0x0ffffffff;
  2826. H4 = (H4 + E) & 0x0ffffffff;
  2827. }
  2828. var temp = cvt_hex(H0) + cvt_hex(H1) + cvt_hex(H2) + cvt_hex(H3) + cvt_hex(H4);
  2829. return temp.toLowerCase();
  2830. };
  2831. (function (jQuery, window, undefined) {
  2832. "use strict";
  2833. var matched, browser;
  2834. jQuery.uaMatch = function (ua) {
  2835. ua = ua.toLowerCase();
  2836. var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
  2837. /(webkit)[ \/]([\w.]+)/.exec(ua) ||
  2838. /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
  2839. /(msie) ([\w.]+)/.exec(ua) ||
  2840. ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
  2841. [];
  2842. var platform_match = /(ipad)/.exec(ua) ||
  2843. /(iphone)/.exec(ua) ||
  2844. /(android)/.exec(ua) ||
  2845. [];
  2846. var browser = match[1] || "";
  2847. if (ua.indexOf("like gecko") != -1 && ua.indexOf('webkit') == -1 && ua.indexOf('opera') == -1) {
  2848. browser = "msie";
  2849. }
  2850. return {
  2851. browser: browser,
  2852. version: match[2] || "0",
  2853. platform: platform_match[0] || ""
  2854. };
  2855. };
  2856. matched = jQuery.uaMatch(window.navigator.userAgent);
  2857. browser = {};
  2858. if (matched.browser) {
  2859. browser[matched.browser] = true;
  2860. browser.version = matched.version;
  2861. }
  2862. if (matched.platform) {
  2863. browser[matched.platform] = true
  2864. }
  2865. // Chrome is Webkit, but Webkit is also Safari.
  2866. if (browser.chrome) {
  2867. browser.webkit = true;
  2868. } else if (browser.webkit) {
  2869. browser.safari = true;
  2870. }
  2871. jQuery.browser = browser;
  2872. })(jQuery, window);