history.vue 17.6 KB
Newer Older
1 2 3 4 5
<template lang='pug'>
  v-app(:dark='darkMode').history
    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='darkMode ? `grey darken-2` : `grey lighten-2`'
              :class='darkMode ? `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='darkMode ? `grey darken-2` : `grey lighten-2`'
              :class='darkMode ? `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='darkMode ? `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
import { createPatch } from 'diff'
135
import { get } from 'vuex-pathify'
136
import _ from 'lodash'
NGPixel's avatar
NGPixel committed
137
import gql from 'graphql-tag'
138

139
export default {
NGPixel's avatar
NGPixel committed
140
  i18nOptions: { namespaces: 'history' },
141
  props: {
142
    pageId: {
143 144 145 146 147 148 149 150 151 152
      type: Number,
      default: 0
    },
    locale: {
      type: String,
      default: 'en'
    },
    path: {
      type: String,
      default: 'home'
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 185
    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
    },
186 187 188
    liveContent: {
      type: String,
      default: ''
189 190
    }
  },
NGPixel's avatar
NGPixel committed
191
  data () {
192
    return {
NGPixel's avatar
NGPixel committed
193
      source: {
NGPixel's avatar
NGPixel committed
194
        versionId: 0,
NGPixel's avatar
NGPixel committed
195 196 197 198 199
        content: '',
        title: '',
        description: ''
      },
      target: {
NGPixel's avatar
NGPixel committed
200
        versionId: 0,
NGPixel's avatar
NGPixel committed
201 202 203 204
        content: '',
        title: '',
        description: ''
      },
205 206 207
      trail: [],
      diffSource: 0,
      diffTarget: 0,
208
      offsetPage: 0,
NGPixel's avatar
NGPixel committed
209
      total: 0,
NGPixel's avatar
NGPixel committed
210
      viewMode: 'line-by-line',
NGPixel's avatar
NGPixel committed
211 212 213 214 215
      cache: [],
      restoreTarget: {
        versionId: 0,
        versionDate: ''
      },
216 217 218 219 220 221
      branchOffOpts: {
        versionId: 0,
        locale: 'en',
        path: 'new-page',
        modal: false
      },
NGPixel's avatar
NGPixel committed
222 223
      isRestoreConfirmDialogShown: false,
      restoreLoading: false
224
    }
225 226
  },
  computed: {
227
    darkMode: get('site/dark'),
228 229 230 231 232 233 234 235 236 237 238 239 240 241
    fullTrail () {
      return [
        {
          versionId: 0,
          authorId: this.authorId,
          authorName: this.authorName,
          actionType: 'live',
          valueBefore: null,
          valueAfter: null,
          versionDate: this.updatedAt
        },
        ...this.trail
      ]
    },
NGPixel's avatar
NGPixel committed
242
    diffs () {
NGPixel's avatar
NGPixel committed
243
      return createPatch(`/${this.path}`, this.source.content, this.target.content)
244
    },
NGPixel's avatar
NGPixel committed
245
    diffHTML () {
NGPixel's avatar
NGPixel committed
246
      return Diff2Html.html(this.diffs, {
247
        inputFormat: 'diff',
NGPixel's avatar
NGPixel committed
248
        drawFileList: false,
249
        matching: 'lines',
NGPixel's avatar
NGPixel committed
250
        outputFormat: this.viewMode
251 252 253 254
      })
    }
  },
  watch: {
NGPixel's avatar
NGPixel committed
255
    trail (newValue, oldValue) {
256
      if (newValue && newValue.length > 0) {
257 258
        this.diffTarget = 0
        this.diffSource = _.get(_.head(newValue), 'versionId', 0)
259
      }
NGPixel's avatar
NGPixel committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
    },
    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)
        }
      }
280
    }
281 282 283 284 285
  },
  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)
286 287

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

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    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]
312 313
  },
  methods: {
NGPixel's avatar
NGPixel committed
314 315 316 317 318 319 320 321 322 323 324 325 326
    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
327
                versionDate
NGPixel's avatar
NGPixel committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
                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: '' }
      }
357 358 359 360 361 362 363
    },
    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
364 365 366 367 368 369 370 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
    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
417 418
    },
    branchOff (versionId) {
419 420 421 422 423 424 425 426 427 428
      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
429
    },
NGPixel's avatar
NGPixel committed
430 431 432
    toggleViewMode () {
      this.viewMode = (this.viewMode === 'line-by-line') ? 'side-by-side' : 'line-by-line'
    },
NGPixel's avatar
NGPixel committed
433
    goLive () {
434
      window.location.assign(`/${this.path}`)
435
    },
NGPixel's avatar
NGPixel committed
436
    setDiffSource (versionId) {
437 438
      this.diffSource = versionId
    },
NGPixel's avatar
NGPixel committed
439
    setDiffTarget (versionId) {
440 441
      this.diffTarget = versionId
    },
NGPixel's avatar
NGPixel committed
442
    loadMore () {
443 444 445 446 447
      this.offsetPage++
      this.$apollo.queries.trail.fetchMore({
        variables: {
          id: this.pageId,
          offsetPage: this.offsetPage,
448
          offsetSize: this.$vuetify.breakpoint.mdAndUp ? 25 : 5
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
        },
        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
464
    trailColor (actionType) {
465 466 467 468 469 470 471
      switch (actionType) {
        case 'edit':
          return 'primary'
        case 'move':
          return 'purple'
        case 'initial':
          return 'teal'
472 473
        case 'live':
          return 'orange'
474 475 476 477
        default:
          return 'grey'
      }
    },
NGPixel's avatar
NGPixel committed
478
    trailIcon (actionType) {
479 480
      switch (actionType) {
        case 'edit':
NGPixel's avatar
NGPixel committed
481
          return '' // 'mdi-pencil'
482 483 484
        case 'move':
          return 'forward'
        case 'initial':
Nick's avatar
Nick committed
485
          return 'mdi-plus'
486 487
        case 'live':
          return 'mdi-atom-variant'
488
        default:
489
          return 'mdi-alert'
490 491
      }
    },
NGPixel's avatar
NGPixel committed
492
    trailBgColor (actionType) {
493 494
      switch (actionType) {
        case 'move':
495
          return this.darkMode ? 'purple' : 'purple lighten-5'
496
        case 'initial':
497
          return this.darkMode ? 'teal darken-3' : 'teal lighten-5'
498 499
        case 'live':
          return this.darkMode ? 'orange darken-3' : 'orange lighten-5'
500
        default:
NGPixel's avatar
NGPixel committed
501
          return this.darkMode ? 'grey darken-3' : 'grey lighten-4'
502 503 504 505 506
      }
    }
  },
  apollo: {
    trail: {
NGPixel's avatar
NGPixel committed
507 508 509 510 511 512 513 514 515 516 517
      query: gql`
        query($id: Int!, $offsetPage: Int, $offsetSize: Int) {
          pages {
            history(id:$id, offsetPage:$offsetPage, offsetSize:$offsetSize) {
              trail {
                versionId
                authorId
                authorName
                actionType
                valueBefore
                valueAfter
518
                versionDate
NGPixel's avatar
NGPixel committed
519 520 521 522 523 524 525
              }
              total
            }
          }
        }
      `,
      variables () {
526 527
        return {
          id: this.pageId,
528
          offsetPage: 0,
529
          offsetSize: this.$vuetify.breakpoint.mdAndUp ? 25 : 5
530 531
        }
      },
532
      manual: true,
NGPixel's avatar
NGPixel committed
533
      result ({ data, loading, networkStatus }) {
534 535 536
        this.total = data.pages.history.total
        this.trail = data.pages.history.trail
      },
537 538 539
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'history-trail-refresh')
      }
540 541 542 543 544 545 546
    }
  }
}
</script>

<style lang='scss'>

547 548 549 550
.history {
  &-promptmenu {
    border-top: 5px solid mc('blue', '700');
  }
NGPixel's avatar
NGPixel committed
551 552 553 554 555 556 557 558 559

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

  .d2h-file-header {
    display: none;
  }
560 561
}

562
</style>