Skip to content

Commit 1f5cc92

Browse files
author
Michael Bleigh
committed
Fix issue with empty actions array.
1 parent d358361 commit 1f5cc92

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

src/sketch.coffee

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Released under the MIT License
4949
false
5050

5151

52+
set: (key, value)->
53+
this[key] = value
54+
5255
startPainting: ->
5356
@painting = true
5457
@action = {
@@ -95,7 +98,8 @@ Released under the MIT License
9598
@context = @el.getContext '2d'
9699
sketch = this
97100
$.each @actions, ->
98-
$.sketch.tools[this.tool].draw.call sketch, this
101+
if this.tool
102+
$.sketch.tools[this.tool].draw.call sketch, this
99103
$.sketch.tools[@action.tool].draw.call sketch, @action if @painting && @action
100104

101105
download: (filename, format)->
@@ -124,9 +128,22 @@ Released under the MIT License
124128
@context.lineWidth = action.size
125129
@context.stroke()
126130

127-
$.fn.sketch = (opts)->
128-
$.error('Sketch can only be called on one element at a time.') if this.length > 1
129-
this.data('sketch', new Sketch(this.get(0), opts))
130-
this
131+
$.fn.sketch = (key, args...)->
132+
$.error('Sketch.js can only be called on one element at a time.') if this.length > 1
133+
sketch = this.data('sketch')
134+
if typeof(key) == 'string' && sketch
135+
if sketch[key]
136+
sketch[key].apply sketch, args
137+
else if args.length == 1
138+
sketch.set key, args[0]
139+
else if args.length == 0
140+
sketch[key]
141+
else
142+
$.error('Sketch.js did not recognize the given command.')
143+
else if sketch
144+
sketch
145+
else
146+
this.data('sketch', new Sketch(this.get(0), key))
147+
this
131148

132149
)(jQuery)

src/sketch.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Sketch.js v0.0.1
44
Copyright 2011 Michael Bleigh and Intridea, Inc.
55
Released under the MIT License
66
7-
*/(function($) {
7+
*/
8+
var __slice = Array.prototype.slice;
9+
(function($) {
810
var Sketch;
911
$.sketch = {
1012
tools: {}
@@ -54,6 +56,9 @@ Released under the MIT License
5456
});
5557
}
5658
}
59+
Sketch.prototype.set = function(key, value) {
60+
return this[key] = value;
61+
};
5762
Sketch.prototype.startPainting = function() {
5863
this.painting = true;
5964
return this.action = {
@@ -111,7 +116,9 @@ Released under the MIT License
111116
this.context = this.el.getContext('2d');
112117
sketch = this;
113118
$.each(this.actions, function() {
114-
return $.sketch.tools[this.tool].draw.call(sketch, this);
119+
if (this.tool) {
120+
return $.sketch.tools[this.tool].draw.call(sketch, this);
121+
}
115122
});
116123
if (this.painting && this.action) {
117124
return $.sketch.tools[this.action.tool].draw.call(sketch, this.action);
@@ -148,11 +155,28 @@ Released under the MIT License
148155
return this.context.stroke();
149156
}
150157
};
151-
return $.fn.sketch = function(opts) {
158+
return $.fn.sketch = function() {
159+
var args, key, sketch;
160+
key = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
152161
if (this.length > 1) {
153-
$.error('Sketch can only be called on one element at a time.');
162+
$.error('Sketch.js can only be called on one element at a time.');
163+
}
164+
sketch = this.data('sketch');
165+
if (typeof key === 'string' && sketch) {
166+
if (sketch[key]) {
167+
return sketch[key].apply(sketch, args);
168+
} else if (args.length === 1) {
169+
return sketch.set(key, args[0]);
170+
} else if (args.length === 0) {
171+
return sketch[key];
172+
} else {
173+
return $.error('Sketch.js did not recognize the given command.');
174+
}
175+
} else if (sketch) {
176+
return sketch;
177+
} else {
178+
this.data('sketch', new Sketch(this.get(0), key));
179+
return this;
154180
}
155-
this.data('sketch', new Sketch(this.get(0), opts));
156-
return this;
157181
};
158182
})(jQuery);

0 commit comments

Comments
 (0)