vue-ext.js
3.15 KB
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
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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
define('vue-ext', ['vue-plugins', 'vue-directives'], function() {
});
define('vue-plugins', ['vue-loader']);
define('vue-directives', ['direct-proxy', 'direct-class', 'direct-scroll']);
define('vue-loader', ['vue'], function(Vue) {
var loader = {};
loader.install = function(Vue, options) {
Vue.components = function(name) {
if(Vue.component(name)) return;
Vue.component(name, function(resolve, reject) {
require([name], function(component) {
Vue.importCssText(name, component.cssText, component.document);
resolve({
template: component.template,
data: data
});
});
});
};
Vue.importCssText = function( id, cssText, doc) {
doc = doc || document;
if(doc.getElementById( id+'.css' )) return;
var style = doc.createElement('style');
cssText += "\n/*# sourceURL=" + id + " */"; // 添加source URL 便于分清css来源
style.appendChild( doc.createTextNode(cssText) );
style.id = id + '.css';
doc.querySelector('head').appendChild(style);
};
Vue.parseJSON = function(data) {
return JSON.parse( JSON.stringify(data) );
}
};
Vue.use(loader);
});
//example: v-proxy:click="{selector: 'a', fn: show}"
define('direct-proxy', ['jquery', 'vue'], function ($, Vue) {
return Vue.directive('proxy', {
bind: function(el, binding) {
var value = binding.value,
event = binding.arg;
$(el).off(event, value.selector)
.on(event, value.selector, function(e) {
value.fn(e.currentTarget);
});
}
});
});
define('direct-class', ['jquery', 'vue'], function($, Vue) {
return Vue.directive('class', {
bind: function(el, binding) {
var value = binding.value,
event = binding.arg || 'click',
modifiers = binding.modifiers;
$(el).off(event, value.selector)
.on(event, value.selector, function(e) {
var vclass= value.vclass,
fn = value.fn,
method = modifiers.only ? "addClass" : "toggleClass";
$(this)[method](vclass).siblings().removeClass(vclass); // toggle
$.isFunction(fn) && fn(e.currentTarget);
});
}
});
});
define('direct-scroll', ['perfect-scrollbar', 'vue', 'css!ps_css'], function(Ps, Vue) {
return Vue.directive('scroll', {
componentUpdated: function(el, binding) {
var settings = binding.value || {};
$(el).css({
'height': el.outerHeight
})
.perfectScrollbar(
$.extend({
wheelSpeed: 1,
wheelPropagation: true,
minScrollbarLength: 10,
suppressScrollX: true
}, settings)
);
if(settings.labelfixed) {
$(el).scroll(function(e) {
var that = $(this),
top = parseInt(that.offset().top);
that.find('section').each(function(index, section) {
var sTop, label;
section = $(section);
sTop = parseInt(section.offset().top);
label = section.find('label');
label.toggleClass("fixed", sTop <= top);
});
});
}
},
update: function(el, binding) {
setTimeout(function() {
// 需要等待dom更新操作完成后执行
$(el).perfectScrollbar('update');
}, 0);
}
});
});