-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnips.js
205 lines (183 loc) · 5.69 KB
/
Snips.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//when a certain item has dissapeared (say in a destructive rerender) and you want to try to find
// something like it again this will loop through and try to find a singular object which fulfills as many
// similarities as possible
var checkAttributes = ["id", "class", "data-id", "role"];
var refindElement = function(old) {
var list = $("body").focusable(),
index = 0,
oldList = list;
while(index < checkAttributes.length) {
var current = checkAttributes[index++],
escaped = current.replace(/:/g, "\\:"),
oldValue = old.attr(current),
filterSelector = oldValue ? "[" + escaped + "='" + oldValue + "']" : ":not([" + escaped + "])";
if(list.length === 1) {
break;
} else if(!list.length) {
list = oldList;
}
oldList = list;
list = list.filter(filterSelector);
}
if(list.length === 1) list.focus();
};
// basic implementation of finding focusable things
jQuery.fn.focusable = function() {
var $potentials = $(this).find("*[tabindex], a[href], area[href], input:not([disabled], [type=hidden]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed,*[contenteditable]");
return $potentials.filter(function() {
var $this = $(this);
return $this.attr("tabindex") != -1 && (!$this.attr("href") || $this.attr("href") != "#");
});
};
//version of underscore memoize that memoizes constructors correctly
_.memoize = function(func, hasher) {
var memo = {};
hasher || (hasher = _.identity);
var mem = function() {
var key = hasher.apply(this, arguments),
construct = this instanceof mem;
if(_.has(memo, key)) {
return memo[key];
} else {
var result = func.apply(this, arguments);
return memo[key] = (construct ? this : result);
}
};
mem.prototype = func.prototype;
return mem;
};
//execute functions in order define by place no matter in which order they are called
//var print = function(x) {console.log(x)};
//setQueue(1,print,2);
//--> nothing
//setQueue(0,print,4);
//--> 4
//--> 2
var setQueue = function(place, func) {
var current = 0, queue = [];
var helper = function(place, func, args) {
if(place === current) {
current++;
func.apply(this, args);
return true;
}
return false;
};
setQueue = function(place, func) {
var args = _.chain(arguments).toArray().tail(2).value();
if(helper(place, func, args)){
_.each(queue, function(obj){
helper(obj.place, obj.func, obj.args);
}, this);
} else {
queue.push({place: place, func: func, args: args});
}
};
return setQueue.apply(this, arguments);
};
//backbone event sponge to allow later connect attempts to get already produced data
var sponge = function(object){
var sponge = {}, eventSplitter = /\s+/, slice = Array.prototype.slice;
var allFunc = function(eventName){
var name, data;
name = eventName;
data = slice.call(arguments, 1);
sponge[name] = data;
console.log(arguments);
};
object.on("all", allFunc);
var _origOn = object.on;
object.on = _.wrap(_origOn, function(func){
var events, args = slice.call(arguments, 1);
func.apply(object, args);
events = args[0].split(eventSplitter);
while (event = events.shift()) {
if(sponge[event]) {
args[1].apply(args[2] || object, sponge[event]);
}
}
});
return {
destroy: function() {
object.on = _origOn;
sponge = null;
object.off("all", allFunc);
}
}
};
//logging timing stuff
logger = function(description, start) {
var now = performance.now() - (start ? start : 0);
var object = JSON.parse(localStorage.getItem("timings")) || {};
var item = object[description];
if(item) {
var totalTime = item + now;
object[description] = totalTime;
console.log(description + ": " + now);
} else {
object[description] = now;
console.log(description + ": " + now);
}
localStorage.setItem("timings", JSON.stringify(object));
};
parseTimings = function() {
var object = JSON.parse(localStorage.getItem("timings"));
if(object) {
var count = localStorage.getItem("reloadss") * 1;
console.log("Reloads: " + count);
_.each(object, function(property, key){
console.log("Cumulative " + key + ": " + (property / count));
});
}
};
reloader = function(count) {
var item = (localStorage.getItem("reloadss") || 1) * 1;
item++
localStorage.setItem("reloadss", item);
if(item < count) {
location.reload();
} else {
parseTimings();
}
};
clearStuff = function() {
localStorage.setItem("reloadss", 0);
localStorage.setItem("timings", null);
};
// extraction of backbone models attrs feature with change etc, can extend objects
define(function(){
return function(parent, extraName){
extraName = extraName || "Attr";
this.attributes = {};
this._changes = {};
this.changed = {};
this._pending = {};
this._previousAttributes = {};
this._validate = function(){return true};
this.get = _.bind(Backbone.Model.prototype.get, this);
this.escape = _.bind(Backbone.Model.prototype.escape, this);
this.set =_.bind(Backbone.Model.prototype.set, this);
this.previous =_.bind(Backbone.Model.prototype.previous, this);
this.previousAttributes =_.bind(Backbone.Model.prototype.previousAttributes, this);
this.changedAttributes =_.bind(Backbone.Model.prototype.changedAttributes, this);
this.hasChanged =_.bind(Backbone.Model.prototype.hasChanged, this);
_.extend(this, Backbone.Events);
if(parent) {
this.on("all", function(event) {
this.trigger.apply(parent, arguments);
}, this);
}
var reveal = {
"set": this.set,
"get": this.get,
escape: this.escape,
previous: this.previous,
previousAttributes: this.previousAttributes,
changed: this.changedAttributes
};
return _.reduce(reveal, function(memo, fn, name) {
memo[name + extraName] = fn;
return memo;
}, {})
};
});