Guest User

Untitled

a guest
Apr 24th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. import {Mail} from '../../../../model/rest/Mail';
  2. import {MailKey} from '../../../../model/rest/MailKey';
  3. import {MailExpression} from '../../../../model/rest/MailExpression';
  4.  
  5. declare let angular: any;
  6.  
  7.  
  8. export class MailListController {
  9.  
  10. private mails: Mail[];
  11. private basicMail: Mail;
  12. private scope: any;
  13. private genExpressions: Array<string> = ['SERVICES_START', 'SERVICES_END'];
  14.  
  15. /** @ngInject */
  16. constructor(private $q,
  17. private $scope,
  18. private $interpolate,
  19. private Restangular,
  20. private toastr,
  21. private $document,
  22. private $window,
  23. mails: Mail[]) {
  24. $scope.ctrl = this;
  25. $scope.tableData = mails;
  26. this.mails = mails;
  27. this.basicMail = this.getBasicMail();
  28. this.scope = $scope;
  29. }
  30.  
  31. public mailSort = (item: Mail) => item.type.order;
  32.  
  33. getExpression = (exp: string) => {
  34. return exp !== '' && exp.indexOf('%') < 0 ? `%${exp}%` : exp;
  35. };
  36.  
  37. public save(item: Mail) {
  38.  
  39. this.prepareMailBody(item);
  40.  
  41. /** Update array in controller at first*/
  42. this.mails.forEach((mail: Mail) => {
  43. if (mail.id === item.id) {
  44. mail = angular.copy(item);
  45. }
  46. });
  47.  
  48. this.basicMail = this.getBasicMail();
  49. /** Save in DB */
  50. return this.Restangular
  51. .one('mails', item.id)
  52. .customPUT(item).then(() => {
  53. this.toastr.info('Mail template was updated successfully');
  54. this.scope.waitResponse = false;
  55. }, () => {
  56. this.toastr.info('Something went wrong');
  57. });
  58. }
  59.  
  60. /**
  61. * Render expressions in Mail
  62. * @param {Mail} item
  63. * @returns {string}
  64. */
  65. public getMail(item: Mail): string {
  66.  
  67. let compile = item.body;
  68. let literals = item.expressions;
  69. if (!compile) compile = '';
  70. /**
  71. * Fix bug with editor
  72. * @type {RegExp}
  73. */
  74. this.prepareMailBody(item);
  75.  
  76. if (this.isGeneratorBody(compile)) compile = this.getGeneratorBody(compile, item.expressions);
  77.  
  78. compile = this.getTextBody(compile, item.expressions);
  79.  
  80. if (!this.isBasicMail(item)) {
  81. return this.getBasicContent(compile);
  82. }
  83. return compile;
  84.  
  85. }
  86.  
  87. /**
  88. * Insert HTML to textEditor
  89. * @param text
  90. */
  91. public insertToEditor(text: string) {
  92. var sel, range;
  93. if (this.$window.getSelection) {
  94. sel = this.$window.getSelection();
  95.  
  96. if (sel.getRangeAt && sel.rangeCount) {
  97. range = sel.getRangeAt(0);
  98. let element = range.commonAncestorContainer.parentElement.closest('[text-angular]');
  99. let editor = range.commonAncestorContainer.parentElement.closest('[contenteditable]');
  100. if(element!= null){
  101. range.deleteContents();
  102. range.insertNode(document.createTextNode(text));
  103. editor.focus();
  104. }
  105. }
  106. }
  107. }
  108.  
  109. private prepareMailBody(mail: Mail) {
  110. let regular = new RegExp(/<span id="selectionBoundary_\d+_\d+"[\s\S]*?(?!(<span))\<\/span\>/ig);
  111. mail.body = mail.body.replace(regular, '');
  112. }
  113.  
  114. private isBasicMail(mail: Mail): Boolean {
  115. return mail.id === this.basicMail.id;
  116. }
  117.  
  118. private isGeneratorBody(body: string): Boolean {
  119. if (!body) body = '';
  120.  
  121. let res: Boolean = false;
  122. this.genExpressions.forEach((exp) => {
  123. if (body.indexOf(exp) >= 0) res = true;
  124. });
  125. return res;
  126. }
  127.  
  128. private getTextBody(body: string, literals: MailExpression[]): string {
  129. return body.replace(/%[^%\s]+%/ig, (match, p1, offset, string) => {
  130. let literal = match.replace(/[%]/g, '');
  131. let fLiteral = null;
  132.  
  133. literals.forEach((exp) => {
  134. if (exp.expression === literal) {
  135. fLiteral = exp.value;
  136. }
  137. });
  138. return fLiteral !== null ? fLiteral : match;
  139. });
  140. }
  141.  
  142. private getGeneratorBody(body: string, literals: MailExpression[], count: number = 3): string {
  143. let reg: RegExp = new RegExp(`(?:%${this.genExpressions[0]}%)([\\s\\S]*)(?:%${this.genExpressions[1]}%)`, 'g');
  144. let repeatBody = body.match(reg); //generation sections
  145.  
  146. repeatBody.forEach((section: string) => {
  147.  
  148. let mStr: string = section.replace(reg, '$1');
  149.  
  150. let totalStr: string = '';
  151. for (let i = 1; i <= count; i++) {
  152. totalStr += this.getTextBody(mStr, literals);
  153. literals.forEach((exp) => {
  154. switch (exp.expression) {
  155. case 'SERVICE_NUMBER': {
  156. exp.value = <string>(1 + exp.value);
  157. }
  158. }
  159. });
  160. }
  161. body = body.replace(section, totalStr);
  162. });
  163.  
  164. return body;
  165. }
  166.  
  167. private getBasicMail(): Mail {
  168. let basicMail: Mail =
  169. this.mails.filter((item, index, array) => {
  170. return item.type.key === MailKey.BASIC;
  171. })[0];
  172. return angular.copy(basicMail);
  173. }
  174.  
  175. private getBasicContent(text: string) {
  176. /** Check integrity of basic template */
  177. if (this.basicMail.body.indexOf('CONTENT') < 0) {
  178. return text;
  179. }
  180.  
  181. this.basicMail.expressions.forEach((exp: MailExpression) => {
  182. if (exp.expression === 'CONTENT') {
  183. exp.value = text;
  184. }
  185. });
  186. return this.getMail(this.basicMail);
  187. }
  188.  
  189.  
  190. }
Add Comment
Please, Sign In to add comment