DashboardPage.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. var DashboardPage = {
  2. onPageShow: function () {
  3. Dashboard.showLoadingMsg();
  4. DashboardPage.pollForInfo();
  5. DashboardPage.startInterval();
  6. $(document).on("websocketmessage", DashboardPage.onWebSocketMessage).on("websocketopen", DashboardPage.onWebSocketConnectionChange).on("websocketerror", DashboardPage.onWebSocketConnectionChange).on("websocketclose", DashboardPage.onWebSocketConnectionChange);
  7. DashboardPage.lastAppUpdateCheck = null;
  8. DashboardPage.lastPluginUpdateCheck = null;
  9. },
  10. onPageHide: function () {
  11. $(document).off("websocketmessage", DashboardPage.onWebSocketMessage).off("websocketopen", DashboardPage.onWebSocketConnectionChange).off("websocketerror", DashboardPage.onWebSocketConnectionChange).off("websocketclose", DashboardPage.onWebSocketConnectionChange);
  12. DashboardPage.stopInterval();
  13. },
  14. startInterval: function () {
  15. if (Dashboard.isWebSocketOpen()) {
  16. Dashboard.sendWebSocketMessage("DashboardInfoStart", "0,1500");
  17. }
  18. },
  19. stopInterval: function () {
  20. if (Dashboard.isWebSocketOpen()) {
  21. Dashboard.sendWebSocketMessage("DashboardInfoStop");
  22. }
  23. },
  24. onWebSocketMessage: function (e, msg) {
  25. if (msg.MessageType == "DashboardInfo") {
  26. DashboardPage.renderInfo(msg.Data);
  27. }
  28. },
  29. onWebSocketConnectionChange: function () {
  30. DashboardPage.stopInterval();
  31. DashboardPage.startInterval();
  32. },
  33. pollForInfo: function () {
  34. $.getJSON("dashboardInfo").done(DashboardPage.renderInfo);
  35. },
  36. renderInfo: function (dashboardInfo) {
  37. DashboardPage.lastDashboardInfo = dashboardInfo;
  38. DashboardPage.renderRunningTasks(dashboardInfo);
  39. DashboardPage.renderSystemInfo(dashboardInfo);
  40. DashboardPage.renderActiveConnections(dashboardInfo);
  41. Dashboard.hideLoadingMsg();
  42. },
  43. renderActiveConnections: function (dashboardInfo) {
  44. var page = $.mobile.activePage;
  45. var html = '';
  46. if (!dashboardInfo.ActiveConnections.length) {
  47. html += '<p>There are no users currently connected.</p>';
  48. $('#divConnections', page).html(html).trigger('create');
  49. return;
  50. }
  51. html += '<table class="tblConnections" style="border-collapse:collapse;">';
  52. for (var i = 0, length = dashboardInfo.ActiveConnections.length; i < length; i++) {
  53. var connection = dashboardInfo.ActiveConnections[i];
  54. var user = dashboardInfo.Users.filter(function (u) {
  55. return u.Id == connection.UserId;
  56. })[0];
  57. html += '<tr>';
  58. html += '<td style="text-align:center;">';
  59. html += DashboardPage.getClientType(connection);
  60. html += '</td>';
  61. html += '<td>';
  62. html += user.Name;
  63. html += '</td>';
  64. html += '<td>';
  65. html += connection.DeviceName;
  66. html += '</td>';
  67. html += '<td>';
  68. html += DashboardPage.getNowPlayingImage(connection.NowPlayingItem);
  69. html += '</td>';
  70. html += '<td>';
  71. html += DashboardPage.getNowPlayingText(connection, connection.NowPlayingItem);
  72. html += '</td>';
  73. html += '</tr>';
  74. }
  75. html += '</table>';
  76. $('#divConnections', page).html(html);
  77. },
  78. getClientType: function (connection) {
  79. var clientLowered = connection.Client.toLowerCase();
  80. if (clientLowered == "dashboard") {
  81. return "<img src='css/images/clients/html5.png' alt='Dashboard' title='Dashboard' />";
  82. }
  83. if (clientLowered == "media browser classic") {
  84. return "<img src='css/images/clients/mb.png' alt='Media Browser Classic' title='Media Browser Classic' />";
  85. }
  86. if (clientLowered == "media browser theater") {
  87. return "<img src='css/images/clients/mb.png' alt='Media Browser Theater' title='Media Browser Theater' />";
  88. }
  89. if (clientLowered == "android") {
  90. return "<img src='css/images/clients/android.png' alt='Android' title='Android' />";
  91. }
  92. if (clientLowered == "ios") {
  93. return "<img src='css/images/clients/ios.png' alt='iOS' title='iOS' />";
  94. }
  95. if (clientLowered == "windows rt") {
  96. return "<img src='css/images/clients/windowsrt.png' alt='Windows RT' title='Windows RT' />";
  97. }
  98. if (clientLowered == "windows phone") {
  99. return "<img src='css/images/clients/windowsphone.png' alt='Windows Phone' title='Windows Phone' />";
  100. }
  101. if (clientLowered == "dlna") {
  102. return "<img src='css/images/clients/dlna.png' alt='Dlna' title='Dlna' />";
  103. }
  104. return connection.Client;
  105. },
  106. getNowPlayingImage: function (item) {
  107. if (item) {
  108. if (item.BackdropImageTag) {
  109. var url = ApiClient.getImageUrl(item.Id, {
  110. type: "Backdrop",
  111. height: 100,
  112. tag: item.BackdropImageTag
  113. });
  114. return "<img class='clientNowPlayingImage' src='" + url + "' alt='" + item.Name + "' title='" + item.Name + "' />";
  115. }
  116. else if (item.PrimaryImageTag) {
  117. var url = ApiClient.getImageUrl(item.Id, {
  118. type: "Primary",
  119. height: 100,
  120. tag: item.PrimaryImageTag
  121. });
  122. return "<img class='clientNowPlayingImage' src='" + url + "' alt='" + item.Name + "' title='" + item.Name + "' />";
  123. }
  124. }
  125. return "";
  126. },
  127. getNowPlayingText: function (connection, item) {
  128. var html = "";
  129. if (item) {
  130. html += "<div>" + item.Name + "</div>";
  131. html += "<div>";
  132. if (item.RunTimeTicks) {
  133. html += DashboardPage.getDisplayText(connection.NowPlayingPositionTicks || 0) + " / ";
  134. html += DashboardPage.getDisplayText(item.RunTimeTicks);
  135. }
  136. html += "</div>";
  137. }
  138. return html;
  139. },
  140. getDisplayText: function (ticks) {
  141. var ticksPerHour = 36000000000;
  142. var parts = [];
  143. var hours = ticks / ticksPerHour;
  144. hours = parseInt(hours);
  145. if (hours) {
  146. parts.push(hours);
  147. }
  148. ticks -= (hours * ticksPerHour);
  149. var ticksPerMinute = 600000000;
  150. var minutes = ticks / ticksPerMinute;
  151. minutes = parseInt(minutes);
  152. ticks -= (minutes * ticksPerMinute);
  153. if (minutes < 10) {
  154. minutes = '0' + minutes;
  155. }
  156. parts.push(minutes);
  157. var ticksPerSecond = 10000000;
  158. var seconds = ticks / ticksPerSecond;
  159. seconds = parseInt(seconds);
  160. if (seconds < 10) {
  161. seconds = '0' + seconds;
  162. }
  163. parts.push(seconds);
  164. return parts.join(':');
  165. },
  166. renderRunningTasks: function (dashboardInfo) {
  167. var page = $.mobile.activePage;
  168. var html = '';
  169. if (!dashboardInfo.RunningTasks.length) {
  170. html += '<p>No tasks are currently running.</p>';
  171. }
  172. for (var i = 0, length = dashboardInfo.RunningTasks.length; i < length; i++) {
  173. var task = dashboardInfo.RunningTasks[i];
  174. html += '<p>';
  175. html += task.Name;
  176. if (task.State == "Running") {
  177. var progress = Math.round(task.CurrentProgressPercentage || 0);
  178. html += '<span style="color:#267F00;margin-right:5px;font-weight:bold;"> - ' + progress + '%</span>';
  179. html += '<button type="button" data-icon="stop" data-iconpos="notext" data-inline="true" data-theme="b" data-mini="true" onclick="DashboardPage.stopTask(\'' + task.Id + '\');">Stop</button>';
  180. }
  181. else if (task.State == "Cancelling") {
  182. html += '<span style="color:#cc0000;"> - Stopping</span>';
  183. }
  184. html += '</p>';
  185. }
  186. $('#divRunningTasks', page).html(html).trigger('create');
  187. },
  188. renderSystemInfo: function (dashboardInfo) {
  189. Dashboard.updateSystemInfo(dashboardInfo.SystemInfo);
  190. var page = $.mobile.activePage;
  191. $('#appVersionNumber', page).html(dashboardInfo.SystemInfo.Version);
  192. if (dashboardInfo.RunningTasks.filter(function (task) {
  193. return task.Id == dashboardInfo.ApplicationUpdateTaskId;
  194. }).length) {
  195. $('#btnUpdateApplication', page).button('disable');
  196. } else {
  197. $('#btnUpdateApplication', page).button('enable');
  198. }
  199. DashboardPage.renderApplicationUpdateInfo(dashboardInfo);
  200. DashboardPage.renderPluginUpdateInfo(dashboardInfo);
  201. DashboardPage.renderPendingInstallations(dashboardInfo.SystemInfo);
  202. },
  203. renderApplicationUpdateInfo: function (dashboardInfo) {
  204. var page = $.mobile.activePage;
  205. if (dashboardInfo.SystemInfo.IsNetworkDeployed && !dashboardInfo.SystemInfo.HasPendingRestart) {
  206. // Only check once every 10 mins
  207. if (DashboardPage.lastAppUpdateCheck && (new Date().getTime() - DashboardPage.lastAppUpdateCheck) < 600000) {
  208. return;
  209. }
  210. DashboardPage.lastAppUpdateCheck = new Date().getTime();
  211. ApiClient.getAvailableApplicationUpdate().done(function (packageInfo) {
  212. var version = packageInfo[0];
  213. if (!version) {
  214. $('#pUpToDate', page).show();
  215. $('#pUpdateNow', page).hide();
  216. } else {
  217. $('#pUpToDate', page).hide();
  218. $('#pUpdateNow', page).show();
  219. $('#newVersionNumber', page).html("Version " + version.versionStr + " is now available for download.");
  220. }
  221. }).fail(function () {
  222. Dashboard.showFooterNotification({ html: '<img src="css/images/notifications/error.png" class="notificationIcon" />There was an error connecting to the remote Media Browser repository.', id: "MB3ConnectionError" });
  223. });
  224. } else {
  225. if (dashboardInfo.SystemInfo.HasPendingRestart) {
  226. $('#pUpToDate', page).hide();
  227. } else {
  228. $('#pUpToDate', page).show();
  229. }
  230. $('#pUpdateNow', page).hide();
  231. }
  232. },
  233. renderPendingInstallations: function (systemInfo) {
  234. var page = $.mobile.activePage;
  235. if (systemInfo.CompletedInstallations.length) {
  236. $('#collapsiblePendingInstallations', page).show();
  237. } else {
  238. $('#collapsiblePendingInstallations', page).hide();
  239. return;
  240. }
  241. var html = '';
  242. for (var i = 0, length = systemInfo.CompletedInstallations.length; i < length; i++) {
  243. var update = systemInfo.CompletedInstallations[i];
  244. html += '<div><strong>' + update.Name + '</strong> (' + update.Version + ')</div>';
  245. }
  246. $('#pendingInstallations', page).html(html);
  247. },
  248. renderPluginUpdateInfo: function (dashboardInfo) {
  249. // Only check once every 10 mins
  250. if (DashboardPage.lastPluginUpdateCheck && (new Date().getTime() - DashboardPage.lastPluginUpdateCheck) < 600000) {
  251. return;
  252. }
  253. DashboardPage.lastPluginUpdateCheck = new Date().getTime();
  254. var page = $.mobile.activePage;
  255. ApiClient.getAvailablePluginUpdates().done(function (updates) {
  256. var elem = $('#pPluginUpdates', page);
  257. if (updates.length) {
  258. elem.show();
  259. } else {
  260. elem.hide();
  261. return;
  262. }
  263. var html = '';
  264. for (var i = 0, length = updates.length; i < length; i++) {
  265. var update = updates[i];
  266. html += '<p><strong>A new version of ' + update.name + ' is available!</strong></p>';
  267. html += '<button type="button" data-icon="download" data-theme="b" onclick="DashboardPage.installPluginUpdate(this);" data-name="' + update.name + '" data-version="' + update.versionStr + '" data-classification="' + update.classification + '">Update Now</button>';
  268. }
  269. elem.html(html).trigger('create');
  270. }).fail(function () {
  271. Dashboard.showFooterNotification({ html: '<img src="css/images/notifications/error.png" class="notificationIcon" />There was an error connecting to the remote Media Browser repository.', id: "MB3ConnectionError" });
  272. });
  273. },
  274. installPluginUpdate: function (button) {
  275. $(button).button('disable');
  276. var name = button.getAttribute('data-name');
  277. var version = button.getAttribute('data-version');
  278. var classification = button.getAttribute('data-classification');
  279. Dashboard.showLoadingMsg();
  280. ApiClient.installPlugin(name, classification, version).done(function () {
  281. Dashboard.hideLoadingMsg();
  282. });
  283. },
  284. updateApplication: function () {
  285. var page = $.mobile.activePage;
  286. $('#btnUpdateApplication', page).button('disable');
  287. Dashboard.showLoadingMsg();
  288. ApiClient.startScheduledTask(DashboardPage.lastDashboardInfo.ApplicationUpdateTaskId).done(function () {
  289. DashboardPage.pollForInfo();
  290. Dashboard.hideLoadingMsg();
  291. });
  292. },
  293. stopTask: function (id) {
  294. ApiClient.stopScheduledTask(id).done(function () {
  295. DashboardPage.pollForInfo();
  296. });
  297. }
  298. };
  299. $(document).on('pageshow', "#dashboardPage", DashboardPage.onPageShow).on('pagehide', "#dashboardPage", DashboardPage.onPageHide);