Advertisement
maramizo

Untitled

Jan 15th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.61 KB | None | 0 0
  1. import { Component, ViewChild } from '@angular/core';
  2. import { ModalModule, ModalDirective } from "ngx-bootstrap/modal";
  3. import { GridOptions } from "ag-grid";
  4. import { StatusEditorComponent } from "./status-editor.component";
  5. import { StatusRendererComponent } from "./status-renderer.component";
  6. import { GridHTTP } from "./gridFunctions/grid-http";
  7. import { NotificationHTTP } from '../../components/globalAppClasses/app.notifications';
  8.  
  9. @Component({
  10. selector: 'app-lead-entry-application',
  11. templateUrl: 'lead-entry.component.html',
  12. providers: [GridHTTP, NotificationHTTP]
  13. })
  14.  
  15. export class LeadEntryComponent {
  16. public myModal;
  17. public largeModal;
  18. public smallModal;
  19. public primaryModal;
  20. public successModal;
  21. public warningModal;
  22. public dangerModal;
  23. public infoModal;
  24.  
  25. public active = new Array();
  26. public editingMode = false;
  27.  
  28. public refresh;
  29.  
  30. private username = JSON.parse(sessionStorage.getItem('username'));
  31. private data = new Array();
  32.  
  33. public gridOptions: GridOptions;
  34. private gridApi;
  35. private gridColumnApi;
  36. public openerStatusDefinitions = [
  37. "Junk Lead",
  38. "Callback",
  39. "Prospect",
  40. "Existing",
  41. "Do Not Call",
  42. "Sold",
  43. "Open Lead"
  44. ];
  45. private colDefinitions = [
  46. {
  47. headerName: "bid",
  48. field: "bid",
  49. hide: true
  50. },
  51. {
  52. headerName: "Date",
  53. field: "dateE"
  54. },
  55. {
  56. headerName: "Business Name",
  57. filter:'text',
  58. field: "bname"
  59. },
  60. {
  61. headerName: "Website",
  62. filter:'text',
  63. field: "website"
  64. },
  65. {
  66. headerName: "Phone #",
  67. filter:'text',
  68. field: "phone"
  69. },
  70. {
  71. headerName: "First Name",
  72. filter:'text',
  73. field: "fname",
  74. maxWidth: 100
  75. },
  76. {
  77. headerName: "Last Name",
  78. filter:'text',
  79. field: "lname",
  80. maxWidth: 100
  81. },
  82. {
  83. headerName: "Notes",
  84. filter:'text',
  85. field: "notes"
  86. },
  87. {
  88. headerName: "Status",
  89. field: "status",
  90. editable:true,
  91. cellClass: 'badge',
  92. cellClassRules: {
  93. 'badge-danger': function(params) {return (params.value == "Junk Lead" || params.value == "Existing")},
  94. 'badge-success': function(params) { return (params.value == "Prospect" || params.value == "Open Lead")},
  95. 'badge-warning': function(params) { return params.value == "Callback"},
  96. 'badge-primary': function(params) { return params.value == "Sold"},
  97. 'badge-secondary': function(params) { return params.value == "Do Not Call"},
  98. },
  99. filter:"text",
  100. cellEditorFramework: StatusEditorComponent,
  101. cellRendererFramework: StatusRendererComponent
  102. },
  103. {
  104. headerName: "Call",
  105. field: "call",
  106. cellRenderer: this.callCellRendererFunc
  107. }
  108. ];
  109.  
  110. constructor(private _svc:GridHTTP, private _nSvc:NotificationHTTP){
  111. this.gridOptions = <GridOptions>{};
  112. this.gridOptions = {
  113. enableFilter: true,
  114. columnDefs: this.colDefinitions
  115. }
  116. this.active[0] = 1;
  117. this.active[1] = 0;
  118. }
  119. callCellRendererFunc(params) {
  120. if(params.data.length != undefined && params.data.status != "Do Not Call" && params.data.status != "Existing"){
  121. var eSpan = document.createElement('button');
  122. eSpan.innerHTML = '<i class="fa fa-microphone"></i>&nbsp; Call';
  123. //eSpan.className = 'btn';
  124. eSpan.className = 'btn-primary .btn-sm';
  125. eSpan.style.fontSize = '12px';
  126. eSpan.style.height = params.node.rowHeight*0.8 + 'px';
  127. eSpan.style.textAlign = "left";
  128. eSpan.style.width = '75px';
  129. //console.dir(params);
  130. eSpan.addEventListener('click', () => {
  131. var focusedCell = params.api.getFocusedCell().rowIndex;
  132. var uriParams = params.api.getDisplayedRowAtIndex(focusedCell).data.phone;
  133. //console.dir();
  134. //var newWindow = window.open('eye:' + uriParams.replace(new RegExp(' ', 'g'), ''));
  135. });
  136. return eSpan;
  137. }
  138. }
  139. sizeToFit() {
  140. this.gridApi.sizeColumnsToFit();
  141. }
  142.  
  143. onGridReady(params) {
  144. this.gridApi = params.api;
  145. this.gridColumnApi = params.columnApi
  146. this.data = new Array();
  147.  
  148. var username = JSON.parse(sessionStorage.getItem('username'));
  149. var data = {username};
  150. this._svc.getData(data)
  151. .subscribe(
  152. data => {
  153. if (!data) {
  154. //var newRow = this.createEmptyRow(null);
  155. //this.gridApi.updateRowData({add: newRow});
  156. } else if ('error' in data){
  157. //Display an error here.
  158. }
  159. else {
  160. //console.dir(data);
  161. if (typeof data != undefined && data.bname) {
  162. var lengthf = data.bname.length;
  163. for (var _i = 0; _i < lengthf; _i++) {
  164. var rowDefz = Array();
  165. var addRow = true;
  166. this.colDefinitions.forEach((item, index) => {
  167. if (data[item.field] !== undefined) {
  168. if(item.field == "status") {
  169. if(data[item.field][_i] == 4 || data[item.field][_i] == 5)
  170. addRow = false;
  171. rowDefz[item.field] = this.openerStatusDefinitions[data[item.field][_i]];
  172. }
  173. else
  174. rowDefz[item.field] = data[item.field][_i];
  175. }
  176. });
  177. if(addRow){
  178. this.gridApi.updateRowData({add: [rowDefz]});
  179. this.data.push(rowDefz);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. );
  186.  
  187. setTimeout(() => this.autoSizeAll(), 250);
  188. setTimeout(() => params.api.sizeColumnsToFit(), 500);
  189. console.dir(this.data);
  190. }
  191. onGridSizeChanged(params) {
  192. params.api.sizeColumnsToFit();
  193. }
  194. autoSizeAll() {
  195. //console.log("Timeout ended.");
  196. var allColumnIds = [];
  197. this.gridColumnApi.getAllColumns().forEach(function(column) {
  198. allColumnIds.push(column.colId);
  199. });
  200. this.gridColumnApi.autoSizeColumns(allColumnIds);
  201. }
  202. ngOnInit(){
  203. }
  204.  
  205. onAddRow(newItem) {
  206. var res = this.gridApi.updateRowData({ add: [newItem] });
  207. }
  208.  
  209. createEmptyRow(data){
  210. //this.gridOptions.rowData = {bname, website, fname, ...}
  211. var rowDef = new Array();
  212. if(!data){
  213. this.gridOptions.columnDefs.forEach((item, index) => {
  214. rowDef[index] = {};
  215. });
  216. }else{
  217. data.forEach((item, index) => {
  218. rowDef[index] = data[index];
  219. });
  220. }
  221. return rowDef;
  222. }
  223. myEvent(event) {
  224. this.gridApi.forEachLeafNode((node, index) => {
  225. if (node.group) {
  226. //console.log(index + " -> group: " + node.key);
  227. } else {
  228. //console.log(index + " -> data: " + JSON.stringify(node.data));
  229. //var j = JSON.parse(sessionStorage.getItem('gridData'));
  230. var j = {};
  231. Object.assign(j, node.data); // Does not work with IE. #COMPATIBILITY
  232. j = JSON.parse(JSON.stringify(j));
  233. //var columnReq = {bname: 1, website: 1, fname: 1, lname: 1, status: 1};
  234. var columnReq = {bname: 1, website: 1, fname: 1, lname: 1, status: 1};
  235. if(checkAvailableData(columnReq, j)){
  236. console.log("Saving grid data.");
  237. Object.assign(j, JSON.parse(sessionStorage.getItem('username')));
  238. console.dir(j);
  239. this._svc.setData(j)
  240. .subscribe(
  241. data => {
  242. if(!data){
  243. console.log("XD");
  244. }
  245. }
  246. );
  247. }
  248. }
  249. });
  250. }
  251. updateClicked(event){
  252. this.refresh = false;
  253. this.gridApi.forEachLeafNode((node, index) => {
  254. if (node.data) {
  255. var dataRowObject = <any>{};
  256. //console.log(node.data);
  257. Object.assign(dataRowObject, node.data);
  258. dataRowObject = JSON.parse(JSON.stringify(dataRowObject));
  259.  
  260. var requiredColumns = {bid: 1, status: 1};
  261. if(checkAvailableData(requiredColumns, dataRowObject)){
  262. //console.log("Updating grid PHP.");
  263. //console.dir(dataRowObject);
  264.  
  265. var updatedData = <any>{};
  266. updatedData.bid = dataRowObject.bid;
  267.  
  268. updatedData.status = this.getKeyFromArray(dataRowObject.status);
  269. console.dir(JSON.parse(JSON.stringify(updatedData)));
  270.  
  271. if(updatedData.status == 5){
  272. var data: Object = {username: this.username, bname: dataRowObject.bname}
  273. this._nSvc.setWarmLead(data)
  274. .subscribe(
  275. data => {
  276. if(data)
  277. //console.log("All is well.");
  278. else
  279. //console.log("Error creating the notification.");
  280. }
  281. );
  282. this.gridApi.updateRowData({remove: [node.data]});
  283. }
  284. this._svc.updateData(updatedData)
  285. .subscribe(
  286. data => {
  287. if(data == 1)
  288. //data
  289. else
  290. console.log(data);
  291. //else
  292. //console.log("Error saving data.");
  293. }
  294. );
  295. }
  296. }
  297. });
  298. this.gridColumnApi.resetColumnState();
  299. this.gridColumnApi.resetColumnGroupState();
  300. this.gridApi.setSortModel(null);
  301. this.gridApi.setFilterModel(null);
  302. }
  303. onFilterTextBoxChanged() {
  304. this.gridOptions.api.setQuickFilter((<HTMLInputElement>document.getElementById('filter-text-box')).value);
  305. //console.log("onFilterTextBoxChanged() called.");
  306. }
  307.  
  308. getKeyFromArray(value){
  309. var i = 0;
  310. for (let key of this.openerStatusDefinitions) {
  311. if(key == value)
  312. return i
  313. else
  314. i++;
  315. }
  316. }
  317. }
  318. function checkAvailableData(requiredColumns, checkedObject){
  319. var lacksData = 0, isEmpty = 1, keepGoing = true;
  320. Object.keys(requiredColumns).forEach(key =>{
  321. if(keepGoing){
  322. if(!checkedObject[key]){
  323. lacksData = 1;
  324. }if(checkedObject[key]){
  325. isEmpty = 0;
  326. }if(!isEmpty && lacksData){
  327. return false;
  328. }
  329. }
  330. });
  331. if(!isEmpty && keepGoing)
  332. return true;
  333. return false;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement