vue-app.ejs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <script>
  2. var sort_data = function (objects, sort) {
  3. if(sort == 'desc'){
  4. return objects.sort(function(a, b) {
  5. a = moment(a.start_date);
  6. b = moment(b.start_date);
  7. return a>b ? -1 : a<b ? 1 : 0;
  8. });
  9. } else if(sort == "asc"){
  10. return objects.sort(function(a,b){return moment(a.start_date) - moment(b.start_date)});
  11. }
  12. }
  13. var generate_search_terms = function(projects){
  14. var terms = [];
  15. projects.forEach(project => {
  16. terms.push({ text: project.title, type: 'project', classes : 'result-project' });
  17. var date = moment(new Date(project.date)).format('YYYY');
  18. terms.push({ text: date, type: 'year', classes : 'result-date' });
  19. project.tags.forEach(tag => {
  20. terms.push({ text: tag, type: 'tag', classes : 'result-tag' });
  21. })
  22. project.contacts.forEach(contact => {
  23. terms.push({text: contact.name, type: 'contact', classes : 'result-contact'});
  24. })
  25. });
  26. var filtered_terms = [];
  27. terms.forEach(term => {
  28. var found = false;
  29. for (let i = 0; i < filtered_terms.length; i++) {
  30. if(term.text == filtered_terms[i].text) found = true;
  31. }
  32. if(found == false) filtered_terms.push(term);
  33. });
  34. return filtered_terms;
  35. }
  36. var app = new Vue({
  37. el: '#vue-app',
  38. data: {
  39. view : {
  40. type: 'cards',
  41. sort : 'desc'
  42. },
  43. contactMessage : {
  44. name: "",
  45. email: "",
  46. content: ""
  47. },
  48. contactMessageSuccess : false,
  49. contactMessageError : false,
  50. projects : sort_data(projectsList, 'desc'),
  51. search_query : '',
  52. search_tags : [],
  53. autocompleteSearchTerms: generate_search_terms(projectsList)
  54. },
  55. methods: {
  56. filterProjects: function(){
  57. console.log('Filtering projects')
  58. },
  59. getDate: function(date){
  60. return moment(new Date(date)).fromNow();
  61. },
  62. getSearchTagIcon: function(category){
  63. switch (category) {
  64. case 'contact':
  65. return 'fa-user';
  66. case 'tag':
  67. return 'fa-tag';
  68. case 'project':
  69. return 'fa-folder';
  70. case 'year':
  71. return 'fa-calendar';
  72. default:
  73. return '';
  74. }
  75. },
  76. sendMessage: function(contactMessage){
  77. if(contactMessage.name != "" && contactMessage.email != "" && contactMessage.content != ""){
  78. console.log("sending message");
  79. var req = new XMLHttpRequest();
  80. req.overrideMimeType("application/json");
  81. req.open('POST', "https://bj2npxevi6.execute-api.us-east-1.amazonaws.com/prod/jamesperet-lambda-mailer", true);
  82. req.onload = function() {
  83. var res = req.responseText;
  84. console.log(res)
  85. if(req.status != 200) app.contactMessageError = true;
  86. else app.contactMessageSuccess = true;
  87. };
  88. req.send(JSON.stringify(contactMessage));
  89. }
  90. },
  91. resetContactForm: function(){
  92. if(app.contactMessageError == true) app.contactMessageError = false;
  93. else{
  94. app.contactMessageSuccess = false;
  95. app.contactMessage.content = ""
  96. }
  97. }
  98. },
  99. computed: {
  100. filterSearchTerms() {
  101. if(this.search_query.length == 1) return [];
  102. return this.autocompleteSearchTerms.filter(i => {
  103. return i.text.toLowerCase().indexOf(this.search_query.toLowerCase()) !== -1;
  104. });
  105. },
  106. filteredProjects(){
  107. var result = [];
  108. if(this.search_tags.length == 0){
  109. return this.projects;
  110. } else {
  111. console.log(this.search_tags);
  112. var filtered = [];
  113. this.projects.forEach(project => {
  114. filtered.push(project);
  115. })
  116. this.search_tags.forEach(search_tag => {
  117. search = search_tag.text.toLowerCase();
  118. if(search_tag.type == 'project'){
  119. filtered = filtered.filter(project => {
  120. title = project.title.toLowerCase();
  121. if(title.includes(search) || title == search ){
  122. return true
  123. }
  124. });
  125. } else if(search_tag.type == 'tag') {
  126. console.log("search is tag")
  127. filtered = filtered.filter(project => {
  128. for (let i = 0; i < project.tags.length; i++) {
  129. const tag = project.tags[i];
  130. if(tag == search) return true;
  131. }
  132. })
  133. } else if(search_tag.type == 'contact') {
  134. console.log("search is contact")
  135. filtered = filtered.filter(project => {
  136. for (let i = 0; i < project.contacts.length; i++) {
  137. var contact = project.contacts[i].name.toLowerCase();
  138. if(contact == search) return true;
  139. }
  140. })
  141. } else if(search_tag.type == 'date') {
  142. console.log("search is date")
  143. filtered = filtered.filter(project => {
  144. if(moment(new Date(project.date)).format('YYYY') == search) return true
  145. })
  146. } else {
  147. filtered = filtered.filter(project => {
  148. title = project.title.toLowerCase();
  149. if(title.includes(search) || title == search ){
  150. return true
  151. }
  152. for (let i = 0; i < project.tags.length; i++) {
  153. const tag = project.tags[i];
  154. if(tag.includes(search) || tag == search) {
  155. return true;
  156. }
  157. }
  158. for (let i = 0; i < project.contacts.length; i++) {
  159. var contact = project.contacts[i].name.toLowerCase();
  160. if(contact.includes(search) || contact == search) {
  161. return true;
  162. }
  163. }
  164. if(moment(new Date(project.date)).format('YYYY') == search) return true
  165. if(project.description.includes(search)){
  166. return true
  167. }
  168. return false;
  169. })
  170. }
  171. });
  172. return filtered;
  173. }
  174. }
  175. },
  176. mounted: function () {
  177. this.$nextTick(function () {
  178. // Code that will run only after the
  179. // entire view has been rendered
  180. if(app.projects.length > 0) console.log(app.projects);
  181. })
  182. }
  183. })
  184. </script>