social_cheesecake 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,448 @@
1
+ socialCheesecake.defineModule(
2
+ 'SocialCheesecake#Sector'
3
+ )
4
+ .dependsOn(
5
+ 'SocialCheesecake#Text'
6
+ )
7
+ .withCode(function() {
8
+ socialCheesecake.Sector = function(settings) {
9
+ var defaultSettings = {
10
+ center : { x : 0, y : 0 },
11
+ rIn : 0,
12
+ rOut : 300,
13
+ delta : Math.PI / 2,
14
+ phi : 0,
15
+ label : "",
16
+ color : "#eeffee",
17
+ mouseover : { color : "#aaffaa" },
18
+ mouseout : { color : "#eeffee" },
19
+ mouseup : { color : "#77ff77" },
20
+ mousedown : { color : "#aaffaa" }
21
+ }
22
+ for(var property in defaultSettings) {
23
+ if(!( property in settings)) {
24
+ settings[property] = defaultSettings[property];
25
+ }
26
+ }
27
+ if(settings.phi < 0 || settings.phi > 2 * Math.PI) {
28
+ throw "Phi must be greater or equal to 0 and less than 2*pi";
29
+ }
30
+ if(settings.delta <= 0 || settings.delta > 2 * Math.PI) {
31
+ throw "Delta must be greater than 0 and less than 2*pi";
32
+ }
33
+ this.x = settings.center.x;
34
+ this.y = settings.center.y;
35
+ this.rOut = settings.rOut;
36
+ this.rIn = settings.rIn;
37
+ this.phi = settings.phi;
38
+ this.delta = settings.delta;
39
+ this.label = settings.label;
40
+ this.color = settings.color;
41
+ this.mouseover = settings.mouseover;
42
+ this.mouseup = settings.mouseup;
43
+ this.mouseout = settings.mouseout;
44
+ this.mousedown = settings.mousedown;
45
+ this.subsectors = [];
46
+ this.actors = [];
47
+ if(settings.parent != null) this.parent = settings.parent;
48
+ if(settings.simulate != null) this.simulate = settings.simulate;
49
+
50
+ if(settings.subsectors != null) {
51
+ var rInSubsector = this.rIn;
52
+ var separation = (this.rOut - this.rIn) / settings.subsectors.length;
53
+ for(var i in settings.subsectors) {
54
+ var rOutSubsector = rInSubsector + separation;
55
+ var layer = new socialCheesecake.Subsector({
56
+ label : settings.subsectors[i].name,
57
+ parent : this,
58
+ x : this.x,
59
+ y : this.y,
60
+ phi : this.phi,
61
+ delta : this.delta,
62
+ rIn : rInSubsector,
63
+ rOut : rOutSubsector,
64
+ actors : settings.subsectors[i].actors
65
+ });
66
+ rInSubsector = rOutSubsector;
67
+ this.subsectors.push(layer);
68
+ }
69
+ }
70
+ this.originalAttr = {
71
+ x : this.x,
72
+ y : this.y,
73
+ phi : this.phi,
74
+ delta : this.delta,
75
+ rIn : this.rIn,
76
+ rOut : this.rOut,
77
+ color : this.color,
78
+ label : this.label,
79
+ mouseover : this.mouseover,
80
+ mouseout : this.mouseout,
81
+ mousedown : this.mousedown,
82
+ mouseup : this.mouseup,
83
+ simulate : this.simulate,
84
+ subsectors : this.subsectors
85
+ };
86
+ this._region = null;
87
+ }
88
+
89
+ socialCheesecake.Sector.prototype._draw = function(context, options) {
90
+ var x = this.x;
91
+ var y = this.y;
92
+ var phi = this.phi;
93
+ var delta = this.delta;
94
+ var rIn = this.rIn;
95
+ var rOut = this.rOut;
96
+ var color = this.color;
97
+ var label = this.label;
98
+ if(options != null) {
99
+ if(options.x != null) x = options.x;
100
+ if(options.y != null) y = options.y;
101
+ if(options.phi != null) phi = options.phi;
102
+ if(options.delta != null) delta = options.delta;
103
+ if(options.rIn != null) rIn = options.rIn;
104
+ if(options.rOut != null) rOut = options.rOut;
105
+ if(options.color != null) color = options.color;
106
+ if(options.label != null) label = options.label;
107
+ }
108
+ context.restore();
109
+ context.save();
110
+ context.beginPath();
111
+ context.arc(x, y, rOut, -phi, -(phi + delta), true);
112
+ context.lineTo(x + rIn * Math.cos(-phi - delta), y + rIn * Math.sin(-phi - delta));
113
+ context.arc(x, y, rIn, -(phi + delta), -phi, false);
114
+ context.closePath();
115
+ context.fillStyle = color;
116
+ context.fill();
117
+ context.lineWidth = 4;
118
+ context.stroke();
119
+ socialCheesecake.text.writeCurvedText(label, context, x, y, (rOut + rIn) / 2, phi, delta);
120
+ }
121
+
122
+ socialCheesecake.Sector.prototype.getRegion = function(regenerate) {
123
+ if((this._region == null) || (regenerate == true)) {
124
+ var sector = this;
125
+ if(sector._region != null) {
126
+ /* TO-DO!!!
127
+ if(sector.parent != null){
128
+ var cheesecake=sector.parent;
129
+ var regions= cheesecake.stage.regions;
130
+ var regionIndex;
131
+ for(var j in regions){
132
+ if(regions[j]==sector._region){
133
+ regionIndex= j;
134
+ }
135
+ }
136
+ }
137
+ var canvas=sector.getRegion().getCanvas();
138
+ if((canvas!=null)&&(canvas.parentNode!=null)){
139
+ canvas.parentNode.removeChild(canvas);
140
+ }*/
141
+ }
142
+ sector._region = new Kinetic.Shape(function() {
143
+ var context = this.getContext();
144
+ sector._draw(context);
145
+ });
146
+ /*
147
+ if(regionIndex!=null){
148
+ regions[regionIndex]=this._region;
149
+ } */
150
+ sector._region.addEventListener('mouseover', function() {
151
+ sector.eventHandler('mouseover');
152
+ });
153
+ sector._region.addEventListener('mouseout', function() {
154
+ sector.eventHandler('mouseout');
155
+ });
156
+ sector._region.addEventListener('mousedown', function() {
157
+ sector.eventHandler('mousedown');
158
+ });
159
+ sector._region.addEventListener('mouseup', function() {
160
+ sector.eventHandler('mouseup');
161
+ });
162
+ }
163
+ return this._region
164
+ }
165
+
166
+ socialCheesecake.Sector.prototype.eventHandler = function(eventName) {
167
+ var sector = this;
168
+ if(sector[eventName] != null){
169
+ if(sector[eventName].color != null) {
170
+ var color = sector[eventName].color;
171
+ sector.changeColor(color);
172
+ }
173
+ if(sector[eventName].callback != null) {
174
+ sector[eventName].callback(sector);
175
+ }
176
+ }
177
+ }
178
+
179
+ socialCheesecake.Sector.prototype.splitUp = function() {
180
+ var cheesecake = this.parent;
181
+ var phi = this.phi;
182
+ var delta = this.delta;
183
+ var rOut = this.rOut;
184
+ var rIn = this.rIn;
185
+ var sector; (this.simulate != null) ? sector = cheesecake.sectors[this.simulate] : sector = this;
186
+ var subsectors = sector.subsectors;
187
+
188
+ //Draw sector's subsectors over it
189
+ var subsectorRIn = rIn;
190
+ var separation = 0;
191
+ if(subsectors.length > 0)
192
+ separation = (rOut + rIn) / subsectors.length;
193
+ for(var i in subsectors) {
194
+ subsectors[i].rIn = rIn;
195
+ subsectors[i].rOut = rIn + separation;
196
+ subsectors[i].phi = phi;
197
+ subsectors[i].delta = delta;
198
+ cheesecake.stage.add(subsectors[i].getRegion());
199
+ rIn += separation;
200
+ }
201
+ }
202
+
203
+ socialCheesecake.Sector.prototype.putTogether = function() {
204
+ var cheesecake = this.parent;
205
+ var sector; (this.simulate != null) ? sector = cheesecake.sectors[this.simulate] : sector = this;
206
+ var subsectors = sector.subsectors;
207
+ //Clear subsectors from stage
208
+ for(var i in subsectors) {
209
+ cheesecake.stage.remove(subsectors[i].getRegion());
210
+ }
211
+ }
212
+
213
+ socialCheesecake.Sector.prototype.changeColor = function(color) {
214
+ var sector = this;
215
+ if (sector._region){
216
+ var context = sector._region.getContext();
217
+ sector.color = color;
218
+ context.fillStyle = color;
219
+ context.fill();
220
+ context.lineWidth = 4;
221
+ context.stroke();
222
+ sector._region.getContext().restore();
223
+ sector._region.getContext().save();
224
+ socialCheesecake.text.writeCurvedText(sector.label, sector._region.getContext(),
225
+ sector.x, sector.y, (sector.rOut + sector.rIn) / 2, sector.phi, sector.delta);
226
+ }
227
+ }
228
+
229
+ /**
230
+ *
231
+ * Options:
232
+ * delta - new delta to achieve
233
+ * context - sector context to work with
234
+ * step - sets the animation speed
235
+ * anchor - "beginning" , "b", "B"
236
+ * "middle", "m", "M"
237
+ * "end", "e", "E"
238
+ *
239
+ **/
240
+ socialCheesecake.Sector.prototype.resize = function(options) {
241
+ if(!options)
242
+ throw "No arguments passed to the function";
243
+ if(options.context == null)
244
+ throw "context must be defined"
245
+ var context = options.context;
246
+ var sector = this;
247
+ var currentDelta = sector.delta;
248
+ var currentPhi = sector.phi;
249
+ var step = 0.05;
250
+ var goalDelta = Math.PI / 2;
251
+ var anchor = 1;
252
+ if(options.step) step = options.step;
253
+ if(options.delta) {
254
+ goalDelta = options.delta;
255
+ }
256
+ if(options.anchor) {
257
+ if((options.anchor.toLowerCase() == "b") || (options.anchor == "beginning"))
258
+ anchor = 0;
259
+ if((options.anchor.toLowerCase() == "m") || (options.anchor == "middle"))
260
+ anchor = 0.5;
261
+ if((options.anchor.toLowerCase() == "e") || (options.anchor == "end"))
262
+ anchor = 1;
263
+ }
264
+
265
+ if(currentDelta > goalDelta) {
266
+ if(currentDelta - goalDelta < step) step = currentDelta - goalDelta;
267
+ currentDelta -= step;
268
+ currentPhi += anchor * step;
269
+ } else if(currentDelta < goalDelta) {
270
+ if(goalDelta - currentDelta < step) step = goalDelta - currentDelta;
271
+ currentDelta += step;
272
+ currentPhi -= anchor * step;
273
+ }
274
+ sector.delta = currentDelta;
275
+ sector.phi = currentPhi;
276
+
277
+ sector.clear();
278
+ sector._draw(context);
279
+ if(currentDelta != goalDelta) {
280
+ requestAnimFrame(function() {
281
+ sector.resize(options);
282
+ });
283
+ } else if(options.callback) {
284
+ options.callback();
285
+ }
286
+ }
287
+
288
+ socialCheesecake.Sector.prototype.focus = function() {
289
+ var sector = this;
290
+ var context = sector._region.getContext();
291
+ sector.rOut *= 1.05;
292
+ sector.clear();
293
+ sector._draw(context);
294
+ }
295
+
296
+ socialCheesecake.Sector.prototype.unfocus = function() {
297
+ var sector = this;
298
+ var context = sector._region.getContext();
299
+ sector.rOut = sector.originalAttr.rOut;
300
+ sector.clear();
301
+ sector._draw(context);
302
+ }
303
+
304
+ socialCheesecake.Sector.prototype.clear = function() {
305
+ var sector = this;
306
+ var context = sector.getRegion().getContext();
307
+ if(context != undefined) {
308
+ context.restore();
309
+ context.save();
310
+ }
311
+ sector.getRegion().clear();
312
+ }
313
+
314
+ socialCheesecake.Sector.prototype.rotateTo = function(options) {
315
+ // update stage
316
+ var sector = this;
317
+ var currentPhi = this.phi;
318
+ var delta = this.delta;
319
+ var step = 0.05;
320
+ var anchor = 0;
321
+ if(!options) throw "No arguments passed to the function";
322
+ if(options.step) step = options.step;
323
+ if(options.context == null) throw "context must be defined";
324
+ var context = options.context;
325
+ if(options.destination == null) throw "destination must be defined";
326
+ if(options.anchor){
327
+ if((options.anchor.toLowerCase() == "b") || (options.anchor == "beginning"))
328
+ anchor = 0;
329
+ if((options.anchor.toLowerCase() == "m") || (options.anchor == "middle"))
330
+ anchor = 0.5;
331
+ if((options.anchor.toLowerCase() == "e") || (options.anchor == "end"))
332
+ anchor = 1;
333
+ }
334
+ var phiDestination = (options.destination- anchor*delta) % (2 * Math.PI) ;
335
+ while(phiDestination < 0) {
336
+ phiDestination += (2 * Math.PI);
337
+ }
338
+
339
+ var grow = 0;
340
+ if(phiDestination > currentPhi) {
341
+ grow = 1;
342
+ } else if(phiDestination < currentPhi) {
343
+ grow = -1;
344
+ }
345
+ if(Math.round(((2 * Math.PI) - Math.abs(phiDestination - currentPhi) ) * 1000) / 1000 >= Math.round(Math.abs(phiDestination - currentPhi) * 1000) / 1000) {
346
+ if(Math.abs(phiDestination - currentPhi) < step)
347
+ step = Math.abs(phiDestination - currentPhi);
348
+ currentPhi += (grow * step);
349
+
350
+ // if (grow>0) console.log("giro al contrario agujas. Caso 1 ");
351
+ //if(grow<0) console.log("giro segun agujas. Caso 1");
352
+ } else {
353
+ if((2 * Math.PI) - Math.abs(phiDestination - currentPhi) < step)
354
+ step = (2 * Math.PI) - Math.abs(phiDestination - currentPhi);
355
+ phiDestination -= (grow * 2 * Math.PI);
356
+ currentPhi -= (grow * step);
357
+ //if (grow<0) console.log("giro al contrario agujas. Caso 2");
358
+ //if(grow>0) console.log("giro segun agujas. Caso 2 ");
359
+ }
360
+ sector.phi = currentPhi;
361
+
362
+ // clear stage
363
+ sector.clear();
364
+
365
+ // draw stage
366
+ this._draw(context);
367
+
368
+ // request new frame
369
+ if(Math.abs(currentPhi - phiDestination) > 0.001) {
370
+ sector.phi = currentPhi % (2 * Math.PI);
371
+ requestAnimFrame(function() {
372
+ sector.rotateTo({
373
+ context : context,
374
+ destination : options.destination,
375
+ step : step,
376
+ callback : options.callback,
377
+ anchor : options.anchor
378
+ });
379
+ });
380
+ } else {
381
+ if(options.callback) options.callback();
382
+ }
383
+ }
384
+
385
+ socialCheesecake.Sector.prototype.addActor = function(actor_info , subsector){
386
+ var actors = this.actors;
387
+ var actor;
388
+
389
+ //Check if the actor is already in the array
390
+ var actorAlreadyDeclared = false;
391
+ for (var i in actors){
392
+ if (actors[i].id == actor_info.id){
393
+ actorAlreadyDeclared = true;
394
+ actor = actors[i];
395
+ //Check if the subsector has already been declared a parent of the actor
396
+ var subsectorAlreadyDeclared = false;
397
+ for ( var parent in actor.parents){
398
+ if (actor.parents[parent] == subsector) subsectorAlreadyDeclared=true;
399
+ }
400
+ if (!subsectorAlreadyDeclared) actor.parents.push(subsector);
401
+ }
402
+ }
403
+ // If the actor was not in the array, ask the parent or the grid for it
404
+ if(!actorAlreadyDeclared){
405
+ if (this == subsector){
406
+ actor = this.parent.addActor(actor_info, subsector);
407
+ }else{
408
+ actor = this.parent.grid.addActor(actor_info, subsector);
409
+ }
410
+ actors.push(actor);
411
+ }
412
+ return actor;
413
+ }
414
+
415
+ /*SUBSECTOR*/
416
+ socialCheesecake.Subsector = function(settings) {
417
+ if(settings.parent != null) this.parent = settings.parent;
418
+ this.label = "";
419
+ if(settings.label != null) this.label = settings.label;
420
+ this.x = settings.x;
421
+ this.y = settings.y;
422
+ this.rOut = settings.rOut;
423
+ this.rIn = settings.rIn;
424
+ this.phi = settings.phi;
425
+ this.delta = settings.delta;
426
+ this.actors = [];
427
+
428
+ var grid = this.parent.parent.grid;
429
+ if (settings.actors){
430
+ for( var actor in settings.actors){
431
+ var actor_info = {
432
+ id : settings.actors[actor][0]
433
+ }
434
+ this.addActor(actor_info ,this);
435
+ }
436
+ }
437
+ }
438
+
439
+ socialCheesecake.Subsector.prototype = new socialCheesecake.Sector({
440
+ parent : this.parent,
441
+ center : { x : this.x, y : this.y },
442
+ label : this.label,
443
+ rIn : this.rIn,
444
+ rOut : this.rOut,
445
+ phi : this.phi,
446
+ delta : this.delta
447
+ });
448
+ });
@@ -0,0 +1,36 @@
1
+ socialCheesecake.defineModule(
2
+ 'SocialCheesecake#Text'
3
+ ).dependsOn(
4
+
5
+ ).withCode(function() {
6
+ socialCheesecake.text = {
7
+ writeCurvedText : function(text, context, x, y, r, phi, delta) {
8
+ context.font = "bold 14px sans-serif";
9
+ context.fillStyle = '#000';
10
+ context.textBaseline = "middle";
11
+ var medium_alpha = Math.tan(context.measureText(text).width / (text.length * r));
12
+ if(medium_alpha * text.length <= delta) {
13
+ context.translate(x, y);
14
+ var orientation = 0;
15
+ if((phi + delta / 2 >= Math.PI ) && (phi + delta / 2 < Math.PI * 2)) {
16
+ orientation = -1;
17
+ context.rotate(-(delta - (medium_alpha * text.length)) / 2 - phi - Math.PI / 2);
18
+ } else {
19
+ orientation = 1;
20
+ context.rotate((delta - (medium_alpha * text.length)) / 2 + Math.PI / 2 - delta - phi);
21
+ }
22
+ for(var i = 0; i < text.length; i++) {
23
+ context.fillText(text[i], 0, -(orientation * r));
24
+ var alpha = Math.tan(context.measureText(text[i]).width / r);
25
+ context.rotate(orientation * alpha);
26
+ }
27
+ return true;
28
+ } else {
29
+ return false;
30
+ }
31
+ },
32
+ writeCenterText : function(text, context, centerX, centerY) {
33
+ context.fillText(text, centerX - context.measureText(text).width / 2, centerY);
34
+ }
35
+ }
36
+ });
@@ -1,3 +1,3 @@
1
1
  module SocialCheesecake
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -13,7 +13,8 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.rubyforge_project = "social_cheesecake"
15
15
 
