admin.js 4.35 KB
Newer Older
1
'use strict'
2

3
/* global usrData, usrDataName */
4

5 6 7
import $ from 'jquery'
import _ from 'lodash'
import Vue from 'vue'
8

9 10 11 12 13 14 15 16
module.exports = (alerts) => {
  if ($('#page-type-admin-profile').length) {
    let vueProfile = new Vue({
      el: '#page-type-admin-profile',
      data: {
        password: '********',
        passwordVerify: '********',
        name: ''
17
      },
18 19 20 21 22 23 24 25 26 27 28 29 30 31
      methods: {
        saveUser: (ev) => {
          if (vueProfile.password !== vueProfile.passwordVerify) {
            alerts.pushError('Error', "Passwords don't match!")
            return
          }
          $.post(window.location.href, {
            password: vueProfile.password,
            name: vueProfile.name
          }).done((resp) => {
            alerts.pushSuccess('Saved successfully', 'Changes have been applied.')
          }).fail((jqXHR, txtStatus, resp) => {
            alerts.pushError('Error', resp)
          })
32
        }
33
      },
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
      created: function () {
        this.name = usrDataName
      }
    })
  } else if ($('#page-type-admin-users').length) {
    require('../modals/admin-users-create.js')(alerts)
  } else if ($('#page-type-admin-users-edit').length) {
    let vueEditUser = new Vue({
      el: '#page-type-admin-users-edit',
      data: {
        id: '',
        email: '',
        password: '********',
        name: '',
        rights: [],
        roleoverride: 'none'
50
      },
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      methods: {
        addRightsRow: (ev) => {
          vueEditUser.rights.push({
            role: 'write',
            path: '/',
            exact: false,
            deny: false
          })
        },
        removeRightsRow: (idx) => {
          _.pullAt(vueEditUser.rights, idx)
          vueEditUser.$forceUpdate()
        },
        saveUser: (ev) => {
          let formattedRights = _.cloneDeep(vueEditUser.rights)
          switch (vueEditUser.roleoverride) {
            case 'admin':
              formattedRights.push({
                role: 'admin',
                path: '/',
                exact: false,
                deny: false
              })
              break
          }
          $.post(window.location.href, {
            password: vueEditUser.password,
            name: vueEditUser.name,
            rights: JSON.stringify(formattedRights)
          }).done((resp) => {
            alerts.pushSuccess('Saved successfully', 'Changes have been applied.')
          }).fail((jqXHR, txtStatus, resp) => {
            alerts.pushError('Error', resp)
          })
        }
86
      },
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      created: function () {
        this.id = usrData._id
        this.email = usrData.email
        this.name = usrData.name

        if (_.find(usrData.rights, { role: 'admin' })) {
          this.rights = _.reject(usrData.rights, ['role', 'admin'])
          this.roleoverride = 'admin'
        } else {
          this.rights = usrData.rights
        }
      }
    })
    require('../modals/admin-users-delete.js')(alerts)
  } else if ($('#page-type-admin-settings').length) {
    let vueSettings = new Vue({ // eslint-disable-line no-unused-vars
      el: '#page-type-admin-settings',
      data: {
        upgradeModal: {
          state: false,
          step: 'confirm',
          mode: 'upgrade',
          error: 'Something went wrong.'
        }
111
      },
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
      methods: {
        upgrade: (ev) => {
          vueSettings.upgradeModal.mode = 'upgrade'
          vueSettings.upgradeModal.step = 'confirm'
          vueSettings.upgradeModal.state = true
        },
        reinstall: (ev) => {
          vueSettings.upgradeModal.mode = 're-install'
          vueSettings.upgradeModal.step = 'confirm'
          vueSettings.upgradeModal.state = true
        },
        upgradeCancel: (ev) => {
          vueSettings.upgradeModal.state = false
        },
        upgradeStart: (ev) => {
          vueSettings.upgradeModal.step = 'running'
          $.post('/admin/settings/install', {
            mode: vueSettings.upgradeModal.mode
          }).done((resp) => {
            // todo
          }).fail((jqXHR, txtStatus, resp) => {
            vueSettings.upgradeModal.step = 'error'
            vueSettings.upgradeModal.error = jqXHR.responseText
          })
        },
        flushcache: (ev) => {
          window.alert('Coming soon!')
        },
        resetaccounts: (ev) => {
          window.alert('Coming soon!')
        },
        flushsessions: (ev) => {
          window.alert('Coming soon!')
        }
146
      }
147 148
    })
  }
149
}