DashboardPage.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. if (connection.Client.toLowerCase() == "dashboard") {
  80. return "<img src='css/images/clients/html5.png' alt='Dashboard' title='Dashboard' />";
  81. }
  82. if (connection.Client.toLowerCase() == "mediabrowsertheater") {
  83. return "<img src='css/images/clients/mb.png' alt='Media Browser Theater' title='Media Browser Theater' />";
  84. }
  85. if (connection.Client.toLowerCase() == "android") {
  86. return "<img src='css/images/clients/android.png' alt='Android' title='Android' />";
  87. }
  88. if (connection.Client.toLowerCase() == "ios") {
  89. return "<img src='css/images/clients/ios.png' alt='iOS' title='iOS' />";
  90. }
  91. if (connection.Client.toLowerCase() == "windowsrt") {
  92. return "<img src='css/images/clients/windowsrt.png' alt='Windows RT' title='Windows RT' />";
  93. }
  94. if (connection.Client.toLowerCase() == "windowsphone") {
  95. return "<img src='css/images/clients/windowsphone.png' alt='Windows Phone' title='Windows Phone' />";
  96. }
  97. if (connection.Client.toLowerCase() == "dlna") {
  98. return "<img src='css/images/clients/dlna.png' alt='Dlna' title='Dlna' />";
  99. }
  100. return connection.Client;
  101. },
  102. getNowPlayingImage: function (item) {
  103. if (item) {
  104. if (item.BackdropImageTag) {
  105. var url = ApiClient.getImageUrl(item.Id, {
  106. type: "Backdrop",
  107. height: 100,
  108. tag: item.BackdropImageTag
  109. });
  110. return "<img class='clientNowPlayingImage' src='" + url + "' alt='" + item.Name + "' title='" + item.Name + "' />";
  111. }
  112. else if (item.PrimaryImageTag) {
  113. var url = ApiClient.getImageUrl(item.Id, {
  114. type: "Primary",
  115. height: 100,
  116. tag: item.PrimaryImageTag
  117. });
  118. return "<img class='clientNowPlayingImage' src='" + url + "' alt='" + item.Name + "' title='" + item.Name + "' />";
  119. }
  120. }
  121. return "";
  122. },
  123. getNowPlayingText: function (connection, item) {
  124. var html = "";
  125. if (item) {
  126. html += "<div>" + item.Name + "</div>";
  127. html += "<div>";
  128. if (item.RunTimeTicks) {
  129. html += DashboardPage.getDisplayText(connection.NowPlayingPositionTicks || 0) + " / ";
  130. html += DashboardPage.getDisplayText(item.RunTimeTicks);
  131. }
  132. html += "</div>";
  133. }
  134. return html;
  135. },
  136. getDisplayText: function (ticks) {
  137. var ticksPerHour = 36000000000;
  138. var parts = [];
  139. var hours = ticks / ticksPerHour;
  140. hours = parseInt(hours);
  141. if (hours) {
  142. parts.push(hours);
  143. }
  144. ticks -= (hours * ticksPerHour);
  145. var ticksPerMinute = 600000000;
  146. var minutes = ticks / ticksPerMinute;
  147. minutes = parseInt(minutes);
  148. ticks -= (minutes * ticksPerMinute);
  149. if (minutes < 10) {
  150. minutes = '0' + minutes;
  151. }
  152. parts.push(minutes);
  153. var ticksPerSecond = 10000000;
  154. var seconds = ticks / ticksPerSecond;
  155. seconds = parseInt(seconds);
  156. if (seconds < 10) {
  157. seconds = '0' + seconds;
  158. }
  159. parts.push(seconds);
  160. return parts.join(':');
  161. },
  162. renderRunningTasks: function (dashboardInfo) {
  163. var page = $.mobile.activePage;
  164. var html = '';
  165. if (!dashboardInfo.RunningTasks.length) {
  166. html += '<p>No tasks are currently running.</p>';
  167. }
  168. for (var i = 0, length = dashboardInfo.RunningTasks.length; i < length; i++) {
  169. var task = dashboardInfo.RunningTasks[i];
  170. html += '<p>';
  171. html += task.Name;
  172. if (task.State == "Running") {
  173. var progress = Math.round(task.CurrentProgressPercentage || 0);
  174. html += '<span style="color:#267F00;margin-right:5px;font-weight:bold;"> - ' + progress + '%</span>';
  175. 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>';
  176. }
  177. else if (task.State == "Cancelling") {
  178. html += '<span style="color:#cc0000;"> - Stopping</span>';
  179. }
  180. html += '</p>';
  181. }
  182. $('#divRunningTasks', page).html(html).trigger('create');
  183. },
  184. renderSystemInfo: function (dashboardInfo) {
  185. Dashboard.updateSystemInfo(dashboardInfo.SystemInfo);
  186. var page = $.mobile.activePage;
  187. $('#appVersionNumber', page).html(dashboardInfo.SystemInfo.Version);
  188. if (dashboardInfo.RunningTasks.filter(function (task) {
  189. return task.Id == dashboardInfo.ApplicationUpdateTaskId;
  190. }).length) {
  191. $('#btnUpdateApplication', page).button('disable');
  192. } else {
  193. $('#btnUpdateApplication', page).button('enable');
  194. }
  195. DashboardPage.renderApplicationUpdateInfo(dashboardInfo);
  196. DashboardPage.renderPluginUpdateInfo(dashboardInfo);
  197. },
  198. renderApplicationUpdateInfo: function (dashboardInfo) {
  199. var page = $.mobile.activePage;
  200. if (dashboardInfo.SystemInfo.IsNetworkDeployed && !dashboardInfo.SystemInfo.HasPendingRestart) {
  201. // Only check once every 10 mins
  202. if (DashboardPage.lastAppUpdateCheck && (new Date().getTime() - DashboardPage.lastAppUpdateCheck) < 600000) {
  203. return;
  204. }
  205. DashboardPage.lastAppUpdateCheck = new Date().getTime();
  206. ApiClient.getAvailableApplicationUpdate().done(function (packageInfo) {
  207. var version = packageInfo[0];
  208. if (!version) {
  209. $('#pUpToDate', page).show();
  210. $('#pUpdateNow', page).hide();
  211. } else {
  212. $('#pUpToDate', page).hide();
  213. $('#pUpdateNow', page).show();
  214. $('#newVersionNumber', page).html("Version " + version.versionStr + " is now available for download.");
  215. }
  216. }).fail(function () {
  217. 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" });
  218. });
  219. } else {
  220. if (dashboardInfo.SystemInfo.HasPendingRestart) {
  221. $('#pUpToDate', page).hide();
  222. } else {
  223. $('#pUpToDate', page).show();
  224. }
  225. $('#pUpdateNow', page).hide();
  226. }
  227. },
  228. renderPluginUpdateInfo: function (dashboardInfo) {
  229. // Only check once every 10 mins
  230. if (DashboardPage.lastPluginUpdateCheck && (new Date().getTime() - DashboardPage.lastPluginUpdateCheck) < 600000) {
  231. return;
  232. }
  233. DashboardPage.lastPluginUpdateCheck = new Date().getTime();
  234. var page = $.mobile.activePage;
  235. ApiClient.getAvailablePluginUpdates().done(function (updates) {
  236. if (updates.length) {
  237. $('#collapsiblePluginUpdates', page).show();
  238. } else {
  239. $('#collapsiblePluginUpdates', page).hide();
  240. return;
  241. }
  242. var html = '';
  243. for (var i = 0, length = updates.length; i < length; i++) {
  244. var update = updates[i];
  245. html += '<p><strong>A new version of ' + update.name + ' is available!</strong></p>';
  246. 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>';
  247. }
  248. $('#pPluginUpdates', page).html(html).trigger('create');
  249. }).fail(function () {
  250. 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" });
  251. });
  252. },
  253. installPluginUpdate: function (button) {
  254. $(button).button('disable');
  255. var name = button.getAttribute('data-name');
  256. var version = button.getAttribute('data-version');
  257. var classification = button.getAttribute('data-classification');
  258. Dashboard.showLoadingMsg();
  259. ApiClient.installPlugin(name, classification, version).done(function () {
  260. Dashboard.hideLoadingMsg();
  261. });
  262. },
  263. updateApplication: function () {
  264. var page = $.mobile.activePage;
  265. $('#btnUpdateApplication', page).button('disable');
  266. Dashboard.showLoadingMsg();
  267. ApiClient.startScheduledTask(DashboardPage.lastDashboardInfo.ApplicationUpdateTaskId).done(function () {
  268. DashboardPage.pollForInfo();
  269. Dashboard.hideLoadingMsg();
  270. });
  271. },
  272. stopTask: function (id) {
  273. ApiClient.stopScheduledTask(id).done(function () {
  274. DashboardPage.pollForInfo();
  275. });
  276. }
  277. };
  278. $(document).on('pageshow', "#dashboardPage", DashboardPage.onPageShow).on('pagehide', "#dashboardPage", DashboardPage.onPageHide);