16
- s.files = `git ls-files`.split("\n")
16
+ s.files = `git ls-files`.split("\n") |
17
+ Dir["app/assets/javascripts/socialcheesecake/*"]
17
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
20
  s.require_paths = ["lib"]
metadata CHANGED
@@ -1,71 +1,61 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: social_cheesecake
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
11
6
  platform: ruby
12
- authors:
13
- - "Alicia D\xC3\xADez"
7
+ authors:
8
+ - Alicia Díez
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-12-14 00:00:00 Z
12
+ date: 2011-12-19 00:00:00.000000000Z
19
13
  dependencies: []
20
-
21
14
  description: Wrapper gem for socialCheesecake.js
22
- email:
15
+ email:
23
16
  - adiezbal@gmail.com
24
17
  executables: []
25
-
26
18
  extensions: []
27
-
28
19
  extra_rdoc_files: []
29
-
30
- files:
20
+ files:
21
+ - .gitignore
31
22
  - Gemfile
32
23
  - Rakefile
24
+ - app/assets/javascripts/kinetic.js
25
+ - app/assets/javascripts/socialCheesecake.js
26
+ - app/assets/javascripts/social_cheesecake.js
33
27
  - lib/social_cheesecake.rb
34
28
  - lib/social_cheesecake/engine.rb
