Guest User

Untitled

a guest
Feb 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. <template>
  2.  
  3. <div class="text-center">
  4.  
  5. <div class="panel panel-default">
  6. <div class="panel-heading">
  7. <h4>Recent Views</h4>
  8. </div>
  9. <div class="panel-body">
  10. <div v-for="view in recentViews" :key="view.id">
  11. <a :href="'/' + view.sequence">{{ view.sequence }}</a> was viewed <strong>{{ view.created_at | ago }}</strong>
  12. </div>
  13. <div >
  14. <a href="#" @click.prevent="numRecentViews += 5">more</a>
  15. </div>
  16. </div>
  17. </div>
  18.  
  19. <div class="panel panel-default">
  20. <div class="panel-body">
  21. <div>
  22. <strong>Zoom:</strong>
  23. <a href="#" @click.prevent="timeRange = 'year'">1 Year</a>
  24. <a href="#" @click.prevent="timeRange = 'month'">1 Month</a>
  25. <a href="#" @click.prevent="timeRange = 'week'">1 Week</a>
  26. </div>
  27. <line-chart :chartData="chartData" @chartClick="handleClick"></line-chart>
  28. </div>
  29. </div>
  30.  
  31. <div v-if="focusViews.length" class="panel panel-default">
  32. <div class="panel-heading">
  33. <h4>Policies viewed {{ focusDate }}</h4>
  34. </div>
  35. <div class="panel-body">
  36. <div v-for="view in focusViews" :key="view.id">
  37. <a :href="'/' + view.sequence">{{ view.sequence }}</a>
  38. <span v-if="timeRange == 'year'"> - {{ view.created_at | date }}</span>
  39. </div>
  40. </div>
  41. </div>
  42.  
  43. </div>
  44.  
  45. </template>
  46.  
  47. <script>
  48.  
  49. import LineChart from '../components/LineChart';
  50. import moment from 'moment';
  51.  
  52. export default {
  53.  
  54. components: { LineChart },
  55.  
  56. props: {
  57. views: {
  58. type: String,
  59. default: ''
  60. }
  61. },
  62.  
  63. data() {
  64. return {
  65. myViews: null,
  66. timeRange: 'year',
  67. numRecentViews: 5,
  68. focusDate: null,
  69. focusViews: [],
  70. dates: [],
  71. activeData: []
  72. }
  73. },
  74.  
  75. mounted() {
  76. this.myViews = JSON.parse(this.views);
  77. },
  78.  
  79. computed: {
  80.  
  81. chartData() {
  82.  
  83. if (!this.myViews) return {};
  84.  
  85. switch(this.timeRange){
  86. case 'week':
  87. return this.weekData();
  88. case 'month':
  89. return this.monthData();
  90. }
  91.  
  92. return this.yearData();
  93.  
  94. },
  95.  
  96. recentViews() {
  97. if (!this.myViews) return [];
  98.  
  99. return this.myViews.slice(0, this.numRecentViews);
  100. },
  101.  
  102. },
  103.  
  104. methods: {
  105.  
  106. handleClick(element){
  107. let x = element._index;
  108. let date = this.chartData.labels[x];
  109.  
  110. this.focusDate = this.dates[x];
  111. this.focusViews = this.activeData[x];
  112. },
  113.  
  114. yearData() {
  115.  
  116. let labels = [];
  117. let dates = [];
  118. let data = Array(12);
  119.  
  120. for (let i = 12; i > 0; i--){
  121. dates.push('in ' + moment().subtract(i, 'months').format('MMMM YYYY'));
  122. labels.push(moment().subtract(i, 'months').format('MMM'));
  123. data[i-1] = [];
  124. }
  125.  
  126. this.dates = dates;
  127.  
  128. this.myViews.forEach(view => {
  129. const i = labels.indexOf(moment(view.created_at).format('MMM'));
  130. data[i].push(view);
  131. });
  132.  
  133. this.activeData = data;
  134.  
  135. return {
  136. labels,
  137. datasets: [{
  138. data: data.map(d => d.length),
  139. backgroundColor: "#2BBE83"
  140. }]
  141. };
  142.  
  143. },
  144.  
  145. monthData() {
  146.  
  147. let labels = [];
  148. let dates = [];
  149. let data = Array(30);
  150.  
  151. let viewsThisMonth = this.myViews.filter(v => {
  152. return moment().diff(v.created_at, 'days') < 30;
  153. });
  154.  
  155. for (let i = 30; i > 0; i--){
  156. dates.push('on ' + moment().subtract(i, 'days').format('MMMM Do YYYY'));
  157. labels.push(moment().subtract(i, 'days').format('MMM D'));
  158. data[i - 1] = [];
  159. }
  160.  
  161. viewsThisMonth.forEach(view => {
  162. const i = labels.indexOf(moment(view.created_at).format('MMM D'));
  163. data[i].push(view);
  164. });
  165.  
  166. this.dates = dates;
  167. this.activeData = data;
  168.  
  169. return {
  170. labels,
  171. datasets: [{
  172. data: data.map(d => d.length),
  173. backgroundColor: "#2BBE83"
  174. }]
  175. }
  176.  
  177. },
  178.  
  179. weekData() {
  180.  
  181. let labels = [];
  182. let dates = [];
  183. let data = Array(7);
  184.  
  185. let viewsThisWeek = this.myViews.filter(v => {
  186. return moment().diff(v.created_at, 'days') < 7;
  187. });
  188.  
  189. for (let i = 7; i > 0; i--){
  190. dates.push('on ' + moment().subtract(i, 'days').format('MMMM Do YYYY'));
  191. labels.push(moment().subtract(i, 'days').format('MMM D'));
  192. data[i - 1] = [];
  193. }
  194.  
  195. viewsThisWeek.forEach(view => {
  196. const i = labels.indexOf(moment(view.created_at).format('MMM D'));
  197. data[i].push(view);
  198. });
  199.  
  200. this.dates = dates;
  201. this.activeData = data;
  202.  
  203. return {
  204. labels,
  205. datasets: [{
  206. data: data.map(d => d.length),
  207. backgroundColor: "#2BBE83"
  208. }]
  209. }
  210. }
  211. },
  212.  
  213. filters: {
  214. ago: (value) => {
  215. if (!value) return '';
  216. return moment(value).fromNow();
  217. },
  218.  
  219. date: (value) => {
  220. if (!value) return '';
  221. return moment(value).format('MMMM Do');
  222.  
  223. }
  224. }
  225. }
  226.  
  227. </script>
Add Comment
Please, Sign In to add comment