-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrantable.js
53 lines (40 loc) · 1.23 KB
/
grantable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 权限控制
// 依赖:jQuery
;(function ($, window, document, undefined) {
"use strict";
// singleton system object
qui = window.qui || {};
var Grantable = function (element, options) {
var self = this
this.$element = $(element)
this.options = (options || '').split('|')
this.toggle()
}
Grantable.prototype.checkAvailable = function(){
var user = JSON.parse(window.sessionStorage.user || window.localStorage.user)
if(!user || !user.role) return false
if(user.role.id === '1') return true
if(!user.role.permissions) return false
var exist = _.intersection(user.role.permissions, this.options)
return exist.length > 0
}
Grantable.prototype.toggle = function(){
var visable = this.checkAvailable()
this.$element.toggle(visable)
}
Grantable.VERSION = '1.0.0'
function Plugin() {
return this.each(function () {
var $this = $(this)
var data = $this.data('q.grantable')
var options = $this.data('grant')
if (!data) $this.data('q.grantable', (data = new Grantable(this, options)))
return $this.data('q.grantable')
})
}
$.fn.grant = Plugin
$.fn.grant.Constructor = Grantable
qui.notifyCenter.on('refresh', function () {
$('[data-grant]').grant()
})
})(jQuery, window, document);