-
Notifications
You must be signed in to change notification settings - Fork 0
/
jot.js
206 lines (169 loc) · 4.98 KB
/
jot.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
206
;(function (root) {
var Jot = {};
if (typeof module !== 'undefined' && module.exports) module.exports = Jot;
else root.Jot = Jot;
var Task = Jot.Task = function (context) {
this.context = context || {};
this.props = this.props || {
i: undefined,
o: undefined,
into: null,
outo: null,
iString: null
};
return this;
};
Task.prototype.i = function (fn) {
if (typeof fn === 'undefined') return this.props.i;
if (typeof fn === 'function') {
this.props.i = fn;
this.props.iString = cleanInput(fn);
} else if (typeof fn === 'string') {
var contextualEval = this.eval;
this.props.iString = fn;
this.props.i = function () {
return contextualEval(fn);
};
}
return this;
};
Task.prototype.iString = function () {
return this.props.iString;
};
Task.prototype.o = function (o) {
if (typeof o === 'undefined') return this.props.o;
this.props.o = o;
return this;
};
Task.prototype.into = function (el) {
if (typeof el === 'undefined') return this.props.into;
this.props.into = el;
return this;
};
Task.prototype.outo = function (el) {
if (typeof el === 'undefined') return this.props.outo;
this.props.outo = el;
return this;
};
Task.prototype.eval = function (str) {
return eval(str);
};
Task.prototype.invade = function (el) {
var container = getElement(el);
if (container) {
var inputEl = document.createElement('code');
var outputEl = document.createElement('code');
var inputPre = document.createElement('pre');
var outputPre = document.createElement('pre');
container.appendChild(inputPre).appendChild(inputEl);
container.appendChild(outputPre).appendChild(outputEl);
this.into(inputEl).outo(outputEl);
}
return this;
};
Task.prototype.render = function () {
var iString = this.iString(),
o = this.o(),
into = this.into(),
outo = this.outo();
if (outo) outo.innerHTML = '';
if (typeof iString !== 'undefined' && into) into.innerHTML = iString;
if (typeof o !== 'undefined' && outo) {
if (o instanceof Element) {
outo.appendChild(o);
} else {
if (o instanceof Error) o = o.toString();
else if (!detectTags(o)) o = JSON.stringify(o);
outo.innerHTML = o;
}
}
return this;
};
Task.prototype.run = function (newFn) {
if (newFn) this.i(newFn);
var i = this.i();
if (i) {
var out = tryCatch(i, this.context);
if (out && (out instanceof Promise || out.then)) {
return out.then(this.o.bind(this))
.then(this.render.bind(this));
}
this.o(out);
}
this.render();
return this;
};
Task.prototype.reset = function () {
this.props.o = undefined;
return this;
};
var TaskSet = Jot.TaskSet = function () {
this.context = {};
this.tasks = [];
return this;
};
TaskSet.prototype.addTask = function (task) {
Jot.Task.call(task, this.context);
this.tasks.push(task);
return this;
};
TaskSet.prototype.createTask = function (inputEl, outputEl) {
var task = new Task(this.context);
if (!outputEl) task.invade(inputEl);
else task.into(inputEl).outo(outputEl);
this.tasks.push(task);
return task;
};
TaskSet.prototype.render = function () {
this.tasks.forEach(function (task) { task.render(); });
return this;
};
TaskSet.prototype.run = function () {
this.tasks.forEach(function (task) { task.run(); });
return this;
};
TaskSet.prototype.reset = function () {
this.tasks.forEach(function (task) { task.reset(); });
return this;
};
function getElement (query) {
if (query instanceof Element) return query;
if (query instanceof Array) return query[0];
if (typeof query === 'string') return document.querySelector(query);
}
function detectTags (string) {
var hasTags = false;
if (typeof string === 'string')
hasTags = escapeTags(string).length !== string.length;
return hasTags;
}
function escapeTags (string) {
string = string.replace(/</g, '<');
string = string.replace(/>/g, '>');
return string;
}
function cleanInput (input) {
var start = /^function[A-z\s]*\([\s]*\)[\s]*\{\n*/, // Matches the beginning of a function "function xyz () {".
end = /[\n\s]*\}$/, // Matches the end of a function " }".
indent = /^[\s]+/, // Matches the first indentation.
cleaned = escapeTags(input.toString());
cleaned = cleaned.replace(start, '');
cleaned = cleaned.replace(end, '');
var indents = cleaned.match(indent);
if (indents) {
// If we find an indent, clean it from each line.
var baseIndent = new RegExp('^' + indents[0], 'gm');
cleaned = cleaned.replace(baseIndent, '');
}
return cleaned;
}
function tryCatch (fn, ctx) {
try {
var r = fn.call(ctx);
return r;
} catch (e) {
return e;
}
}
return;
})(this);