history.vue 18 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
    fullTrail () {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
      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
246
      return [
247
        liveTrailItem,
248 249 250
        ...this.trail
      ]
    },
NGPixel's avatar
NGPixel committed
251
    diffs () {
NGPixel's avatar
NGPixel committed
252
      return createPatch(`/${this.path}`, this.source.content, this.target.content)
253
    },
NGPixel's avatar
NGPixel committed
254
    diffHTML () {
NGPixel's avatar
NGPixel committed
255
      return Diff2Html.html(this.diffs, {
256
        inputFormat: 'diff',
NGPixel's avatar
NGPixel committed
257
        drawFileList: false,
258
        matching: 'lines',
NGPixel's avatar
NGPixel committed
259
        outputFormat: this.viewMode
260 261 262 263
      })
    }
  },
  watch: {
NGPixel's avatar
NGPixel committed
264
    trail (newValue, oldValue) {
265
      if (newValue && newValue.length > 0) {
266 267
        this.diffTarget = 0
        this.diffSource = _.get(_.head(newValue), 'versionId', 0)
268
      }
NGPixel's avatar
NGPixel committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    },
    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)
        }
      }
289
    }
290 291 292 293 294
  },
  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)
295 296

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

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

<style lang='scss'>

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

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

  .d2h-file-header {
    display: none;
  }
569 570
}

571
</style>