Edition.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <template>
  2. <MainContentLayout
  3. id="edition"
  4. :reftoscrollto="reftoscrollto"
  5. :navopened="navopened"
  6. @onCenterScrolled="onCenterScrolled"
  7. >
  8. <template v-if="!content" #header>
  9. <span class="loading">Loading ...</span>
  10. </template>
  11. <template #header>
  12. <h1>
  13. <router-link :to="{ name:'edition', params: { id: editionid }}">{{ title }}</router-link>
  14. </h1>
  15. <p v-if="meta">{{ meta.author }}</p>
  16. <aside
  17. v-if="indexitem"
  18. class="index-tooltip"
  19. :style="{ top:tooltip_top + 'px' }"
  20. :data-index="indexitem.index"
  21. :data-uuid="indexitem.uuid"
  22. @click.prevent="onClickTooltip"
  23. @keyup.enter="onClickTooltip"
  24. >
  25. <span v-if="indexitem == 'loading'">Loading ...</span>
  26. <template v-if="indexitem !== 'loading'">
  27. <h1 v-html="indexitem.title" />
  28. <p v-if="indexitem.birthDate" class="birthdeath">
  29. <time>{{ indexitem.birthDate }}</time>, <span class="place">{{ indexitem.birthPlace }}</span><br>
  30. <time>{{ indexitem.deathDate }}</time>, <span class="place">{{ indexitem.deathPlace }}</span>
  31. </p>
  32. <p v-if="indexitem.occupation" class="occupation">
  33. {{ indexitem.occupation }}
  34. </p>
  35. <p v-if="indexitem.type" class="type">
  36. {{ indexitem.type }}
  37. </p>
  38. </template>
  39. </aside>
  40. </template>
  41. <!-- default slot -->
  42. <div id="text">
  43. <template v-if="texts.length">
  44. <infinite-loading
  45. v-if="flattoc && center_scrolled"
  46. :identifier="inifinite_load_id"
  47. direction="top"
  48. :distance="inifinite_load_distance"
  49. @infinite="prevText"
  50. />
  51. <EdText
  52. v-for="text in texts"
  53. :ref="text.content.uuid"
  54. :key="text.content.uuid"
  55. :tei="text.content.tei"
  56. :uuid="text.content.uuid"
  57. :textid="textid"
  58. @onHoverLink="onHoverLink"
  59. @onLeaveLink="onLeaveLink"
  60. />
  61. <infinite-loading
  62. v-if="flattoc"
  63. :identifier="inifinite_load_id"
  64. @infinite="nextText"
  65. />
  66. </template>
  67. </div>
  68. <template #nav>
  69. <span
  70. class="nav-title"
  71. @click.prevent="onOpenCloseNav"
  72. @keyup.enter="onOpenCloseNav"
  73. >
  74. <svg xmlns="http://www.w3.org/2000/svg" width="14" height="10" role="presentation" class="vs__open-indicator"><path d="M9.211364 7.59931l4.48338-4.867229c.407008-.441854.407008-1.158247 0-1.60046l-.73712-.80023c-.407008-.441854-1.066904-.441854-1.474243 0L7 5.198617 2.51662.33139c-.407008-.441853-1.066904-.441853-1.474243 0l-.737121.80023c-.407008.441854-.407008 1.158248 0 1.600461l4.48338 4.867228L7 10l2.211364-2.40069z" /></svg>
  75. Sommaire
  76. </span>
  77. <EdToc
  78. id="toc"
  79. :toc="toc"
  80. :loadedtextsuuids="textsuuids"
  81. @onClickTocItem="onClickTocItem"
  82. />
  83. <EdPagination
  84. v-if="pagination"
  85. id="page-nav"
  86. :pagination="pagination"
  87. @onClickPaginationItem="onClickPaginationItem"
  88. />
  89. </template>
  90. </MainContentLayout>
  91. </template>
  92. <script>
  93. import qs from 'querystring'
  94. import { REST } from 'api/rest-axios'
  95. import { mapState, mapActions } from 'vuex'
  96. import MainContentLayout from '../components/Layouts/MainContentLayout'
  97. import EdText from '../components/Content/EdText'
  98. import EdToc from '../components/Content/EdToc'
  99. import EdPagination from '../components/Content/EdPagination'
  100. export default {
  101. name: 'Edition',
  102. metaInfo () {
  103. // console.log('metainfo', this.meta)
  104. return {
  105. title: this.metainfotitle
  106. }
  107. },
  108. components: {
  109. MainContentLayout,
  110. EdText,
  111. EdToc,
  112. EdPagination
  113. },
  114. data: () => ({
  115. meta: null,
  116. editionid: null,
  117. textid: null,
  118. texts: [],
  119. textsuuids: [],
  120. metainfotitle: undefined,
  121. title: undefined,
  122. author: undefined,
  123. texttitle: undefined,
  124. //
  125. indexitem: null,
  126. tooltip_top: null,
  127. //
  128. next_loaded: false,
  129. center_scrolled: false,
  130. inifinite_load_distance: 10,
  131. inifinite_load_id: +new Date(),
  132. reftoscrollto: null,
  133. //
  134. toc: null,
  135. flattoc: null,
  136. //
  137. pagination: null,
  138. //
  139. navopened: false
  140. }),
  141. computed: {
  142. ...mapState({
  143. corpusLoaded: state => state.Corpus.corpusLoaded,
  144. editionslist: state => state.Corpus.editionslist,
  145. editionsbyuuid: state => state.Corpus.editionsbyuuid
  146. })
  147. },
  148. watch: {
  149. $route (to, from) {
  150. console.log('Edition Watcher $route', from, to)
  151. if (to.params.textid) {
  152. // change textid when route change
  153. this.textid = to.params.textid
  154. } else if (this.toc) {
  155. // if no textid in new route (e.g. edition front)
  156. // but we have toc
  157. // get the first item
  158. // will be replaced by front page of edition
  159. this.textid = this.toc[0].children[1].uuid
  160. } else {
  161. this.textid = null
  162. }
  163. },
  164. textid (newid, oldid) {
  165. console.log('textid watcher', this, oldid, newid)
  166. this.texts = []
  167. this.textsuuids = []
  168. this.pages = []
  169. this.pagesOtpions = []
  170. if (newid) {
  171. this.getTextContent(newid)
  172. this.inifinite_load_id += 1
  173. }
  174. },
  175. textdata (newtxtdata, oldtxtdata) {
  176. console.log('textdata watcher', oldtxtdata, newtxtdata)
  177. this.metainfotitle = `${this.title} ${newtxtdata.meta.title}`
  178. },
  179. page_selected (newp, oldp) {
  180. console.log('page_selected watcher', oldp, newp)
  181. this.scrollToPage(newp)
  182. },
  183. flattoc (n, o) {
  184. console.log('flattoc watcher', o, n)
  185. // this.scrollToPage(newp)
  186. }
  187. },
  188. created () {
  189. // console.log('Edition this.$route.params.id', this.$route.params.id)
  190. this.editionid = this.$route.params.id
  191. // get the text if textid available
  192. if (this.$route.params.textid) {
  193. this.textid = this.$route.params.textid
  194. }
  195. // wait for editions list from Corpus Store if not already loaded
  196. if (!this.corpusLoaded) {
  197. // this.getCorpuses()
  198. // subsribe to store to get the editionbyuuid list
  199. // https://dev.to/viniciuskneves/watch-for-vuex-state-changes-2mgj
  200. this.edUuuidsUnsubscribe = this.$store.subscribe((mutation, state) => {
  201. // console.log('Edition store subscribe', mutation.type)
  202. if (mutation.type === 'Corpus/setEditionsByUUID') {
  203. // console.log('Edition state.Coprus.editionsbyuuid', this.editionid, state.Corpus.editionsbyuuid)
  204. this.title = this.metainfotitle = state.Corpus.editionsbyuuid[this.editionid].title
  205. }
  206. if (mutation.type === 'Corpus/setTocs') {
  207. console.log('Edition Corpus/setTocs', this.editionid, state.Corpus.editionsbyuuid)
  208. this.toc = state.Corpus.editionsbyuuid[this.editionid].toc
  209. }
  210. if (mutation.type === 'Corpus/buildFlatTocs') {
  211. console.log('Edition Corpus/buildFlatTocs', this.editionid, state.Corpus.editionsbyuuid)
  212. this.flattoc = state.Corpus.editionsbyuuid[this.editionid].flattoc
  213. // launch infinitloading
  214. this.inifinite_load_id += 1
  215. // if no textid in new route (e.g. edition front)
  216. // but we have toc
  217. // get the first item
  218. // will be replaced by front page of edition
  219. if (!this.textid) { this.textid = this.flattoc[1] }
  220. }
  221. if (mutation.type === 'Corpus/setPaginations') {
  222. // console.log('Edition state.Coprus.editionsbyuuid', this.editionid, state.Corpus.editionsbyuuid)
  223. this.pagination = state.Corpus.editionsbyuuid[this.editionid].pagination
  224. }
  225. })
  226. } else {
  227. // console.log('');
  228. this.title = this.metainfotitle = this.editionsbyuuid[this.editionid].title
  229. this.toc = this.editionsbyuuid[this.editionid].toc
  230. this.flattoc = this.editionsbyuuid[this.editionid].flattoc
  231. // if no textid in new route (e.g. edition front)
  232. // but we have toc
  233. // get the first item
  234. // will be replaced by front page of edition
  235. if (!this.textid) { this.textid = this.toc[0].children[0].uuid }
  236. this.pagination = this.editionsbyuuid[this.editionid].pagination
  237. }
  238. },
  239. methods: {
  240. ...mapActions({
  241. getCorpuses: 'Corpus/getCorpuses'
  242. }),
  243. getTextContent (textid, $state = null, direction = 'next') {
  244. console.log('getTextContent', textid)
  245. let params = {
  246. depth: 0
  247. }
  248. let q = qs.stringify(params)
  249. REST.get(`${window.apipath}/items/${textid}?${q}`, {})
  250. .then(({ data }) => {
  251. console.log('text REST: data', data)
  252. if (direction === 'next') {
  253. this.texts.push(data)
  254. this.textsuuids.push(data.content.uuid)
  255. } else {
  256. this.texts.unshift(data)
  257. this.textsuuids.unshift(data.content.uuid)
  258. }
  259. if ($state) {
  260. $state.loaded()
  261. this.next_loaded = true
  262. }
  263. })
  264. .catch((error) => {
  265. console.warn('Issue with getTextContent', error)
  266. Promise.reject(error)
  267. // if some item don't load and if we come from infinite loading
  268. // retry with next step
  269. if ($state) {
  270. switch (direction) {
  271. case 'next':
  272. this.nextText($state, 2)
  273. break
  274. case 'prev':
  275. this.prevText($state, 2)
  276. break
  277. }
  278. }
  279. // this.$router.replace({
  280. // name: 'notfound',
  281. // query: { fullpath: this.$route.path }
  282. // })
  283. })
  284. },
  285. onCenterScrolled (e) {
  286. // console.log('Edition centerScrolled(e)', e.target.scrollTop)
  287. if (!this.center_scrolled && e.target.scrollTop > this.inifinite_load_distance * 1.5) {
  288. this.center_scrolled = true
  289. }
  290. this.indexitem = null
  291. },
  292. nextText ($state, indent = 1) {
  293. console.log('infinite loading nextText()')
  294. let indexofnext = this.flattoc.indexOf(this.textsuuids[this.textsuuids.length - 1]) + indent
  295. if (indexofnext < this.flattoc.length) {
  296. this.getTextContent(this.flattoc[indexofnext], $state, 'next')
  297. } else {
  298. $state.complete()
  299. }
  300. },
  301. prevText ($state, indent = 1) {
  302. console.log('infinite loading prevText()')
  303. let indexofprev = this.flattoc.indexOf(this.textsuuids[0]) - indent
  304. if (indexofprev >= 0) {
  305. this.getTextContent(this.flattoc[indexofprev], $state, 'prev')
  306. } else {
  307. $state.complete()
  308. }
  309. },
  310. onHoverLink (elmt) {
  311. console.log('Edition onHoverLink(elmt)', elmt)
  312. this.tooltip_top = elmt.rect.top
  313. this.getIndexItem(elmt)
  314. },
  315. onLeaveLink () {
  316. console.log('Edition onLeaveLink()')
  317. this.indexitem = null
  318. },
  319. getIndexItem (item) {
  320. this.indexitem = 'loading'
  321. REST.get(`${window.apipath}/index${item.index.charAt(0).toUpperCase()}${item.index.slice(1)}/${item.uuid}`, {})
  322. .then(({ data }) => {
  323. console.log('index tooltip REST: data', data)
  324. if (this.indexitem === 'loading') {
  325. this.indexitem = data.content
  326. this.indexitem.index = item.index
  327. }
  328. })
  329. .catch((error) => {
  330. console.warn('Issue with index tooltip rest', error)
  331. Promise.reject(error)
  332. this.indexitem = null
  333. })
  334. },
  335. onClickTocItem (uuid) {
  336. console.log('Edition onClickTocItem', uuid, this.$refs)
  337. if (this.textsuuids.indexOf(uuid) !== -1) {
  338. // if already loaded, scroll to uuid
  339. this.reftoscrollto = `.tei[data-uuid="${uuid}"]`
  340. } else {
  341. // if not already loaded, change route
  342. this.$router.push({
  343. name: `editiontext`,
  344. params: {
  345. id: this.editionid,
  346. textid: uuid
  347. }
  348. })
  349. }
  350. },
  351. onClickPaginationItem (o) {
  352. console.log('onClickPaginationItem', o)
  353. if (this.textsuuids.indexOf(o.uuid) !== -1) {
  354. // if already loaded, scroll to uuid
  355. // this.scrollToPage(o)
  356. this.reftoscrollto = `span[role="pageBreak"][id="${o.code}"]`
  357. } else {
  358. // if not already loaded, change route
  359. this.$router.push({
  360. name: `editiontext`,
  361. params: {
  362. id: this.editionid,
  363. textid: o.uuid
  364. }
  365. })
  366. }
  367. },
  368. // scrollToPage (p) {
  369. // console.log('scrollToPage', p)
  370. //
  371. // },
  372. onOpenCloseNav (e) {
  373. console.log('onOpenCloseNav', e)
  374. this.navopened = !this.navopened
  375. },
  376. onClickTooltip (e) {
  377. console.log(`onClickTooltip index: ${e.target.dataset.index}, uuid: ${e.target.dataset.uuid}`)
  378. this.$router.push({
  379. name: e.target.dataset.index,
  380. params: { id: e.target.dataset.uuid }
  381. })
  382. }
  383. }
  384. }
  385. </script>
  386. <style lang="scss" scoped>
  387. </style>