history.vue 18.1 KB
Newer Older
1
<template lang='pug'>
2
  v-app(:dark='$vuetify.theme.dark').history
3 4 5
    nav-header
    v-content
      v-toolbar(color='primary', dark)
NGPixel's avatar
NGPixel committed
6
        .subheading Viewing history of #[strong /{{path}}]
7 8 9 10 11
        template(v-if='$vuetify.breakpoint.mdAndUp')
          v-spacer
          .caption.blue--text.text--lighten-3.mr-4 Trail Length: {{total}}
          .caption.blue--text.text--lighten-3 ID: {{pageId}}
          v-btn.ml-4(depressed, color='blue darken-1', @click='goLive') Return to Live Version
12 13
      v-container(fluid, grid-list-xl)
        v-layout(row, wrap)
NGPixel's avatar
NGPixel committed
14 15
          v-flex(xs12, md4)
            v-chip.my-0.ml-6(
16 17
              label
              small
18 19
              :color='$vuetify.theme.dark ? `grey darken-2` : `grey lighten-2`'
              :class='$vuetify.theme.dark ? `grey--text text--lighten-2` : `grey--text text--darken-2`'
20 21 22 23 24
              )
              span Live
            v-timeline(
              dense
              )
25
              v-timeline-item.pb-2(
26
                v-for='(ph, idx) in fullTrail'
27 28 29 30
                :key='ph.versionId'
                :small='ph.actionType === `edit`'
                :color='trailColor(ph.actionType)'
                :icon='trailIcon(ph.actionType)'
31
                )
32
                v-card.radius-7(flat, :class='trailBgColor(ph.actionType)')
33
                  v-toolbar(flat, :color='trailBgColor(ph.actionType)', height='40')
34
                    .caption(:title='$options.filters.moment(ph.versionDate, `LLL`)') {{ ph.versionDate | moment('ll') }}
NGPixel's avatar
NGPixel committed
35
                    v-divider.mx-3(vertical)
36 37 38
                    .caption(v-if='ph.actionType === `edit`') Edited by #[strong {{ ph.authorName }}]
                    .caption(v-else-if='ph.actionType === `move`') Moved from #[strong {{ph.valueBefore}}] to #[strong {{ph.valueAfter}}] by #[strong {{ ph.authorName }}]
                    .caption(v-else-if='ph.actionType === `initial`') Created by #[strong {{ ph.authorName }}]
39
                    .caption(v-else-if='ph.actionType === `live`') Last Edited by #[strong {{ ph.authorName }}]
40
                    .caption(v-else) Unknown Action by #[strong {{ ph.authorName }}]
41 42
                    v-spacer
                    v-menu(offset-x, left)
Nick's avatar
Nick committed
43
                      template(v-slot:activator='{ on }')
NGPixel's avatar
NGPixel committed
44
                        v-btn.mr-2.radius-4(icon, v-on='on', small, tile): v-icon mdi-dots-horizontal
Nick's avatar
Nick committed
45
                      v-list(dense, nav).history-promptmenu
46
                        v-list-item(@click='setDiffSource(ph.versionId)', :disabled='(ph.versionId >= diffTarget && diffTarget !== 0) || ph.versionId === 0')
47 48
                          v-list-item-avatar(size='24'): v-avatar A
                          v-list-item-title Set as Differencing Source
49
                        v-list-item(@click='setDiffTarget(ph.versionId)', :disabled='ph.versionId <= diffSource && ph.versionId !== 0')
50 51 52
                          v-list-item-avatar(size='24'): v-avatar B
                          v-list-item-title Set as Differencing Target
                        v-list-item(@click='viewSource(ph.versionId)')
Nick's avatar
Nick committed
53
                          v-list-item-avatar(size='24'): v-icon mdi-code-tags
54
                          v-list-item-title View Source
55
                        v-list-item(@click='download(ph.versionId)')
Nick's avatar
Nick committed
56
                          v-list-item-avatar(size='24'): v-icon mdi-cloud-download-outline
57
                          v-list-item-title Download Version
NGPixel's avatar
NGPixel committed
58
                        v-list-item(@click='restore(ph.versionId, ph.versionDate)', :disabled='ph.versionId === 0')
59
                          v-list-item-avatar(size='24'): v-icon(:disabled='ph.versionId === 0') mdi-history
60
                          v-list-item-title Restore
61
                        v-list-item(@click='branchOff(ph.versionId)')
