This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 324
/
preamble.js
148 lines (127 loc) · 3.7 KB
/
preamble.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
/*
backgrid
http://github.com/cloudflare/backgrid
Copyright (c) 2013-present Cloudflare, Inc. and contributors
Licensed under the MIT license.
*/
// Copyright 2009, 2010 Kristopher Michael Kowal
// https://github.com/kriskowal/es5-shim
// ES5 15.5.4.20
// http://es5.github.com/#x15.5.4.20
var ws = "\x09\x0A\x0B\x0C\x0D\x20\xA0\u1680\u180E\u2000\u2001\u2002\u2003" +
"\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028" +
"\u2029\uFEFF";
if (!String.prototype.trim || ws.trim()) {
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
// http://perfectionkills.com/whitespace-deviations/
ws = "[" + ws + "]";
var trimBeginRegexp = new RegExp("^" + ws + ws + "*"),
trimEndRegexp = new RegExp(ws + ws + "*$");
String.prototype.trim = function trim() {
if (this === undefined || this === null) {
throw new TypeError("can't convert " + this + " to object");
}
return String(this)
.replace(trimBeginRegexp, "")
.replace(trimEndRegexp, "");
};
}
function lpad(str, length, padstr) {
var paddingLen = length - (str + '').length;
paddingLen = paddingLen < 0 ? 0 : paddingLen;
var padding = '';
for (var i = 0; i < paddingLen; i++) {
padding = padding + padstr;
}
return padding + str;
}
var $ = Backbone.$;
var Backgrid = {
Extension: {},
resolveNameToClass: function (name, suffix) {
if (_.isString(name)) {
var key = _.map(name.split('-'), function (e) {
return e.slice(0, 1).toUpperCase() + e.slice(1);
}).join('') + suffix;
var klass = Backgrid[key] || Backgrid.Extension[key];
if (_.isUndefined(klass)) {
throw new ReferenceError("Class '" + key + "' not found");
}
return klass;
}
return name;
},
callByNeed: function () {
var value = arguments[0];
if (!_.isFunction(value)) return value;
var context = arguments[1];
var args = [].slice.call(arguments, 2);
return value.apply(context, !!(args + '') ? args : []);
}
};
_.extend(Backgrid, Backbone.Events);
/**
Command translates a DOM Event into commands that Backgrid
recognizes. Interested parties can listen on selected Backgrid events that
come with an instance of this class and act on the commands.
It is also possible to globally rebind the keyboard shortcuts by replacing
the methods in this class' prototype.
@class Backgrid.Command
@constructor
*/
var Command = Backgrid.Command = function (evt) {
_.extend(this, {
altKey: !!evt.altKey,
"char": evt["char"],
charCode: evt.charCode,
ctrlKey: !!evt.ctrlKey,
key: evt.key,
keyCode: evt.keyCode,
locale: evt.locale,
location: evt.location,
metaKey: !!evt.metaKey,
repeat: !!evt.repeat,
shiftKey: !!evt.shiftKey,
which: evt.which
});
};
_.extend(Command.prototype, {
/**
Up Arrow
@member Backgrid.Command
*/
moveUp: function () { return this.keyCode == 38; },
/**
Down Arrow
@member Backgrid.Command
*/
moveDown: function () { return this.keyCode === 40; },
/**
Shift Tab
@member Backgrid.Command
*/
moveLeft: function () { return this.shiftKey && this.keyCode === 9; },
/**
Tab
@member Backgrid.Command
*/
moveRight: function () { return !this.shiftKey && this.keyCode === 9; },
/**
Enter
@member Backgrid.Command
*/
save: function () { return this.keyCode === 13; },
/**
Esc
@member Backgrid.Command
*/
cancel: function () { return this.keyCode === 27; },
/**
None of the above.
@member Backgrid.Command
*/
passThru: function () {
return !(this.moveUp() || this.moveDown() || this.moveLeft() ||
this.moveRight() || this.save() || this.cancel());
}
});