Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

app-page.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import {ComponentsPage} from './components-page';
  2. import {ComponentPage} from './component-page';
  3. import {CategoryPage} from './category-page';
  4. import {PatternsPage} from './patterns-page';
  5. import {PatternPage} from './pattern-page';
  6. import {NotFoundPage} from './notfound-page';
  7. import {getQueryParams, mergeQueryString, parseQueryString} from '../util';
  8. export const createAppPageComponent = ({components, categories}) => ({
  9. el: '#app',
  10. data: {
  11. components,
  12. categories
  13. },
  14. template: `
  15. <div>
  16. <div class="pv-side-navi">
  17. <a class="pv-side-navi__title" href="/">
  18. Onsen<br />
  19. CSS<br />
  20. Components
  21. </a>
  22. <div>
  23. <a class="pv-side-navi__category" href="/">Components</a>
  24. <div v-for="category in categories" class="pv-side-navi__category-item">
  25. <a :href="'/categories/' + category.hash" class="pv-side-navi__item-link">{{category.name}}</a>
  26. </div>
  27. <a class="pv-side-navi__category" href="/patterns">Patterns</a>
  28. </div>
  29. </div>
  30. <my-router :base-params="createParams()" />
  31. </div>
  32. `,
  33. components: {
  34. 'my-router': createRouter()
  35. },
  36. methods: {
  37. createParams() {
  38. const params = {};
  39. params.components = [].concat(this.components);
  40. params.categories = [].concat(this.categories);
  41. return params;
  42. }
  43. }
  44. });
  45. const createRouter = () => {
  46. return {
  47. props: ['baseParams'],
  48. data: () => {
  49. return {
  50. component: ComponentsPage,
  51. params: {},
  52. query: getQueryParams()
  53. };
  54. },
  55. created() {
  56. // Load query string
  57. page('*', (context, next) => {
  58. requestAnimationFrame(() => {
  59. context.query = parseQueryString(location.search.slice(0));
  60. next();
  61. });
  62. });
  63. page('*', (context, next) => {
  64. document.body.scrollTop = document.body.scrollLeft = 0;
  65. // Load selected theme css.
  66. const theme = context.query.theme;
  67. if (typeof theme === 'string' && theme !== '') {
  68. document.querySelector('#theme-css').setAttribute('href', `/${theme}.css`);
  69. } else {
  70. document.querySelector('#theme-css').setAttribute('href', '/onsen-css-components.css');
  71. }
  72. next();
  73. });
  74. page('/components/:id', (context) => {
  75. this.component = ComponentPage;
  76. this.params = context.params;
  77. this.query = context.query;
  78. });
  79. page('/categories/:id', (context) => {
  80. this.component = CategoryPage;
  81. this.params = context.params;
  82. this.query = context.query;
  83. });
  84. page('/patterns', (context) => {
  85. this.component = PatternsPage;
  86. this.params = context.params;
  87. this.query = context.query;
  88. });
  89. page('/patterns/:id', (context) => {
  90. this.component = PatternPage;
  91. this.params = context.params;
  92. this.query = context.query;
  93. });
  94. page('/', (context) => {
  95. this.component = ComponentsPage;
  96. this.params = context.params;
  97. this.query = context.query;
  98. });
  99. page('*', () => {
  100. this.component = NotFoundPage;
  101. this.params = context.params;
  102. this.query = context.query;
  103. });
  104. page({click: false});
  105. },
  106. render(h) {
  107. const props = {};
  108. if (this.baseParams) {
  109. for (let key in this.baseParams) {
  110. if (this.baseParams.hasOwnProperty(key)) {
  111. props[key] = this.baseParams[key];
  112. }
  113. }
  114. }
  115. for (let key in this.params) {
  116. if (this.params.hasOwnProperty(key)) {
  117. props[key] = this.params[key];
  118. }
  119. }
  120. props.query = {};
  121. for (let key in this.query) {
  122. if (this.query.hasOwnProperty(key)) {
  123. props.query[key] = this.query[key];
  124. }
  125. }
  126. return h(this.component, {props});
  127. }
  128. };
  129. };
  130. document.body.addEventListener('click', e => {
  131. if (e.metaKey || e.ctrlKey || e.shiftKey) {
  132. return;
  133. }
  134. if (e.defaultPrevented) {
  135. return;
  136. }
  137. // ensure link
  138. let el = e.target;
  139. while (el && 'A' !== el.nodeName) {
  140. el = el.parentNode;
  141. }
  142. if (!el || 'A' !== el.nodeName) {
  143. return;
  144. }
  145. // ensure non-hash for the same path
  146. var link = el.getAttribute('href');
  147. if (el.pathname === location.pathname && (el.hash || '#' === link)) {
  148. return;
  149. }
  150. // Check for mailto: in the href
  151. if (link && link.indexOf('mailto:') > -1) {
  152. return;
  153. }
  154. // check target
  155. if (el.target) {
  156. return;
  157. }
  158. // rebuild path
  159. const path = el.pathname + mergeQueryString(el.search, location.search) + (el.hash || '');
  160. e.preventDefault();
  161. page.show(path);
  162. });