Nick's avatar
Nick committed
62
                          v-list-item-avatar(size='24'): v-icon mdi-source-branch
63
                          v-list-item-title Branch off from here
NGPixel's avatar
NGPixel committed
64 65 66 67 68 69
                    v-btn.mr-2.radius-4(
                      @click='setDiffSource(ph.versionId)'
                      icon
                      small
                      depressed
                      tile
70
                      :class='diffSource === ph.versionId ? `pink white--text` : ($vuetify.theme.dark ? `grey darken-2` : `grey lighten-2`)'
71
                      :disabled='(ph.versionId >= diffTarget && diffTarget !== 0) || ph.versionId === 0'
NGPixel's avatar
NGPixel committed
72 73 74 75 76 77 78
                      ): strong A
                    v-btn.mr-0.radius-4(
                      @click='setDiffTarget(ph.versionId)'
                      icon
                      small
                      depressed
                      tile
79
                      :class='diffTarget === ph.versionId ? `pink white--text` : ($vuetify.theme.dark ? `grey darken-2` : `grey lighten-2`)'
80
                      :disabled='ph.versionId <= diffSource && ph.versionId !== 0'
NGPixel's avatar
NGPixel committed
81
                      ): strong B
82

83 84 85
            v-btn.ma-0.radius-7(
              v-if='total > trail.length'
              block
86
              color='primary'
87 88 89 90 91 92
              @click='loadMore'
              )
              .caption.white--text Load More...

            v-chip.ma-0(
              v-else
93 94
              label
              small
95 96
              :color='$vuetify.theme.dark ? `grey darken-2` : `grey lighten-2`'
              :class='$vuetify.theme.dark ? `grey--text text--lighten-2` : `grey--text text--darken-2`'
97
              ) End of history trail
98

NGPixel's avatar
NGPixel committed
99
          v-flex(xs12, md8)
100
            v-card.radius-7(:class='$vuetify.breakpoint.mdAndUp ? `mt-8` : ``')
101
              v-card-text
102
                v-card.grey.radius-7(flat, :class='$vuetify.theme.dark ? `darken-2` : `lighten-4`')
NGPixel's avatar
NGPixel committed
103
                  v-row(no-gutters, align='center')
104
                    v-col
NGPixel's avatar
NGPixel committed
105 106 107
                      v-card-text
                        .subheading {{target.title}}
                        .caption {{target.description}}
108 109
                    v-col.text-right.py-3(cols='2', v-if='$vuetify.breakpoint.mdAndUp')
                      v-btn.mr-3(:color='$vuetify.theme.dark ? `white` : `grey darken-3`', small, dark, outlined, @click='toggleViewMode')
NGPixel's avatar
NGPixel committed
110 111 112
                        v-icon(left) mdi-eye
                        .overline View Mode
                v-card.mt-3(light, v-html='diffHTML', flat)
113

NGPixel's avatar
NGPixel committed
114 115 116 117 118 119 120 121 122 123 124
    v-dialog(v-model='isRestoreConfirmDialogShown', max-width='650', persistent)
      v-card
        .dialog-header.is-orange {{$t('history:restore.confirmTitle')}}
        v-card-text.pa-4
          i18next(tag='span', path='history:restore.confirmText')
            strong(place='date') {{ restoreTarget.versionDate | moment('LLL') }}
        v-card-actions
          v-spacer
          v-btn(text, @click='isRestoreConfirmDialogShown = false', :disabled='restoreLoading') {{$t('common:actions.cancel')}}
          v-btn(color='orange darken-2', dark, @click='restoreConfirm', :loading='restoreLoading') {{$t('history:restore.confirmButton')}}

125 126
    page-selector(mode='create', v-model='branchOffOpts.modal', :open-handler='branchOffHandle', :path='branchOffOpts.path', :locale='branchOffOpts.locale')

127
    nav-footer
128
    notify
129
    search-results
130 131 132
</template>

<script>
NGPixel's avatar
NGPixel committed
133
import * as Diff2Html from 'diff2html'
134 135
import { createPatch } from 'diff'
import _ from 'lodash'
NGPixel's avatar
NGPixel committed
136
import gql from 'graphql-tag'
137

