ApiClient.js 76 KB

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