35
29
  - lib/social_cheesecake/version.rb
36
30
  - social_cheesecake.gemspec
37
- homepage: ""
31
+ - app/assets/javascripts/socialcheesecake/sector.js
32
+ - app/assets/javascripts/socialcheesecake/actor.js
33
+ - app/assets/javascripts/socialcheesecake/text.js
34
+ - app/assets/javascripts/socialcheesecake/cheesecake.js
35
+ - app/assets/javascripts/socialcheesecake/grid.js
36
+ homepage: ''
38
37
  licenses: []
39
-
40
38
  post_install_message:
41
39
  rdoc_options: []
42
-
43
- require_paths:
40
+ require_paths:
44
41
  - lib
45
- required_ruby_version: !ruby/object:Gem::Requirement
42
+ required_ruby_version: !ruby/object:Gem::Requirement
46
43
  none: false
47
- requirements:
48
- - - ">="
49
- - !ruby/object:Gem::Version
50
- hash: 3
51
- segments:
52
- - 0
53
- version: "0"
54
- required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
49
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
61
- - 0
62
- version: "0"
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
63
54
  requirements: []
64
-
65
55
  rubyforge_project: social_cheesecake
66
- rubygems_version: 1.8.10
56
+ rubygems_version: 1.8.12
67
57
  signing_key:
68
58
  specification_version: 3
69
59
  summary: Wrapper gem for socialCheesecake.js
70
60
  test_files: []
71
-
61
+ has_rdoc: