Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.77 KB | None | 0 0
  1. /// <reference path="./../../typings/bootstrap.d.ts"/>
  2.  
  3. import { IView } from "../menu/interfaces";
  4. import { ContextView, ICachedContext } from "../menu/contextView";
  5. import { Url } from "./../../url";
  6. import { Messages, GroupMessages } from "../messages/messages";
  7. import { MessageToast } from "../messages/messageToast";
  8. import { Modal } from "./Modal"
  9. import { SchedulerView } from "./schedulerView"
  10. import { AddGroupModal } from "./group/addGroupModal";
  11. import { AssignTamplateModal } from "./group/assignTemplateModal";
  12. import { EditGroupModal } from "./group/editGroupModal";
  13. import { Group } from "./group/group";
  14.  
  15. export class GroupView implements IView {
  16. $viewElement: JQuery;
  17. name: string;
  18. icon: string;
  19. contextView: ContextView<ICachedContext>;
  20. callbacksSet: boolean = false;
  21.  
  22. rootGroups: { [id: string]: Group } = {};
  23.  
  24. createGroupModal: AddGroupModal;
  25. assignTamplateModal: AssignTamplateModal;
  26. renameGroupModal: EditGroupModal;
  27.  
  28. static messageToast: MessageToast;
  29. static companyId: string;
  30. static currentGroupId: string;
  31.  
  32. static addPrefix: string = "add-";
  33. static assignedPrefix: string = "assigned-";
  34. static assignedListPrefix: string = "assignedlist-";
  35.  
  36. static Singleton: GroupView;
  37.  
  38. constructor(companyId: string, element: JQuery) {
  39. GroupView.companyId = companyId;
  40. this.$viewElement = element;
  41. this.name = "Grupy";
  42. this.icon = "fa-object-group";
  43. }
  44.  
  45. initialize(): boolean {
  46. if (this.$viewElement.length === 0) {
  47. return false;
  48. }
  49.  
  50. this.contextView = new ContextView<ICachedContext>(this.$viewElement.find("#device-section"));
  51.  
  52. this.createGroupModal = new AddGroupModal($("#addModal"), GroupView.companyId, this);
  53. this.assignTamplateModal = new AssignTamplateModal($("#groupAssignModal"), GroupView.companyId, this);
  54. this.renameGroupModal = new EditGroupModal($("#editModal"), GroupView.companyId, this);
  55.  
  56. GroupView.Singleton = this;
  57.  
  58. return true;
  59. }
  60.  
  61. onShow(): void {
  62. var t = this;
  63.  
  64. var rootJElements = $(".group-list >.group-info");
  65. rootJElements.each(function (index: number, element: Element) {
  66. var rootGroup = new Group($(element));
  67. t.rootGroups[rootGroup.Id] = rootGroup;
  68. });
  69. GroupView.messageToast = new MessageToast(2000, "#info-toast-area");
  70.  
  71. if (!this.callbacksSet) {
  72. this.callbacksSet = true;
  73. t.setCallbacks();
  74. t.createGroupModal.modalInit();
  75. t.assignTamplateModal.modalInit();
  76. t.renameGroupModal.modalInit();
  77. }
  78. }
  79.  
  80. public setCallbacks(): void {
  81. var jElement = jQuery("#group-section");
  82.  
  83. var t = this;
  84. jElement.off("click.show-devices").on("click.show-devices", ".show-devices", function () {
  85. var row = $(this).closest(".group-info");
  86. $(".active-row").removeClass("active-row");
  87. row.find(">.data").addClass("active-row");
  88. var groupId = row.data("id");
  89.  
  90. t.diplayGroupDevices(groupId, false);
  91. });
  92. jElement.off("click.assign-devices").on("click.assign-devices", ".assign-devices", function () {
  93. var row = $(this).closest(".group-info");
  94. $(".active-row").removeClass("active-row");
  95. row.find(">.data").addClass("active-row");
  96. var groupId = row.data("id");
  97.  
  98. t.diplayGroupDevices(groupId, true);
  99. });
  100. jElement.off("click.add-subgroup").on("click.add-subgroup", ".add-subgroup", function () {
  101. var groupId = $(this).closest(".group-info").data("id");
  102. t.createGroupModal.displayModal(groupId);
  103. });
  104. jElement.off("click.assign-template").on("click.assign-template", ".assign-template", function () {
  105. var groupId = $(this).closest(".group-info").data("id");
  106. t.assignTamplateModal.displayModal(groupId);
  107. });
  108. jElement.off("click.assigned-template-list-btn").on("click.assigned-template-list-btn", ".assigned-template-list-btn", function () {
  109. var row = $(this).closest(".group-info");
  110. $(".active-row").removeClass("active-row");
  111. row.find(">.data").addClass("active-row");
  112. var groupId = row.data("id");
  113.  
  114. t.displayGroupTemplates(groupId);
  115. });
  116.  
  117. jElement.off("click.rename-group").on("click.rename-group", ".rename-group", function () {
  118. var groupId = $(this).closest(".group-info").data("id");
  119. t.renameGroupModal.displayModal(groupId);
  120. });
  121. jElement.off("click.add-root-group").on("click.add-root-group", ".add-root-group", function () {
  122. t.createGroupModal.displayModal();
  123. });
  124. jElement.off("click.remove-group").on("click.remove-group", ".remove-group", function () {
  125. var groupId = $(this).closest(".group-info").data("id");
  126. Group.getGroup(groupId).delete();
  127. });
  128. jElement.off("click.fold-all").on("click.fold-all", ".fold-all", function () {
  129. Group.foldAll();
  130. })
  131. jElement.off("click.unfold-all").on("click.unfold-all", ".unfold-all", function () {
  132. Group.unfoldAll();
  133. })
  134. }
  135.  
  136. private setDeviceCallbacks(): void {
  137. var t = this;
  138. var getId = function (el: HTMLElement): string {
  139. return $(el).closest(".device-info").data("id");
  140. }
  141.  
  142. $(".action-button-container").off("click.remove-from-group").on("click.remove-from-group", ".remove-from-group", function () {
  143. var deviceId = getId(this);
  144. Group.getGroup(GroupView.currentGroupId).removeDevice(deviceId);
  145. });
  146. $(".action-button-container").off("click.add-to-group").on("click.add-to-group", ".add-to-group", function () {
  147. var deviceId = getId(this);
  148. Group.getGroup(GroupView.currentGroupId).addDevice(deviceId);
  149. });
  150. }
  151.  
  152. public displayGroupTemplates(id: string): void {
  153. var scope = this;
  154. var key = GroupView.assignedListPrefix + id;
  155. GroupView.currentGroupId = id;
  156. var group = Group.getGroup(id);
  157. $.ajax({
  158. type: "GET",
  159. url: Url.action("Template", "GetGroupTemplates", { groupId: id, companyId: GroupView.companyId }),
  160. success: function (data) {
  161. $("#device-section .context-view-body").html(data);
  162. $("#device-section").removeClass("hidden");
  163. scope.loadActionButtons(id);
  164. scope.setTemplateAssignmentCallbacks();
  165. }
  166. });
  167. }
  168.  
  169. public loadActionButtons(groupId: string) {
  170. $("#device-section .top-panel .btn-group").html(`
  171. <div class='action-btn-container'>
  172. <a class="btn btn-link go-to-scheduler" data-groupid='${groupId}' href="#">
  173. <i class="fa fa-calendar" aria-hidden="true"></i>
  174. Harmonogram
  175. </a>
  176. <a class="btn btn-link refresh-device-content" data-groupid='${groupId}' data-companyId='${GroupView.companyId}' href="#">
  177. <i class="fa fa-refresh" aria-hidden="true"></i>
  178. Odśwież urządzenia
  179. </a>`);
  180. //$("#device-section .top-panel").html(`<div class='action-btn-container'><div class='go-to-scheduler' data-groupid='${groupId}'><i class='fa fa- calendar dissociate-group-assignment' aria-hidden='true'></i><p>Harmonogram</p></div></div>`);
  181. }
  182.  
  183. public eraseDeviceSectionTopPanel() {
  184. $("#device-section .top-panel .btn-group").empty();
  185. }
  186.  
  187. public diplayGroupDevices(id: string, addView: boolean): void {
  188. var t = this;
  189. var key = (addView ? GroupView.addPrefix : GroupView.assignedPrefix) + id;
  190.  
  191. GroupView.currentGroupId = id;
  192. var group = Group.getGroup(id);
  193.  
  194. var reload = false;
  195. if (addView && group.AddDirty) {
  196. group.AddDirty = false
  197. reload = true;
  198. }
  199. else if (!addView && group.ListDirty) {
  200. group.ListDirty = false
  201. reload = true;
  202. }
  203.  
  204. if (reload) {
  205. try {
  206. t.contextView.reload(key);
  207. } catch (e) {
  208. t.contextView.load({
  209. url: addView
  210. ? Url.action("Dashboard", "AvaiableDevices", { groupId: id })
  211. : Url.action("Dashboard", "GroupDevices", { groupId: id }),
  212. success: function () {
  213. t.eraseDeviceSectionTopPanel();
  214. t.setDeviceCallbacks();
  215. }
  216. }, key);
  217. }
  218.  
  219.  
  220. } else {
  221. t.contextView.load({
  222. url: addView
  223. ? Url.action("Dashboard", "AvaiableDevices", { groupId: id })
  224. : Url.action("Dashboard", "GroupDevices", { groupId: id }),
  225. success: function () {
  226. t.eraseDeviceSectionTopPanel();
  227. t.setDeviceCallbacks();
  228. }
  229. }, key);
  230. }
  231. }
  232.  
  233. public getGroupParcial(groupId: string, callback: (data: any) => void) {
  234. $.get(Url.action("Dashboard", "GroupParcial", { groupId: groupId }))
  235. .done(function (data) {
  236. callback(data);
  237. GroupView.messageToast.displayToast("success", GroupMessages.parcialDownloaded);
  238. }).fail(function () {
  239. GroupView.messageToast.displayToast("error", Messages.operationFailed);
  240. });
  241. return
  242. }
  243.  
  244. public refreshRoot(id: string) {
  245. var jElement = $(".group-list >.group-info[data-id='" + id + "']");
  246. var root = new Group(jElement);
  247. this.rootGroups[id] = root;
  248. }
  249.  
  250. public removeRoot(id: string) {
  251. var jElement = $(".group-list >.group-info[data-id='" + id + "']");
  252. jElement.remove();
  253. }
  254.  
  255. public setTemplateAssignmentCallbacks(): void {
  256. $(".action-btn-container").off("click.go-to-scheduler").on("click.go-to-scheduler", ".go-to-scheduler", function () {
  257. Group.getGroup(GroupView.currentGroupId).goToScheduler($(".action-btn-container .go-to-scheduler").data("groupid"));
  258. });
  259. $(".action-btn-container").off("click.delete-assignment").on("click.delete-assignment", ".delete-assignment", function () {
  260. Group.getGroup(GroupView.currentGroupId).removeTemplateGroupAssignment($(this).data("assignmentid"));
  261. });
  262. $(".action-btn-container").off("click.refresh-device-content").on("click.refresh-device-content", ".refresh-device-content", function () {
  263. var source = $(".action-btn-container .refresh-device-content"),
  264. groupId: string = source.data("groupid"),
  265. companyId: string = source.data("companyid"),
  266. refreshUrl: string = `/Template/Refresh/${companyId}/${groupId}`;
  267.  
  268. $.ajax({
  269. type: "PUT",
  270. url: refreshUrl,
  271. success: function (data) {
  272. var messageToast = new MessageToast(30000, "#info-toast-area");
  273. messageToast.displayToast("success", "Zaktualizowano treść szablonów na urządzeniach.");
  274. },
  275. error: function (data) {
  276. var message = "Napotkano błędy podczas wysyłania danych";
  277. console.log(data);
  278. var messageToast = new MessageToast(30000, "#info-toast-area");
  279. messageToast.displayToast("error", message);
  280. }
  281.  
  282. })
  283. });
  284.  
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement