history.vue 18.3 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 190 191
    },
    effectivePermissions: {
      type: String,
      default: ''
192 193
    }
  },
NGPixel's avatar
NGPixel committed
194
  data () {
195
    return {
NGPixel's avatar
NGPixel committed
196
      source: {
NGPixel's avatar
NGPixel committed
197
        versionId: 0,
NGPixel's avatar
NGPixel committed
198 199 200 201 202
        content: '',
        title: '',
        description: ''
      },
      target: {
NGPixel's avatar
NGPixel committed
203
        versionId: 0,
NGPixel's avatar
NGPixel committed
204 205 206 207
        content: '',
        title: '',
        description: ''
      },
208 209 210
      trail: [],
      diffSource: 0,
      diffTarget: 0,
211
      offsetPage: 0,
NGPixel's avatar
NGPixel committed
212
      total: 0,
NGPixel's avatar
NGPixel committed
213
      viewMode: 'line-by-line',
NGPixel's avatar
NGPixel committed
214 215 216 217 218
      cache: [],
      restoreTarget: {
        versionId: 0,
        versionDate: ''
      },
219 220 221 222 223 224
      branchOffOpts: {
        versionId: 0,
        locale: 'en',
        path: 'new-page',
        modal: false
      },
NGPixel's avatar
NGPixel committed
225 226
      isRestoreConfirmDialogShown: false,
      restoreLoading: false
227
    }
228 229
  },
  computed: {
230
    fullTrail () {
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
      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
248
      return [
249
        liveTrailItem,
250 251 252
        ...this.trail
      ]
    },
NGPixel's avatar
NGPixel committed
253
    diffs () {
NGPixel's avatar
NGPixel committed
254
      return createPatch(`/${this.path}`, this.source.content, this.target.content)
255
    },
NGPixel's avatar
NGPixel committed
256
    diffHTML () {
NGPixel's avatar
NGPixel committed
257
      return Diff2Html.html(this.diffs, {
258
        inputFormat: 'diff',
NGPixel's avatar
NGPixel committed
259
        drawFileList: false,
260
        matching: 'lines',
NGPixel's avatar
NGPixel committed
261
        outputFormat: this.viewMode
262 263 264 265
      })
    }
  },
  watch: {
NGPixel's avatar
NGPixel committed
266
    trail (newValue, oldValue) {
267
      if (newValue && newValue.length > 0) {
268 269
        this.diffTarget = 0
        this.diffSource = _.get(_.head(newValue), 'versionId', 0)
270
      }
NGPixel's avatar
NGPixel committed
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    },
    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)
        }
      }
291
    }
292 293 294 295 296
  },
  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)
297 298

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

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    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]
323 324

    if (this.effectivePermissions) {
NGPixel's avatar
NGPixel committed
325
      this.$store.set('page/effectivePermissions', JSON.parse(Buffer.from(this.effectivePermissions, 'base64').toString()))
326
    }
327 328
  },
  methods: {
NGPixel's avatar
NGPixel committed
329 330 331 332 333 334 335 336 337 338 339 340 341
    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
342
                versionDate
NGPixel's avatar
NGPixel committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
                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: '' }
      }
372 373 374 375 376 377 378
    },
    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
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
    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 {
421
          throw new Error(_.get(resp, 'data.pages.restore.responseResult.message', 'An unexpected error occurred'))
NGPixel's avatar
NGPixel committed
422 423 424 425 426 427 428 429 430 431
        }
      } catch (err) {
        this.$store.commit('showNotification', {
          style: 'red',
          message: err.message,
          icon: 'alert'
        })
      }
      this.$store.commit(`loadingStop`, 'history-restore')
      this.restoreLoading = false
432 433
    },
    branchOff (versionId) {
434 435 436 437 438 439 440 441 442 443
      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
444
    },
NGPixel's avatar
NGPixel committed
445 446 447
    toggleViewMode () {
      this.viewMode = (this.viewMode === 'line-by-line') ? 'side-by-side' : 'line-by-line'
    },
