all.js 2.04 KB
Newer Older
1 2 3
'use strict'

import $ from 'jquery'
NGPixel's avatar
NGPixel committed
4 5
import Vue from 'vue'
import _ from 'lodash'
6

7 8
const rootUrl = '/'

9 10
module.exports = (alerts, socket) => {
  if ($('#page-type-all').length) {
NGPixel's avatar
NGPixel committed
11 12 13 14 15 16 17 18 19 20 21 22
    let vueAllPages = new Vue({ // eslint-disable-line no-unused-vars
      el: '#page-type-all',
      data: {
        tree: []
      },
      methods: {
        fetch: function (basePath) {
          let self = this
          $('#notifload').addClass('active')
          Vue.nextTick(() => {
            socket.emit('treeFetch', { basePath }, (data) => {
              if (self.tree.length > 0) {
23 24 25
                let branch = _.last(self.tree)
                branch.hasChildren = true
                _.find(branch.pages, { _id: basePath }).isActive = true
NGPixel's avatar
NGPixel committed
26 27 28 29 30 31 32 33
              }
              self.tree.push({
                hasChildren: false,
                pages: data
              })
              $('#notifload').removeClass('active')
            })
          })
34 35 36
        },
        goto: function (entryPath) {
          window.location.assign(rootUrl + entryPath)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        },
        unfold: function (entryPath) {
          let self = this
          let lastIndex = 0
          _.forEach(self.tree, branch => {
            lastIndex++
            if (_.find(branch.pages, { _id: entryPath }) !== undefined) {
              return false
            }
          })
          self.tree = _.slice(self.tree, 0, lastIndex)
          let branch = _.last(self.tree)
          branch.hasChildren = false
          branch.pages.forEach(page => {
            page.isActive = false
          })
        },
        mainAction: function (page) {
          let self = this
          if (page.isActive) {
            self.unfold(page._id)
          } else if (page.isDirectory) {
            self.fetch(page._id)
          } else {
            self.goto(page._id)
          }
NGPixel's avatar
NGPixel committed
63 64 65
        }
      },
      mounted: function () {
66 67 68 69 70
        let basePath = window.location.pathname.slice(0, -4)
        if (basePath.length > 1) {
          basePath = basePath.slice(1)
        }
        this.fetch(basePath)
NGPixel's avatar
NGPixel committed
71 72
      }
    })
73 74
  }
}