├── plugins
├── once.js
└── delegate.js
├── test
└── delegate.htm
├── vine.min.js
├── README.markdown
└── vine.js
/plugins/once.js:
--------------------------------------------------------------------------------
1 | vine.once = function(target, type, handler, data, h){
2 | target = vine.id(target);
3 | vine.bind(target, type, h = function(e){
4 | handler.call(target, e);
5 | vine.unbind(target, h)
6 | }, data);
7 | }
--------------------------------------------------------------------------------
/test/delegate.htm:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | la
6 |
7 |
--------------------------------------------------------------------------------
/vine.min.js:
--------------------------------------------------------------------------------
1 | vine=function(a,b,c,d,e,f,g,h,i){function j(d,e){return e=d[a]=d[a]||b++,c[e]||(c[e]={b:{},e:{}})}function k(a){return a.charAt?g.getElementById(a):a}return h={d:j,id:k,Event:i=function(a,b,c){c=this;for(b in a)c[b]=c[b]||a[b];c.timestamp=+(new Date),c.target||(c.target=c.srcElement)},bind:function(a,b,c,g,i,l,m,n){if((n=(m=b.split(" ")).length)>1)while(n--)h.bind(a,m[n],c);else{a=k(a),i=j(a);if(l=/^(.+)\.([^\.]+)$/.exec(b))b=l[2],l=l[1];(i.e[b]||(i.e[b]=[])).push({n:l,f:c,d:g||{}}),!i.b[b]&&(i.b[b]=1,a[e]?a[e](b,function(c){h.trigger(a,b,c)[d]&&c.preventDefault()},null):a[f]("on"+b,function(){return!h.trigger(a,b,window.event)[d]}))}},trigger:function(a,b,c,e,f,h,l,m,n){a=k(a);if(!c&&a.nodeType){if(!a.fireEvent)return l=g.createEvent((init=/click|mousedown|mouseup|mousemove/.test(b))?"MouseEvents":"HTMLEvents"),l[init?"initMouseEvent":"initEvent"](b,!0,!0,window,0,0,0,0,0,!1,!1,!1,!1,0,null),a.dispatchEvent(l),l;try{return new i({defaultPrevented:a[b==="click"?b:fireEvent]("on"+b)})}catch(o){}}l=new i(c||{});if(e=j(a).e[b]){for(f=0,h=e.length;f 1) {
62 | while (l--) vine.bind(object, arr[l], fn)
63 |
64 | //otherwise...
65 | } else {
66 | //Get the element if object is a string
67 | object = id(object)
68 |
69 | //Get data
70 | dat = _data(object)
71 |
72 | //Check for a namespace, if one is present assign to namespace variable
73 | if (ns = /^(.+)\.([^\.]+)$/.exec(type)) {
74 | type = ns[2]
75 | ns = ns[1]
76 | }
77 |
78 | //Initialize the array of functions, then push the handler and other data to it
79 | (dat.e[type] || (dat.e[type] = [])).push({
80 | n: ns,
81 | f: fn,
82 | d: evt_dat || {}
83 | })
84 |
85 | //Bind if the object is an element
86 | !dat.b[type] && (dat.b[type] = 1, object[addEventListener]
87 | ? object[addEventListener](type, function(e){
88 | vine.trigger(object, type, e)[defaultPrevented] && e.preventDefault()
89 | },null)
90 | : object[attachEvent]("on" + type, function(){
91 | return !vine.trigger(object, type, window.event)[defaultPrevented]
92 | }));
93 | }
94 | },
95 | trigger: function (
96 | object, //Object to trigger event on
97 | type, //Type of event to trigger
98 | evt, //Optional object to mix in to the event passed
99 | handlers, //Placeholder for an array of handlers
100 | x, len, //Placeholders for iteration
101 | event, //Placeholder for genereated event
102 | prev, //Placeholder for determining if default is prevented
103 | handler //Placeholder for specific handler
104 | ) {
105 | object = id(object)
106 |
107 |
108 | if (!evt && object.nodeType) {
109 | if (object.fireEvent) {
110 | try{
111 | return new Event({defaultPrevented:object[type === "click" ? type : fireEvent]("on" + type)})
112 | }catch(e){}
113 | } else {
114 | //make the event, init it, execute it, then return
115 | event = document.createEvent((
116 |
117 | //if it's a mouse event, use mouse event init
118 | init=/click|mousedown|mouseup|mousemove/.test(type)
119 |
120 | ) ? "MouseEvents" : "HTMLEvents")
121 | event[init ? "initMouseEvent" : "initEvent"](type, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
122 | object.dispatchEvent(event)
123 | return event
124 | }
125 | }
126 | event = new Event(evt || {})
127 |
128 | if (handlers = _data(object).e[type]) {
129 | for (x = 0, len = handlers.length; x < len; x++) {
130 | if (handler = handlers[x]) {
131 | event.namespace = handler.n
132 | event.data = handler.d
133 | prev = prev || handler.f.call(object, event) === false
134 | }
135 | }
136 |
137 | event[defaultPrevented] = event[defaultPrevented] || prev
138 | }
139 | return event
140 | },
141 | unbind:function (
142 | object, //Object to detach event from
143 | type, //Type of event to unbind
144 | dat, //Placeholder for data attached to object
145 | x, y, len, a//Placeholders for iteration
146 | ) {
147 | object = id(object)
148 |
149 | //If only an object is given, remove data
150 | if (!type) {
151 | //remove both the id on the object and the object from data object to reduce memory usage
152 | return data[object[expando]] = object[expando] = null
153 | }
154 | dat = _data(object)
155 |
156 | //If type is a string
157 | if (type.charAt) {
158 | //if it is a namespace
159 | if (type.charAt(0) === ".") {
160 | //go through all handlers and test for the namespace
161 | type = type.substring(1)
162 | for (y in dat.e) {
163 | a = dat.e[y]
164 | len = a.length
165 | for (x = 0; x < len;x++) {
166 | a[x].n === type && (a[x] = null)
167 | }
168 | }
169 | //Otherwise just reset the entire type
170 | } else {
171 | dat.e[type] = []
172 | }
173 | //If type is instead a function, remove all instances of that function
174 | } else {
175 | for (y in dat.e) {
176 | a = dat.e[y]
177 | len = a.length
178 | for (x = 0; x < len; x++) {
179 | a[x].f === type && (a[x] = null)
180 | }
181 | }
182 | }
183 | }
184 | }
185 |
186 | //Functions on the event's prototype
187 | Event.prototype = {
188 | defaultPrevented: false,
189 | preventDefault: function () {
190 | this[defaultPrevented] = true
191 | }
192 | }
193 |
194 | return vine
195 |
196 | })(+(new Date), 1, {}, "defaultPrevented", "addEventListener", "attachEvent", document);
--------------------------------------------------------------------------------