-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStuk.js
74 lines (74 loc) · 2.46 KB
/
Stuk.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
/**
* @fileOverview simple two way data bind for js.
* @author <a href="https://gitlab.com/Cadub">Carlos Benedetti</a>
* @version 0.1.2
*/
/**
* Creates a two way bind with the elements that have the stuk attribute value equal to a variable name in the object sent as parameter e.g : ` <input stuk="name">` by having the "name" as attribute the value of that input in linked to the variable "name" sent with the object
*
* @example
* ```html
* <input stuk="name">
* <script>
* stuk.bindPage({name:"Benedetti"})
* </script>
* ```
*
*
*
*/
var Stuk;
(function (Stuk) {
var BindingController = /** @class */ (function () {
function BindingController(b) {
this.elementBindings = [];
this.value = b.object[b.property];
Object.defineProperty(b.object, b.property, {
get: this.valueGetter.bind(this),
set: this.valueSetter.bind(this)
});
b.object[b.property] = this.value;
}
BindingController.prototype.valueGetter = function () {
return this.value;
};
BindingController.prototype.valueSetter = function (val) {
this.value = val;
this.elementBindings.forEach(function (binding) {
binding.element[binding.attribute] = val;
});
};
BindingController.prototype.addBinding = function (element, attribute, event) {
var _this = this;
var binding = {
element: element,
attribute: attribute,
event: event ? event : "change"
};
if (event) {
element.addEventListener(event, function (event) {
_this.valueSetter(element[attribute]);
});
}
this.elementBindings.push(binding);
element[attribute] = this.value;
return this;
};
return BindingController;
}());
Stuk.BindingController = BindingController;
function bindPage(page) {
var input = document.querySelectorAll('[stuk]');
input.forEach(function (element) {
var value = element.getAttribute("stuk");
if (value) {
new BindingController({
object: page,
property: value
}).addBinding(element, "value", "keyup");
}
});
}
Stuk.bindPage = bindPage;
})(Stuk || (Stuk = {}));