uikit-rails 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ uikit
@@ -0,0 +1,6 @@
1
+ module Uikit
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -1,5 +1,5 @@
1
1
  module Uikit
2
2
  module Rails
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -0,0 +1,115 @@
1
+
2
+ /**
3
+ * Expose `Card`.
4
+ */
5
+
6
+ exports.Card = Card;
7
+
8
+ /**
9
+ * Create a new `Card`.
10
+ *
11
+ * @param {Mixed} front
12
+ * @param {Mixed} back
13
+ * @return {Card}
14
+ * @api public
15
+ */
16
+
17
+ exports.card = function(front, back){
18
+ return new Card(front, back);
19
+ };
20
+
21
+ /**
22
+ * Initialize a new `Card` with content
23
+ * for face `front` and `back`.
24
+ *
25
+ * Emits "flip" event.
26
+ *
27
+ * @param {Mixed} front
28
+ * @param {Mixed} back
29
+ * @api public
30
+ */
31
+
32
+ function Card(front, back) {
33
+ ui.Emitter.call(this);
34
+ this._front = front || $('<p>front</p>');
35
+ this._back = back || $('<p>back</p>');
36
+ this.template = html;
37
+ this.render();
38
+ };
39
+
40
+ /**
41
+ * Inherit from `Emitter.prototype`.
42
+ */
43
+
44
+ Card.prototype = new ui.Emitter;
45
+
46
+ /**
47
+ * Set front face `val`.
48
+ *
49
+ * @param {Mixed} val
50
+ * @return {Card}
51
+ * @api public
52
+ */
53
+
54
+ Card.prototype.front = function(val){
55
+ this._front = val;
56
+ this.render();
57
+ return this;
58
+ };
59
+
60
+ /**
61
+ * Set back face `val`.
62
+ *
63
+ * @param {Mixed} val
64
+ * @return {Card}
65
+ * @api public
66
+ */
67
+
68
+ Card.prototype.back = function(val){
69
+ this._back = val;
70
+ this.render();
71
+ return this;
72
+ };
73
+
74
+ /**
75
+ * Flip the card.
76
+ *
77
+ * @return {Card} for chaining
78
+ * @api public
79
+ */
80
+
81
+ Card.prototype.flip = function(){
82
+ this.emit('flip');
83
+ this.el.toggleClass('flipped');
84
+ return this;
85
+ };
86
+
87
+ /**
88
+ * Set the effect to `type`.
89
+ *
90
+ * @param {String} type
91
+ * @return {Dialog} for chaining
92
+ * @api public
93
+ */
94
+
95
+ Card.prototype.effect = function(type){
96
+ this.el.addClass(type);
97
+ return this;
98
+ };
99
+
100
+ /**
101
+ * Render with the given `options`.
102
+ *
103
+ * @param {Object} options
104
+ * @api public
105
+ */
106
+
107
+ Card.prototype.render = function(options){
108
+ var self = this
109
+ , el = this.el = $(this.template);
110
+ el.find('.front').empty().append(this._front.el || $(this._front));
111
+ el.find('.back').empty().append(this._back.el || $(this._back));
112
+ el.click(function(){
113
+ self.flip();
114
+ });
115
+ };
@@ -0,0 +1,351 @@
1
+
2
+ /**
3
+ * Expose `ColorPicker`.
4
+ */
5
+
6
+ exports.ColorPicker = ColorPicker;
7
+
8
+ /**
9
+ * RGB util.
10
+ */
11
+
12
+ function rgb(r,g,b) {
13
+ return 'rgb(' + r + ', ' + g + ', ' + b + ')';
14
+ }
15
+
16
+ /**
17
+ * RGBA util.
18
+ */
19
+
20
+ function rgba(r,g,b,a) {
21
+ return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a + ')';
22
+ }
23
+
24
+ /**
25
+ * Mouse position util.
26
+ */
27
+
28
+ function localPos(e) {
29
+ var offset = $(e.target).offset();
30
+ return {
31
+ x: e.pageX - offset.left
32
+ , y: e.pageY - offset.top
33
+ };
34
+ }
35
+
36
+ /**
37
+ * Initialize a new `ColorPicker`.
38
+ *
39
+ * Emits:
40
+ *
41
+ * - `change` with the given color object
42
+ *
43
+ * @api public
44
+ */
45
+
46
+ function ColorPicker() {
47
+ ui.Emitter.call(this);
48
+ this._colorPos = {};
49
+ this.el = $(html);
50
+ this.main = this.el.find('.main').get(0);
51
+ this.spectrum = this.el.find('.spectrum').get(0);
52
+ $(this.main).bind('selectstart', function(e){ e.preventDefault() });
53
+ $(this.spectrum).bind('selectstart', function(e){ e.preventDefault() });
54
+ this.hue(rgb(255, 0, 0));
55
+ this.spectrumEvents();
56
+ this.mainEvents();
57
+ this.w = 180;
58
+ this.h = 180;
59
+ this.render();
60
+ }
61
+
62
+ /**
63
+ * Inherit from `Emitter.prototype`.
64
+ */
65
+
66
+ ColorPicker.prototype = new ui.Emitter;
67
+
68
+ /**
69
+ * Set width / height to `n`.
70
+ *
71
+ * @param {Number} n
72
+ * @return {ColorPicker} for chaining
73
+ * @api public
74
+ */
75
+
76
+ ColorPicker.prototype.size = function(n){
77
+ return this
78
+ .width(n)
79
+ .height(n);
80
+ };
81
+
82
+ /**
83
+ * Set width to `n`.
84
+ *
85
+ * @param {Number} n
86
+ * @return {ColorPicker} for chaining
87
+ * @api public
88
+ */
89
+
90
+ ColorPicker.prototype.width = function(n){
91
+ this.w = n;
92
+ this.render();
93
+ return this;
94
+ };
95
+
96
+ /**
97
+ * Set height to `n`.
98
+ *
99
+ * @param {Number} n
100
+ * @return {ColorPicker} for chaining
101
+ * @api public
102
+ */
103
+
104
+ ColorPicker.prototype.height = function(n){
105
+ this.h = n;
106
+ this.render();
107
+ return this;
108
+ };
109
+
110
+ /**
111
+ * Spectrum related events.
112
+ *
113
+ * @api private
114
+ */
115
+
116
+ ColorPicker.prototype.spectrumEvents = function(){
117
+ var self = this
118
+ , canvas = $(this.spectrum)
119
+ , down;
120
+
121
+ function update(e) {
122
+ var offsetY = localPos(e).y
123
+ , color = self.hueAt(offsetY - 4);
124
+ self.hue(color.toString());
125
+ self.emit('change', color);
126
+ self._huePos = offsetY;
127
+ self.render();
128
+ }
129
+
130
+ canvas.mousedown(function(e){
131
+ e.preventDefault();
132
+ down = true;
133
+ update(e);
134
+ });
135
+
136
+ canvas.mousemove(function(e){
137
+ if (down) update(e);
138
+ });
139
+
140
+ canvas.mouseup(function(){
141
+ down = false;
142
+ });
143
+ };
144
+
145
+ /**
146
+ * Hue / lightness events.
147
+ *
148
+ * @api private
149
+ */
150
+
151
+ ColorPicker.prototype.mainEvents = function(){
152
+ var self = this
153
+ , canvas = $(this.main)
154
+ , down;
155
+
156
+ function update(e) {
157
+ var color;
158
+ self._colorPos = localPos(e);
159
+ color = self.colorAt(self._colorPos.x, self._colorPos.y);
160
+ self.color(color.toString());
161
+ self.emit('change', color);
162
+
163
+ self.render();
164
+ }
165
+
166
+ canvas.mousedown(function(e){
167
+ down = true;
168
+ update(e);
169
+ });
170
+
171
+ canvas.mousemove(function(e){
172
+ if (down) update(e);
173
+ });
174
+
175
+ canvas.mouseup(function(){
176
+ down = false;
177
+ });
178
+ };
179
+
180
+ /**
181
+ * Get the RGB color at `(x, y)`.
182
+ *
183
+ * @param {Number} x
184
+ * @param {Number} y
185
+ * @return {Object}
186
+ * @api private
187
+ */
188
+
189
+ ColorPicker.prototype.colorAt = function(x, y){
190
+ var data = this.main.getContext('2d').getImageData(x, y, 1, 1).data;
191
+ return {
192
+ r: data[0]
193
+ , g: data[1]
194
+ , b: data[2]
195
+ , toString: function(){
196
+ return rgb(this.r, this.g, this.b);
197
+ }
198
+ };
199
+ };
200
+
201
+ /**
202
+ * Get the RGB value at `y`.
203
+ *
204
+ * @param {Type} name
205
+ * @return {Type}
206
+ * @api private
207
+ */
208
+
209
+ ColorPicker.prototype.hueAt = function(y){
210
+ var data = this.spectrum.getContext('2d').getImageData(0, y, 1, 1).data;
211
+ return {
212
+ r: data[0]
213
+ , g: data[1]
214
+ , b: data[2]
215
+ , toString: function(){
216
+ return rgb(this.r, this.g, this.b);
217
+ }
218
+ };
219
+ };
220
+
221
+ /**
222
+ * Get or set `color`.
223
+ *
224
+ * @param {String} color
225
+ * @return {String|ColorPicker}
226
+ * @api public
227
+ */
228
+
229
+ ColorPicker.prototype.color = function(color){
230
+ // TODO: update pos
231
+ if (0 == arguments.length) return this._color;
232
+ this._color = color;
233
+ return this;
234
+ };
235
+
236
+ /**
237
+ * Get or set hue `color`.
238
+ *
239
+ * @param {String} color
240
+ * @return {String|ColorPicker}
241
+ * @api public
242
+ */
243
+
244
+ ColorPicker.prototype.hue = function(color){
245
+ // TODO: update pos
246
+ if (0 == arguments.length) return this._hue;
247
+ this._hue = color;
248
+ return this;
249
+ };
250
+
251
+ /**
252
+ * Render with the given `options`.
253
+ *
254
+ * @param {Object} options
255
+ * @api public
256
+ */
257
+
258
+ ColorPicker.prototype.render = function(options){
259
+ options = options || {};
260
+ this.renderMain(options);
261
+ this.renderSpectrum(options);
262
+ };
263
+
264
+ /**
265
+ * Render spectrum.
266
+ *
267
+ * @api private
268
+ */
269
+
270
+ ColorPicker.prototype.renderSpectrum = function(options){
271
+ var el = this.el
272
+ , canvas = this.spectrum
273
+ , ctx = canvas.getContext('2d')
274
+ , pos = this._huePos
275
+ , w = this.w * .12
276
+ , h = this.h;
277
+
278
+ canvas.width = w;
279
+ canvas.height = h;
280
+
281
+ var grad = ctx.createLinearGradient(0, 0, 0, h);
282
+ grad.addColorStop(0, rgb(255, 0, 0));
283
+ grad.addColorStop(.15, rgb(255, 0, 255));
284
+ grad.addColorStop(.33, rgb(0, 0, 255));
285
+ grad.addColorStop(.49, rgb(0, 255, 255));
286
+ grad.addColorStop(.67, rgb(0, 255, 0));
287
+ grad.addColorStop(.84, rgb(255, 255, 0));
288
+ grad.addColorStop(1, rgb(255, 0, 0));
289
+
290
+ ctx.fillStyle = grad;
291
+ ctx.fillRect(0, 0, w, h);
292
+
293
+ // pos
294
+ if (!pos) return;
295
+ ctx.fillStyle = rgba(0,0,0, .3);
296
+ ctx.fillRect(0, pos, w, 1);
297
+ ctx.fillStyle = rgba(255,255,255, .3);
298
+ ctx.fillRect(0, pos + 1, w, 1);
299
+ };
300
+
301
+ /**
302
+ * Render hue/luminosity canvas.
303
+ *
304
+ * @api private
305
+ */
306
+
307
+ ColorPicker.prototype.renderMain = function(options){
308
+ var el = this.el
309
+ , canvas = this.main
310
+ , ctx = canvas.getContext('2d')
311
+ , w = this.w
312
+ , h = this.h
313
+ , x = (this._colorPos.x || w) + .5
314
+ , y = (this._colorPos.y || 0) + .5;
315
+
316
+ canvas.width = w;
317
+ canvas.height = h;
318
+
319
+ var grad = ctx.createLinearGradient(0, 0, w, 0);
320
+ grad.addColorStop(0, rgb(255, 255, 255));
321
+ grad.addColorStop(1, this._hue);
322
+
323
+ ctx.fillStyle = grad;
324
+ ctx.fillRect(0, 0, w, h);
325
+
326
+ grad = ctx.createLinearGradient(0, 0, 0, h);
327
+ grad.addColorStop(0, rgba(255, 255, 255, 0));
328
+ grad.addColorStop(1, rgba(0, 0, 0, 1));
329
+
330
+ ctx.fillStyle = grad;
331
+ ctx.fillRect(0, 0, w, h);
332
+
333
+ // pos
334
+ var rad = 10;
335
+ ctx.save();
336
+ ctx.beginPath();
337
+ ctx.lineWidth = 1;
338
+
339
+ // outer dark
340
+ ctx.strokeStyle = rgba(0,0,0,.5);
341
+ ctx.arc(x, y, rad / 2, 0, Math.PI * 2, false);
342
+ ctx.stroke();
343
+
344
+ // outer light
345
+ ctx.strokeStyle = rgba(255,255,255,.5);
346
+ ctx.arc(x, y, rad / 2 - 1, 0, Math.PI * 2, false);
347
+ ctx.stroke();
348
+
349
+ ctx.beginPath();
350
+ ctx.restore();
351
+ };