opalla 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,26 @@
1
+ require 'diffDOM.js'
2
+
3
+ module Opalla
4
+ class DiffDom
5
+ def initialize(after_apply: ->{})
6
+ @instance = %x{new diffDOM({
7
+ valueDiffing: false
8
+ })}
9
+ # {
10
+ # postDiffApply: function(info){
11
+ # if('e' in info.diff){
12
+ # #{after_apply.call(Element[`info.node`])}
13
+ # }
14
+ # }
15
+ # })}
16
+ end
17
+
18
+ def diff(a,b)
19
+ %x{#{@instance}.diff(#{a}[0], #{b}[0])}
20
+ end
21
+
22
+ def apply(el, diff)
23
+ %x{#{@instance}.apply(#{el}[0], #{diff})}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ class Element
2
+ def morph(target, callback=nil)
3
+ dd = Opalla::DiffDom.new(after_apply: callback)
4
+ diff = dd.diff(self, target)
5
+ dd.apply(self, diff)
6
+ self
7
+ end
8
+
9
+ def inner_html
10
+ `#{self}[0].innerHTML`
11
+ end
12
+
13
+ def outer_html
14
+ `#{self}[0].outerHTML`
15
+ end
16
+
17
+ end
@@ -0,0 +1,12 @@
1
+ require 'sha1.js'
2
+
3
+ module Opalla
4
+ module HexRandom
5
+ def self.[]
6
+ x = rand.to_s
7
+ y = Time.new.strftime("%S%L")
8
+ z = "#{x}#{y}"
9
+ `sha1.hex(#{z})`
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,50 @@
1
+ module Opalla
2
+ class Model
3
+ def initialize
4
+ @model_id = HexRandom[]
5
+ end
6
+
7
+ def bind(*attributes, callback)
8
+ @callbacks ||= []
9
+ @callbacks << callback
10
+ attributes.each do |attribute|
11
+ define_singleton_method :"#{attribute}=" do |value|
12
+ instance_variable_set :"@#{attribute}", value
13
+ trigger_callbacks
14
+ end
15
+ end
16
+ end
17
+
18
+ def model_id
19
+ @model_id
20
+ end
21
+
22
+ def trigger_callbacks
23
+ @callbacks.each(&:call)
24
+ end
25
+
26
+ def data
27
+ get_class_data.reduce({'model-id' => model_id}) do |memo, i|
28
+ memo.merge({ i.to_s.dasherize => send(i) })
29
+ end
30
+ end
31
+
32
+ def find(arg=nil)
33
+ return self
34
+ end
35
+
36
+ protected
37
+
38
+ def get_class_data
39
+ self.class.instance_variable_get(:"@data_attrs") || {}
40
+ end
41
+
42
+ def self.data(*data_attrs)
43
+ @data_attrs = data_attrs
44
+ attrs = data_attrs.dup
45
+ attrs.delete(:model_id)
46
+ attr_reader *attrs
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,21 @@
1
+ require 'opal'
2
+ require 'opal/full'
3
+ require 'opal_ujs'
4
+ require 'opal-jquery'
5
+ require 'opal-haml'
6
+ require 'active_support'
7
+
8
+ require 'native'
9
+ require 'js'
10
+ require 'browser'
11
+ require 'browser/console'
12
+ require 'element'
13
+ require 'diff_dom'
14
+ require 'hex_random'
15
+
16
+ require 'view_helper'
17
+ require 'model'
18
+ require 'collection'
19
+ require 'component'
20
+ require 'controller'
21
+ require 'router'
@@ -0,0 +1,66 @@
1
+ module Opalla
2
+ class Router
3
+ class << self
4
+ def start
5
+ create_route_helpers
6
+ current_path = $$.location.pathname
7
+ routes.each do |name, r|
8
+ if r[:path] == current_path
9
+ navigate_to name
10
+ break
11
+ end
12
+ end
13
+ end
14
+
15
+ def navigate_to(route_key, *params)
16
+ route = routes[route_key.to_s]
17
+ controller_name, action = route[:reqs].split('#')
18
+ controller = set_controller(controller_name)
19
+ url = set_url(route[:path])
20
+ history_push(url)
21
+ controller.new action, params
22
+ end
23
+
24
+ def create_route_helpers
25
+ a = ApplicationComponent
26
+ routes.each do |name, r|
27
+ unless name.blank?
28
+ args = extract_args(r[:path])
29
+ a.define_method "#{name}_path" do
30
+ Router.solve_route(r[:path], *args)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def extract_args(raw_path)
37
+ []
38
+ end
39
+
40
+ def solve_route(raw_path, *args)
41
+ # raw_path.gsub %r{:(.*)/}
42
+ raw_path
43
+ end
44
+
45
+ def url_for(path)
46
+ [window.location.origin, path].join('/')
47
+ end
48
+
49
+ def set_controller(name)
50
+ Object::const_get("#{name.camelize}Controller")
51
+ end
52
+
53
+ def set_url(path)
54
+ end
55
+
56
+ def history_push(url)
57
+ end
58
+
59
+ protected
60
+
61
+ def routes
62
+ Marshal.load($$.opalla_data)[:routes]
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,366 @@
1
+ /*
2
+ * [js-sha1]{@link https://github.com/emn178/js-sha1}
3
+ *
4
+ * @version 0.4.1
5
+ * @author Chen, Yi-Cyuan [emn178@gmail.com]
6
+ * @copyright Chen, Yi-Cyuan 2014-2016
7
+ * @license MIT
8
+ */
9
+ /*jslint bitwise: true */
10
+ (function() {
11
+ 'use strict';
12
+
13
+ var root = typeof window === 'object' ? window : {};
14
+ var NODE_JS = !root.JS_SHA1_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
15
+ if (NODE_JS) {
16
+ root = global;
17
+ }
18
+ var COMMON_JS = !root.JS_SHA1_NO_COMMON_JS && typeof module === 'object' && module.exports;
19
+ var AMD = typeof define === 'function' && define.amd;
20
+ var HEX_CHARS = '0123456789abcdef'.split('');
21
+ var EXTRA = [-2147483648, 8388608, 32768, 128];
22
+ var SHIFT = [24, 16, 8, 0];
23
+ var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer'];
24
+
25
+ var blocks = [];
26
+
27
+ var createOutputMethod = function (outputType) {
28
+ return function (message) {
29
+ return new Sha1(true).update(message)[outputType]();
30
+ };
31
+ };
32
+
33
+ var createMethod = function () {
34
+ var method = createOutputMethod('hex');
35
+ if (NODE_JS) {
36
+ method = nodeWrap(method);
37
+ }
38
+ method.create = function () {
39
+ return new Sha1();
40
+ };
41
+ method.update = function (message) {
42
+ return method.create().update(message);
43
+ };
44
+ for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
45
+ var type = OUTPUT_TYPES[i];
46
+ method[type] = createOutputMethod(type);
47
+ }
48
+ return method;
49
+ };
50
+
51
+ var nodeWrap = function (method) {
52
+ var crypto = require('crypto');
53
+ var Buffer = require('buffer').Buffer;
54
+ var nodeMethod = function (message) {
55
+ if (typeof message === 'string') {
56
+ return crypto.createHash('sha1').update(message, 'utf8').digest('hex');
57
+ } else if (message.constructor === ArrayBuffer) {
58
+ message = new Uint8Array(message);
59
+ } else if (message.length === undefined) {
60
+ return method(message);
61
+ }
62
+ return crypto.createHash('sha1').update(new Buffer(message)).digest('hex');
63
+ };
64
+ return nodeMethod;
65
+ };
66
+
67
+ function Sha1(sharedMemory) {
68
+ if (sharedMemory) {
69
+ blocks[0] = blocks[16] = blocks[1] = blocks[2] = blocks[3] =
70
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
71
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
72
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
73
+ this.blocks = blocks;
74
+ } else {
75
+ this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
76
+ }
77
+
78
+ this.h0 = 0x67452301;
79
+ this.h1 = 0xEFCDAB89;
80
+ this.h2 = 0x98BADCFE;
81
+ this.h3 = 0x10325476;
82
+ this.h4 = 0xC3D2E1F0;
83
+
84
+ this.block = this.start = this.bytes = 0;
85
+ this.finalized = this.hashed = false;
86
+ this.first = true;
87
+ }
88
+
89
+ Sha1.prototype.update = function (message) {
90
+ if (this.finalized) {
91
+ return;
92
+ }
93
+ var notString = typeof(message) !== 'string';
94
+ if (notString && message.constructor === root.ArrayBuffer) {
95
+ message = new Uint8Array(message);
96
+ }
97
+ var code, index = 0, i, length = message.length || 0, blocks = this.blocks;
98
+
99
+ while (index < length) {
100
+ if (this.hashed) {
101
+ this.hashed = false;
102
+ blocks[0] = this.block;
103
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
104
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
105
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
106
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
107
+ }
108
+
109
+ if(notString) {
110
+ for (i = this.start; index < length && i < 64; ++index) {
111
+ blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
112
+ }
113
+ } else {
114
+ for (i = this.start; index < length && i < 64; ++index) {
115
+ code = message.charCodeAt(index);
116
+ if (code < 0x80) {
117
+ blocks[i >> 2] |= code << SHIFT[i++ & 3];
118
+ } else if (code < 0x800) {
119
+ blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
120
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
121
+ } else if (code < 0xd800 || code >= 0xe000) {
122
+ blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
123
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
124
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
125
+ } else {
126
+ code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
127
+ blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
128
+ blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
129
+ blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
130
+ blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
131
+ }
132
+ }
133
+ }
134
+
135
+ this.lastByteIndex = i;
136
+ this.bytes += i - this.start;
137
+ if (i >= 64) {
138
+ this.block = blocks[16];
139
+ this.start = i - 64;
140
+ this.hash();
141
+ this.hashed = true;
142
+ } else {
143
+ this.start = i;
144
+ }
145
+ }
146
+ return this;
147
+ };
148
+
149
+ Sha1.prototype.finalize = function () {
150
+ if (this.finalized) {
151
+ return;
152
+ }
153
+ this.finalized = true;
154
+ var blocks = this.blocks, i = this.lastByteIndex;
155
+ blocks[16] = this.block;
156
+ blocks[i >> 2] |= EXTRA[i & 3];
157
+ this.block = blocks[16];
158
+ if (i >= 56) {
159
+ if (!this.hashed) {
160
+ this.hash();
161
+ }
162
+ blocks[0] = this.block;
163
+ blocks[16] = blocks[1] = blocks[2] = blocks[3] =
164
+ blocks[4] = blocks[5] = blocks[6] = blocks[7] =
165
+ blocks[8] = blocks[9] = blocks[10] = blocks[11] =
166
+ blocks[12] = blocks[13] = blocks[14] = blocks[15] = 0;
167
+ }
168
+ blocks[15] = this.bytes << 3;
169
+ this.hash();
170
+ };
171
+
172
+ Sha1.prototype.hash = function () {
173
+ var a = this.h0, b = this.h1, c = this.h2, d = this.h3, e = this.h4;
174
+ var f, j, t, blocks = this.blocks;
175
+
176
+ for(j = 16; j < 80; ++j) {
177
+ t = blocks[j - 3] ^ blocks[j - 8] ^ blocks[j - 14] ^ blocks[j - 16];
178
+ blocks[j] = (t << 1) | (t >>> 31);
179
+ }
180
+
181
+ for(j = 0; j < 20; j += 5) {
182
+ f = (b & c) | ((~b) & d);
183
+ t = (a << 5) | (a >>> 27);
184
+ e = t + f + e + 1518500249 + blocks[j] << 0;
185
+ b = (b << 30) | (b >>> 2);
186
+
187
+ f = (a & b) | ((~a) & c);
188
+ t = (e << 5) | (e >>> 27);
189
+ d = t + f + d + 1518500249 + blocks[j + 1] << 0;
190
+ a = (a << 30) | (a >>> 2);
191
+
192
+ f = (e & a) | ((~e) & b);
193
+ t = (d << 5) | (d >>> 27);
194
+ c = t + f + c + 1518500249 + blocks[j + 2] << 0;
195
+ e = (e << 30) | (e >>> 2);
196
+
197
+ f = (d & e) | ((~d) & a);
198
+ t = (c << 5) | (c >>> 27);
199
+ b = t + f + b + 1518500249 + blocks[j + 3] << 0;
200
+ d = (d << 30) | (d >>> 2);
201
+
202
+ f = (c & d) | ((~c) & e);
203
+ t = (b << 5) | (b >>> 27);
204
+ a = t + f + a + 1518500249 + blocks[j + 4] << 0;
205
+ c = (c << 30) | (c >>> 2);
206
+ }
207
+
208
+ for(; j < 40; j += 5) {
209
+ f = b ^ c ^ d;
210
+ t = (a << 5) | (a >>> 27);
211
+ e = t + f + e + 1859775393 + blocks[j] << 0;
212
+ b = (b << 30) | (b >>> 2);
213
+
214
+ f = a ^ b ^ c;
215
+ t = (e << 5) | (e >>> 27);
216
+ d = t + f + d + 1859775393 + blocks[j + 1] << 0;
217
+ a = (a << 30) | (a >>> 2);
218
+
219
+ f = e ^ a ^ b;
220
+ t = (d << 5) | (d >>> 27);
221
+ c = t + f + c + 1859775393 + blocks[j + 2] << 0;
222
+ e = (e << 30) | (e >>> 2);
223
+
224
+ f = d ^ e ^ a;
225
+ t = (c << 5) | (c >>> 27);
226
+ b = t + f + b + 1859775393 + blocks[j + 3] << 0;
227
+ d = (d << 30) | (d >>> 2);
228
+
229
+ f = c ^ d ^ e;
230
+ t = (b << 5) | (b >>> 27);
231
+ a = t + f + a + 1859775393 + blocks[j + 4] << 0;
232
+ c = (c << 30) | (c >>> 2);
233
+ }
234
+
235
+ for(; j < 60; j += 5) {
236
+ f = (b & c) | (b & d) | (c & d);
237
+ t = (a << 5) | (a >>> 27);
238
+ e = t + f + e - 1894007588 + blocks[j] << 0;
239
+ b = (b << 30) | (b >>> 2);
240
+
241
+ f = (a & b) | (a & c) | (b & c);
242
+ t = (e << 5) | (e >>> 27);
243
+ d = t + f + d - 1894007588 + blocks[j + 1] << 0;
244
+ a = (a << 30) | (a >>> 2);
245
+
246
+ f = (e & a) | (e & b) | (a & b);
247
+ t = (d << 5) | (d >>> 27);
248
+ c = t + f + c - 1894007588 + blocks[j + 2] << 0;
249
+ e = (e << 30) | (e >>> 2);
250
+
251
+ f = (d & e) | (d & a) | (e & a);
252
+ t = (c << 5) | (c >>> 27);
253
+ b = t + f + b - 1894007588 + blocks[j + 3] << 0;
254
+ d = (d << 30) | (d >>> 2);
255
+
256
+ f = (c & d) | (c & e) | (d & e);
257
+ t = (b << 5) | (b >>> 27);
258
+ a = t + f + a - 1894007588 + blocks[j + 4] << 0;
259
+ c = (c << 30) | (c >>> 2);
260
+ }
261
+
262
+ for(; j < 80; j += 5) {
263
+ f = b ^ c ^ d;
264
+ t = (a << 5) | (a >>> 27);
265
+ e = t + f + e - 899497514 + blocks[j] << 0;
266
+ b = (b << 30) | (b >>> 2);
267
+
268
+ f = a ^ b ^ c;
269
+ t = (e << 5) | (e >>> 27);
270
+ d = t + f + d - 899497514 + blocks[j + 1] << 0;
271
+ a = (a << 30) | (a >>> 2);
272
+
273
+ f = e ^ a ^ b;
274
+ t = (d << 5) | (d >>> 27);
275
+ c = t + f + c - 899497514 + blocks[j + 2] << 0;
276
+ e = (e << 30) | (e >>> 2);
277
+
278
+ f = d ^ e ^ a;
279
+ t = (c << 5) | (c >>> 27);
280
+ b = t + f + b - 899497514 + blocks[j + 3] << 0;
281
+ d = (d << 30) | (d >>> 2);
282
+
283
+ f = c ^ d ^ e;
284
+ t = (b << 5) | (b >>> 27);
285
+ a = t + f + a - 899497514 + blocks[j + 4] << 0;
286
+ c = (c << 30) | (c >>> 2);
287
+ }
288
+
289
+ this.h0 = this.h0 + a << 0;
290
+ this.h1 = this.h1 + b << 0;
291
+ this.h2 = this.h2 + c << 0;
292
+ this.h3 = this.h3 + d << 0;
293
+ this.h4 = this.h4 + e << 0;
294
+ };
295
+
296
+ Sha1.prototype.hex = function () {
297
+ this.finalize();
298
+
299
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
300
+
301
+ return HEX_CHARS[(h0 >> 28) & 0x0F] + HEX_CHARS[(h0 >> 24) & 0x0F] +
302
+ HEX_CHARS[(h0 >> 20) & 0x0F] + HEX_CHARS[(h0 >> 16) & 0x0F] +
303
+ HEX_CHARS[(h0 >> 12) & 0x0F] + HEX_CHARS[(h0 >> 8) & 0x0F] +
304
+ HEX_CHARS[(h0 >> 4) & 0x0F] + HEX_CHARS[h0 & 0x0F] +
305
+ HEX_CHARS[(h1 >> 28) & 0x0F] + HEX_CHARS[(h1 >> 24) & 0x0F] +
306
+ HEX_CHARS[(h1 >> 20) & 0x0F] + HEX_CHARS[(h1 >> 16) & 0x0F] +
307
+ HEX_CHARS[(h1 >> 12) & 0x0F] + HEX_CHARS[(h1 >> 8) & 0x0F] +
308
+ HEX_CHARS[(h1 >> 4) & 0x0F] + HEX_CHARS[h1 & 0x0F] +
309
+ HEX_CHARS[(h2 >> 28) & 0x0F] + HEX_CHARS[(h2 >> 24) & 0x0F] +
310
+ HEX_CHARS[(h2 >> 20) & 0x0F] + HEX_CHARS[(h2 >> 16) & 0x0F] +
311
+ HEX_CHARS[(h2 >> 12) & 0x0F] + HEX_CHARS[(h2 >> 8) & 0x0F] +
312
+ HEX_CHARS[(h2 >> 4) & 0x0F] + HEX_CHARS[h2 & 0x0F] +
313
+ HEX_CHARS[(h3 >> 28) & 0x0F] + HEX_CHARS[(h3 >> 24) & 0x0F] +
314
+ HEX_CHARS[(h3 >> 20) & 0x0F] + HEX_CHARS[(h3 >> 16) & 0x0F] +
315
+ HEX_CHARS[(h3 >> 12) & 0x0F] + HEX_CHARS[(h3 >> 8) & 0x0F] +
316
+ HEX_CHARS[(h3 >> 4) & 0x0F] + HEX_CHARS[h3 & 0x0F] +
317
+ HEX_CHARS[(h4 >> 28) & 0x0F] + HEX_CHARS[(h4 >> 24) & 0x0F] +
318
+ HEX_CHARS[(h4 >> 20) & 0x0F] + HEX_CHARS[(h4 >> 16) & 0x0F] +
319
+ HEX_CHARS[(h4 >> 12) & 0x0F] + HEX_CHARS[(h4 >> 8) & 0x0F] +
320
+ HEX_CHARS[(h4 >> 4) & 0x0F] + HEX_CHARS[h4 & 0x0F];
321
+ };
322
+
323
+ Sha1.prototype.toString = Sha1.prototype.hex;
324
+
325
+ Sha1.prototype.digest = function () {
326
+ this.finalize();
327
+
328
+ var h0 = this.h0, h1 = this.h1, h2 = this.h2, h3 = this.h3, h4 = this.h4;
329
+
330
+ return [
331
+ (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, h0 & 0xFF,
332
+ (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, h1 & 0xFF,
333
+ (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, h2 & 0xFF,
334
+ (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, h3 & 0xFF,
335
+ (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, h4 & 0xFF
336
+ ];
337
+ };
338
+
339
+ Sha1.prototype.array = Sha1.prototype.digest;
340
+
341
+ Sha1.prototype.arrayBuffer = function () {
342
+ this.finalize();
343
+
344
+ var buffer = new ArrayBuffer(20);
345
+ var dataView = new DataView(buffer);
346
+ dataView.setUint32(0, this.h0);
347
+ dataView.setUint32(4, this.h1);
348
+ dataView.setUint32(8, this.h2);
349
+ dataView.setUint32(12, this.h3);
350
+ dataView.setUint32(16, this.h4);
351
+ return buffer;
352
+ };
353
+
354
+ var exports = createMethod();
355
+
356
+ if (COMMON_JS) {
357
+ module.exports = exports;
358
+ } else {
359
+ root.sha1 = exports;
360
+ if (AMD) {
361
+ define(function () {
362
+ return exports;
363
+ });
364
+ }
365
+ }
366
+ })();