NGPixel's avatar
NGPixel committed
448
    goLive () {
449
      window.location.assign(`/${this.path}`)
450
    },
NGPixel's avatar
NGPixel committed
451
    setDiffSource (versionId) {
452 453
      this.diffSource = versionId
    },
NGPixel's avatar
NGPixel committed
454
    setDiffTarget (versionId) {
455 456
      this.diffTarget = versionId
    },
NGPixel's avatar
NGPixel committed
457
    loadMore () {
458 459 460 461 462
      this.offsetPage++
      this.$apollo.queries.trail.fetchMore({
        variables: {
          id: this.pageId,
          offsetPage: this.offsetPage,
463
          offsetSize: this.$vuetify.breakpoint.mdAndUp ? 25 : 5
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
        },
        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
479
    trailColor (actionType) {
480 481 482 483 484 485 486
      switch (actionType) {
        case 'edit':
          return 'primary'
        case 'move':
          return 'purple'
        case 'initial':
          return 'teal'
487 488
        case 'live':
          return 'orange'
489 490 491 492
        default:
          return 'grey'
      }
    },
NGPixel's avatar
NGPixel committed
493
    trailIcon (actionType) {
494 495
      switch (actionType) {
        case 'edit':
NGPixel's avatar
NGPixel committed
496
          return '' // 'mdi-pencil'
497
        case 'move':
498
          return 'mdi-forward'
499
        case 'initial':
Nick's avatar
Nick committed
500
          return 'mdi-plus'
501 502
        case 'live':
          return 'mdi-atom-variant'
503
        default:
504
          return 'mdi-alert'
505 506
      }
    },
NGPixel's avatar
NGPixel committed
507
    trailBgColor (actionType) {
508 509
      switch (actionType) {
        case 'move':
510
          return this.$vuetify.theme.dark ? 'purple' : 'purple lighten-5'
511
        case 'initial':
512
          return this.$vuetify.theme.dark ? 'teal darken-3' : 'teal lighten-5'
513
        case 'live':
514
          return this.$vuetify.theme.dark ? 'orange darken-3' : 'orange lighten-5'
515
        default:
516
          return this.$vuetify.theme.dark ? 'grey darken-3' : 'grey lighten-4'
517 518 519 520 521
      }
    }
  },
  apollo: {
    trail: {
NGPixel's avatar
NGPixel committed
522 523 524 525 526 527 528 529 530 531 532
      query: gql`
        query($id: Int!, $offsetPage: Int, $offsetSize: Int) {
          pages {
            history(id:$id, offsetPage:$offsetPage, offsetSize:$offsetSize) {
              trail {
                versionId
                authorId
                authorName
                actionType
                valueBefore
                valueAfter
533
                versionDate
NGPixel's avatar
NGPixel committed
534 535 536 537 538 539 540
              }
              total
            }
          }
        }
      `,
      variables () {
541 542
        return {
          id: this.pageId,
543
          offsetPage: 0,
544
          offsetSize: this.$vuetify.breakpoint.mdAndUp ? 25 : 5
545 546
        }
      },
547
      manual: true,
NGPixel's avatar
NGPixel committed
548
      result ({ data, loading, networkStatus }) {
549 550 551
        this.total = data.pages.history.total
        this.trail = data.pages.history.trail
      },
552 553 554
      watchLoading (isLoading) {
        this.$store.commit(`loading${isLoading ? 'Start' : 'Stop'}`, 'history-trail-refresh')
      }
555 556 557 558 559 560 561
    }
  }
}
</script>

<style lang='scss'>

562 563 564 565
.history {
  &-promptmenu {
    border-top: 5px solid mc('blue', '700');
  }
NGPixel's avatar
NGPixel committed
566 567 568 569 570 571 572 573 574

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

  .d2h-file-header {
    display: none;
  }
575 576
}

577
</style>