138
export default {
NGPixel's avatar
NGPixel committed
139
  i18nOptions: { namespaces: 'history' },
140
  props: {
141
    pageId: {
142 143 144 145 146 147 148 149 150 151
      type: Number,
      default: 0
    },
    locale: {
      type: String,
      default: 'en'
    },
    path: {
      type: String,
      default: 'home'
152
    },
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    title: {
      type: String,
      default: 'Untitled Page'
    },
    description: {
      type: String,
      default: ''
    },
    createdAt: {
      type: String,
      default: ''
    },
    updatedAt: {
      type: String,
      default: ''
    },
    tags: {
      type: Array,
      default: () => ([])
    },
    authorName: {
      type: String,
      default: 'Unknown'
    },
    authorId: {
      type: Number,
      default: 0
    },
    isPublished: {
      type: Boolean,
      default: false
    },
185 186 187
    liveContent: {
      type: String,
      default: ''
188 189
    }
  },
NGPixel's avatar
NGPixel committed
190
  data () {
191
    return {
NGPixel's avatar
NGPixel committed
192
      source: {
NGPixel's avatar
NGPixel committed
193
        versionId: 0,
NGPixel's avatar
NGPixel committed
194 195 196 197 198
        content: '',
        title: '',
        description: ''
      },
      target: {
NGPixel's avatar
NGPixel committed
199
        versionId: 0,
NGPixel's avatar
NGPixel committed
200 201 202 203
        content: '',
        title: '',
        description: ''
      },
204 205 206
      trail: [],
      diffSource: 0,
      diffTarget: 0,
207
      offsetPage: 0,
NGPixel's avatar
NGPixel committed
208
      total: 0,
NGPixel's avatar
NGPixel committed
209
      viewMode: 'line-by-line',
NGPixel's avatar
NGPixel committed
210 211 212 213 214
      cache: [],
      restoreTarget: {
        versionId: 0,
        versionDate: ''
      },
215 216 217 218 219 220
      branchOffOpts: {
        versionId: 0,
        locale: 'en',
        path: 'new-page',
        modal: false
      },
NGPixel's avatar
NGPixel committed
221 222
      isRestoreConfirmDialogShown: false,
      restoreLoading: false
223
    }
224 225
  },
  computed: {
226
    fullTrail () {
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
      const liveTrailItem = {
        versionId: 0,
        authorId: this.authorId,
        authorName: this.authorName,
        actionType: 'live',
        valueBefore: null,
        valueAfter: null,
        versionDate: this.updatedAt
      }
      // -> Check for move between latest and live
      const prevPage = _.find(this.cache, ['versionId', _.get(this.trail, '[0].versionId', -1)])
      if (prevPage && this.path !== prevPage.path) {
        liveTrailItem.actionType = 'move'
        liveTrailItem.valueBefore = prevPage.path
        liveTrailItem.valueAfter = this.path
      }
      // -> Combine trail with live
244
      return [
245
        liveTrailItem,
246 247 248
        ...this.trail
      ]
    },
NGPixel's avatar
NGPixel committed
249
    diffs () {
NGPixel's avatar
NGPixel committed
250
      return createPatch(`/${this.path}`, this.source.content, this.target.content)
251
    },
NGPixel's avatar
NGPixel committed
252
    diffHTML () {
NGPixel's avatar
NGPixel committed
253
      return Diff2Html.html(this.diffs, {
254
        inputFormat: 'diff',
NGPixel's avatar
NGPixel committed
255
        drawFileList: false,
256
        matching: 'lines',
NGPixel's avatar
NGPixel committed
257
        outputFormat: this.viewMode
258 259 260 261
      })
    }
  },
  watch: {
NGPixel's avatar
NGPixel committed
262
    trail (newValue, oldValue) {
263
      if (newValue && newValue.length > 0) {
264 265
        this.diffTarget = 0
        this.diffSource = _.get(_.head(newValue), 'versionId', 0)
266
      }
NGPixel's avatar
NGPixel committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
    },
    async diffSource (newValue, oldValue) {
      if (this.diffSource !== this.source.versionId) {
        const page = _.find(this.cache, { versionId: newValue })
        if (page) {
          this.source = page
        } else {
          this.source = await this.loadVersion(newValue)
        }
      }
    },
    async diffTarget (newValue, oldValue) {
      if (this.diffTarget !== this.target.versionId) {
        const page = _.find(this.cache, { versionId: newValue })
        if (page) {
          this.target = page
        } else {
          this.target = await this.loadVersion(newValue)
        }
      }
287
    }
288 289 290 291 292
  },
  created () {
    this.$store.commit('page/SET_ID', this.id)
    this.$store.commit('page/SET_LOCALE', this.locale)
    this.$store.commit('page/SET_PATH', this.path)
293 294

    this.$store.commit('page/SET_MODE', 'history')
295

296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
    this.cache.push({
      action: 'live',
      authorId: this.authorId,
      authorName: this.authorName,
      content: this.liveContent,
      contentType: '',
      createdAt: this.createdAt,
      description: this.description,
      editor: '',
      isPrivate: false,
      isPublished: this.isPublished,
      locale: this.locale,
      pageId: this.pageId,
      path: this.path,
      publishEndDate: '',
      publishStartDate: '',
      tags: this.tags,
      title: this.title,
      versionId: 0,
      versionDate: this.updatedAt
    })

    this.target = this.cache[0]
319 320
  },
  methods: {
NGPixel's avatar
NGPixel committed
321 322 323 324 325 326 327 328 329 330 331 332 333
    async loadVersion (versionId) {
      this.$store.commit(`loadingStart`, 'history-version-' + versionId)
      const resp = await this.$apollo.query({
        query: gql`
          query ($pageId: Int!, $versionId: Int!) {
            pages {
              version (pageId: $pageId, versionId: $versionId) {
                action
                authorId
                authorName
                content
                contentType
                createdAt
334
                versionDate
NGPixel's avatar
NGPixel committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
                description
                editor
                isPrivate
                isPublished
                locale
                pageId
                path
                publishEndDate
                publishStartDate
                tags
                title
                versionId
              }
            }
          }
        `,
        variables: {
          versionId,
          pageId: this.pageId
        }
      })
      this.$store.commit(`loadingStop`, 'history-version-' + versionId)
      const page = _.get(resp, 'data.pages.version', null)
      if (page) {
        this.cache.push(page)
        return page
      } else {
        return { content: '' }
      }
364 365 366 367 368 369 370
    },
    viewSource (versionId) {
      window.location.assign(`/s/${this.locale}/${this.path}?v=${versionId}`)
    },
    download (versionId) {
      window.location.assign(`/d/${this.locale}/${this.path}?v=${versionId}`)
    },
NGPixel's avatar
NGPixel committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    restore (versionId, versionDate) {
      this.restoreTarget = {
        versionId,
        versionDate
      }
      this.isRestoreConfirmDialogShown = true
    },
    async restoreConfirm () {
      this.restoreLoading = true
      this.$store.commit(`loadingStart`, 'history-restore')
      try {
        const resp = await this.$apollo.mutate({
          mutation: gql`
            mutation ($pageId: Int!, $versionId: Int!) {
              pages {
                restore (pageId: $pageId, versionId: $versionId) {
                  responseResult {
                    succeeded
                    errorCode
                    slug
                    message
                  }
                }
              }
            }
          `,
          variables: {
            versionId: this.restoreTarget.versionId,
            pageId: this.pageId
          }
        })
        if (_.get(resp, 'data.pages.restore.responseResult.succeeded', false) === true) {
          this.$store.commit('showNotification', {
            style: 'success',
            message: this.$t('history:restore.success'),
            icon: 'check'
          })
          this.isRestoreConfirmDialogShown = false
          setTimeout(() => {
            window.location.assign(`/${this.locale}/${this.path}`)
          }, 1000)
        } else {
          throw new Error(_.get(resp, 'data.pages.restore.responseResult.message', 'An unexpected error occured'))
        }
      } catch (err) {
        this.$store.commit('showNotification', {
          style: 'red',
          message: err.message,
          icon: 'alert'
        })
      }
      this.$store.commit(`loadingStop`, 'history-restore')
      this.restoreLoading = false
424 425
    },
    branchOff (versionId) {
426 427 428 429 430 431 432 433 434 435
      const pathParts = this.path.split('/')
      this.branchOffOpts = {
        versionId: versionId,
        locale: this.locale,
        path: (pathParts.length > 1) ? _.initial(pathParts).join('/') + `/new-page` : `new-page`,
        modal: true
      }
    },
    branchOffHandle ({ locale, path }) {
      window.location.assign(`/e/${locale}/${path}?from=${this.pageId},${this.branchOffOpts.versionId}`)
NGPixel's avatar
NGPixel committed
436
    },
NGPixel's avatar
NGPixel committed
437 438 439
    toggleViewMode () {
      this.viewMode = (this.viewMode === 'line-by-line') ? 'side-by-side' : 'line-by-line'
    },
NGPixel's avatar
NGPixel committed
440
    goLive () {
441
      window.location.assign(`/${this.path}`)
442
    },
NGPixel's avatar
NGPixel committed
443
    setDiffSource (versionId) {
444 445
      this.diffSource = versionId
    },
NGPixel's avatar
NGPixel committed
446
    setDiffTarget (versionId) {
447 448
      this.diffTarget = versionId
    },
NGPixel's avatar
NGPixel committed
449
    loadMore () {
450 451 452 453 454
      this.offsetPage++
      this.$apollo.queries.trail.fetchMore({
        variables: {
          id: this.pageId,
          offsetPage: this.offsetPage,
455
          offsetSize: this.$vuetify.breakpoint.mdAndUp ? 25 : 5
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
        },
        updateQuery: (previousResult, { fetchMoreResult }) => {
          return {
            pages: {
              history: {
                total: previousResult.pages.history.total,
                trail: [...previousResult.pages.history.trail, ...fetchMoreResult.pages.history.trail],
                __typename: previousResult.pages.history.__typename
              },
              __typename: previousResult.pages.__typename
            }
          }
        }
      })
    },
NGPixel's avatar
NGPixel committed
471
    trailColor (actionType) {
472 473 474 475 476 477 478
      switch (actionType) {
        case 'edit':
          return 'primary'
        case 'move':
          return 'purple'
        case 'initial':
          return 'teal'
479 480
        case 'live':
          return 'orange'
481 482 483 484
        default:
          return 'grey'
      }
    },
NGPixel's avatar
NGPixel committed
485
    trailIcon (actionType) {
486 487
      switch (actionType) {
        case 'edit':
NGPixel's avatar
NGPixel committed
488
          return '' // 'mdi-pencil'
489
        case 'move':
490
          return 'mdi-forward'
491
        case 'initial':
Nick's avatar
Nick committed
492
          return 'mdi-plus'
493 494
        case 'live':
          return 'mdi-atom-variant'
495
        default:
496
          return 'mdi-alert'
497 498
      }
    },
NGPixel's avatar
NGPixel committed
499
    trailBgColor (actionType) {
500 501
      switch (actionType) {
        case 'move':
502
          return this.$vuetify.theme.dark ? 'purple' : 'purple lighten-5'
503
        case 'initial':
504
          return this.$vuetify.theme.dark ? 'teal darken-3' : 'teal lighten-5'
505
        case 'live':
506
          return this.$vuetify.theme.dark ? 'orange darken-3' : 'orange lighten-5'
507
        default:
508
          return this.$vuetify.theme.dark ? 'grey darken-3' : 'grey lighten-4'
509 510 511 512 513
      }
    }
  },
  apollo: {
    trail: {
NGPixel's avatar
NGPixel committed
514 515 516 517 518 519 520 521 522 523 524
      query: gql`
        query($id: Int!, $offsetPage: Int, $offsetSize: Int) {
          pages {
            history(id:$id, offsetPage:$offsetPage, offsetSize:$offsetSize) {
              trail {
                versionId
                authorId
                authorName
                actionType
                valueBefore
                valueAfter
525
                versionDate
NGPixel's avatar
NGPixel committed
526 527 528 529 530 531 532
              }
              total
            }
          }
        }
      `,
      variables () {
533 534
        return {
          id: this.pageId,
535
          offsetPage: 0,
536
          offsetSize: this.$vuetify.breakpoint.mdAndUp ? 25 : 5
537 538
        }
      },
539
      manual: true,
NGPixel's avatar
NGPixel committed
540
      result ({ data, loading, networkStatus }) {
541 542 543
        this.total = data.pages.history.total
        this.trail = data.pages.history.trail
      },
544 545 546
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'history-trail-refresh')
      }
547 548 549 550 551 552 553
    }
  }
}
</script>

<style lang='scss'>

554 555 556 557
.history {
  &-promptmenu {
    border-top: 5px solid mc('blue', '700');
  }
NGPixel's avatar
NGPixel committed
558 559 560 561 562 563 564 565 566

  .d2h-file-wrapper {
    border: 1px solid #EEE;
    border-left: none;
  }

  .d2h-file-header {
    display: none;
  }
567 568
}

569
</style>