This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
vue-element.js
60 lines (57 loc) · 1.69 KB
/
vue-element.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
54
55
56
57
58
59
60
(function () {
function install (Vue) {
Vue.element = function (tag, options) {
var p = Object.create(HTMLElement.prototype)
// Handle attached/detached callbacks
p.attachedCallback = options.attached
p.detachedCallback = options.detached
// disable Vue's own attached/detached detection
// so we don't fire the callback twice
options.attached = null
options.detached = null
// Handle param attributes
var props = options.props || options.paramAttributes || []
var propsHash = Object.create(null)
props.forEach(function (name) {
propsHash[name] = true
Object.defineProperty(p, name, {
get: function () {
return this.__vue__[name]
},
set: function (val) {
this.setAttribute(name, val)
}
})
})
p.attributeChangedCallback = function (name, oldVal, newVal) {
if (propsHash[name]) {
this.__vue__[name] = newVal
}
}
// Define the Vue constructor to manage the element
var VM = Vue.extend(options)
p.createdCallback = function () {
var vm = new VM({
el: this
})
if (options.shadow) {
var shadow = this.createShadowRoot()
while (this.firstChild) {
shadow.appendChild(this.firstChild)
}
}
}
// register element
document.registerElement(tag, {
prototype: p
})
}
}
if (typeof exports == "object") {
module.exports = install
} else if (typeof define == "function" && define.amd) {
define([], function(){ return install })
} else if (window.Vue) {
Vue.use(install)
}
})()