app.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. var TR_EN = {
  2. 'title': 'bow',
  3. 'comment': 'comment'
  4. };
  5. function shortenParagraph(paragraph) {
  6. var sentences = paragraph.split('.');
  7. // console.log(sentences);
  8. return sentences[0] + '. ' + sentences[sentences.length - 2] + '.';
  9. }
  10. Vue.component('article-new', {
  11. props: [],
  12. computed: {
  13. title: function() {
  14. var sstr = this.defaultArticle.url.split('/');
  15. var t = sstr[sstr.length -1];
  16. this.defaultArticle.title = t;
  17. this.summary;
  18. return t;
  19. },
  20. summary: function() {
  21. if (this.defaultArticle.title === undefined || this.defaultArticle.title == '') {
  22. console.log('undefined title');
  23. this.defaultArticle.summary = '';
  24. return;
  25. }
  26. var keyword = this.defaultArticle.title;
  27. var searchUrl = 'https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&prop=extracts&exintro=&explaintext=&titles=' + keyword;
  28. var self = this;
  29. var query = function() {
  30. var xhr = new XMLHttpRequest();
  31. xhr.onload = function() {
  32. var data = {};
  33. try {
  34. data = JSON.parse(xhr.responseText);
  35. } catch(e){
  36. console.log('could not parse JSON');
  37. }
  38. var pages = {};
  39. if ('query' in data) {
  40. if ('pages' in data.query) {
  41. pages = data.query.pages;
  42. }
  43. }
  44. for (var p in pages) {
  45. if ('extract' in pages[p]) {
  46. self.defaultArticle.summary = pages[p].extract;
  47. self.defaultArticle.title = pages[p].title;
  48. self.defaultArticle.id = pages[p].pageid;
  49. }
  50. }
  51. };
  52. xhr.open('GET', searchUrl, true);
  53. try {
  54. xhr.send();
  55. } catch(e) {
  56. console.log('err');
  57. }
  58. }
  59. _.debounce(query, 2000, {leading: true})();
  60. // query();
  61. return '';
  62. },
  63. },
  64. methods: {
  65. addArticle : function() {
  66. if (this.defaultArticle.url == '') {
  67. return;
  68. }
  69. app.articles.push(JSON.parse(JSON.stringify(this.defaultArticle)));
  70. this.defaultArticle = {
  71. title: undefined,
  72. summary: '',
  73. url: '',
  74. id: ''
  75. }
  76. },
  77. },
  78. data: function () {
  79. return {
  80. defaultArticle: {
  81. title: undefined,
  82. summary: '',
  83. url: '',
  84. id: ''
  85. },
  86. };
  87. },
  88. template: '\
  89. <div class="add">\
  90. Search wikipedia or paste URL\
  91. <input placeholder="url" v-model="defaultArticle.url" />\
  92. <button v-if="defaultArticle.summary" type="button" @click="addArticle();writeToDB();">Add</button>\
  93. <div class="title">{{title}}</div>\
  94. <div class="summary">{{defaultArticle.summary}}</div>\
  95. </div>'
  96. });
  97. var app = new Vue({
  98. // router,
  99. el: '#app',
  100. data: {
  101. defaultArticle: {
  102. title: '',
  103. summary: '',
  104. url: ''
  105. },
  106. articles : [],
  107. tr: TR_EN,
  108. compact: false
  109. },
  110. mounted: function () {
  111. var splash = document.getElementById("splash");
  112. splash.style.display = "none";
  113. // unhide main container after mount
  114. this.$el.style.display = "block";
  115. var firebaseConfig = {
  116. apiKey: "AIzaSyAl00bG_4Qt8AEV-aXIHy2o2m7fJfbmoRc",
  117. authDomain: "bestof-1.firebaseapp.com",
  118. databaseURL: "https://bestof-1.firebaseio.com",
  119. projectId: "bestof-1",
  120. storageBucket: "bestof-1.appspot.com",
  121. messagingSenderId: "115676832898"
  122. };
  123. firebase.initializeApp(firebaseConfig);
  124. readTaskData();
  125. },
  126. computed: {
  127. articles: function() {
  128. // Just a wrapper so we can sort later
  129. return this.articles;
  130. },
  131. ready: function() {
  132. return this.articles.length > 0;
  133. }
  134. },
  135. methods: {
  136. getLink: function(article) {
  137. if (article.id) {
  138. return 'https://en.wikipedia.org/?curid=' + article.id;
  139. }
  140. return article.url;
  141. }
  142. },
  143. watch: {
  144. },
  145. });
  146. // FIREBASE DB ////////////////////////////////////////////////////
  147. function writeToDB() {
  148. firebase.database().ref('articles').set(app.articles);
  149. }
  150. function readTaskData() {
  151. var t = firebase.database().ref('articles');
  152. t.on("value", function(snapshot) {
  153. var a = snapshot.val();
  154. // var tasks = snapshot.val();
  155. if (Array.isArray(a)) {
  156. app.articles = a;
  157. }
  158. }, function (error) {
  159. console.log("Error: " + error.code);
  160. });
  161. }