jquery-simplecolorpicker-rails 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module Jquery
2
2
  module Simplecolorpicker
3
3
  module Rails
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
6
6
  end
7
7
  end
@@ -23,52 +23,77 @@
23
23
  constructor: SimpleColorPicker,
24
24
 
25
25
  init: function(type, select, options) {
26
- this.type = type;
26
+ var self = this;
27
27
 
28
- this.$select = $(select);
29
- this.options = $.extend({}, $.fn.simplecolorpicker.defaults, options);
28
+ self.type = type;
30
29
 
31
- this.$select.hide();
30
+ self.$select = $(select);
31
+ var selectValue = self.$select.val();
32
+ self.options = $.extend({}, $.fn.simplecolorpicker.defaults, options);
33
+
34
+ self.$select.hide();
32
35
 
33
36
  // Trick: fix span alignment
34
37
  // When a span does not contain any text, its alignment is not correct
35
38
  var fakeText = '    ';
36
39
 
40
+ self.$colorList = null;
41
+
42
+ if (self.options.picker) {
43
+ var selectText = self.$select.find('option:selected').text();
44
+ self.$icon = $('<span class="simplecolorpicker icon" title="' + selectText + '" style="background-color: ' + selectValue + ';" role="button" tabindex="0">'
45
+ + fakeText
46
+ + '</span>').insertAfter(self.$select);
47
+ self.$icon.on('click.' + self.type, $.proxy(self.showPicker, self));
48
+
49
+ self.$picker = $('<span class="simplecolorpicker picker"></span>').appendTo(document.body);
50
+ self.$colorList = self.$picker;
51
+
52
+ // Hide picker when clicking outside
53
+ $(document).on('mousedown.' + self.type, $.proxy(self.hidePicker, self));
54
+ self.$picker.on('mousedown.' + self.type, $.proxy(self.mousedown, self));
55
+ } else {
56
+ self.$inline = $('<span class="simplecolorpicker inline"></span>').insertAfter(self.$select);
57
+ self.$colorList = self.$inline;
58
+ }
59
+
37
60
  // Build the list of colors
38
61
  // <div class="selected" title="Green" style="background-color: #7bd148;" role="button"></div>
39
- var colorList = '';
40
- $('option', this.$select).each(function() {
62
+ var colors = '';
63
+ $('option', self.$select).each(function() {
41
64
  var option = $(this);
42
65
  var color = option.val();
43
66
  var title = option.text();
44
67
  var selected = '';
45
- if (option.attr('selected')) {
68
+ if (option.prop('selected') === true || selectValue === color) {
46
69
  selected = 'class="selected"';
47
70
  }
48
- colorList += '<div ' + selected + ' title="' + title + '" style="background-color: ' + color + ';" role="button" tabindex="0">'
49
- + fakeText
50
- + '</div>';
71
+ colors += '<div ' + selected + ' title="' + title + '" style="background-color: ' + color + ';" role="button" tabindex="0">'
72
+ + fakeText
73
+ + '</div>';
51
74
  });
52
75
 
53
- if (this.options.picker) {
54
- var selectText = this.$select.find('option:selected').text();
55
- var selectValue = this.$select.val();
56
- this.$icon = $('<span class="simplecolorpicker icon" title="' + selectText + '" style="background-color: ' + selectValue + ';" role="button" tabindex="0">'
57
- + fakeText
58
- + '</span>').insertAfter(this.$select);
59
- this.$icon.on('click.' + this.type, $.proxy(this.showPicker, this));
76
+ self.$colorList.html(colors);
77
+ self.$colorList.on('click.' + self.type, $.proxy(self.click, self));
78
+ },
60
79
 
61
- this.$picker = $('<span class="simplecolorpicker picker"></span>').appendTo(document.body);
62
- this.$picker.html(colorList);
63
- this.$picker.on('click.' + this.type, $.proxy(this.click, this));
80
+ /**
81
+ * Changes the selected color.
82
+ *
83
+ * @param color the hexadecimal color to select, ex: '#fbd75b'
84
+ */
85
+ selectColor: function(color) {
86
+ var self = this;
64
87
 
65
- // Hide picker when clicking outside
66
- $(document).on('mousedown.' + this.type, $.proxy(this.hidePicker, this));
67
- this.$picker.on('mousedown.' + this.type, $.proxy(this.mousedown, this));
88
+ var colorDiv = self.$colorList.find('div').filter(function() {
89
+ var col = $(this).css('background-color');
90
+ return self.rgb2hex(col) === color;
91
+ });
92
+
93
+ if (colorDiv.length > 0) {
94
+ self.selectColorDiv(colorDiv);
68
95
  } else {
69
- this.$inline = $('<span class="simplecolorpicker inline"></span>').insertAfter(this.$select);
70
- this.$inline.html(colorList);
71
- this.$inline.on('click.' + this.type, $.proxy(this.click, this));
96
+ console.error("The given color '" + color + "' could not be found");
72
97
  }
73
98
  },
74
99
 
@@ -87,27 +112,39 @@
87
112
  this.$picker.hide(this.options.delay);
88
113
  },
89
114
 
90
- click: function(e) {
91
- var target = $(e.target);
92
- if (target.length === 1) {
93
- if (target[0].nodeName.toLowerCase() === 'div') {
94
- // When you click on a color
115
+ /**
116
+ * Selects the given div inside $colorList.
117
+ *
118
+ * The given div becomes the selected one.
119
+ * It also changes the HTML select value, this will emit the 'change' event.
120
+ */
121
+ selectColorDiv: function(colorDiv) {
122
+ var color = colorDiv.css('background-color');
123
+ var title = colorDiv.prop('title');
95
124
 
96
- var color = target.css('background-color');
97
- var title = target.attr('title');
125
+ // Mark this div as the selected one
126
+ colorDiv.siblings().removeClass('selected');
127
+ colorDiv.addClass('selected');
98
128
 
99
- // Mark this div as the selected one
100
- target.siblings().removeClass('selected');
101
- target.addClass('selected');
129
+ if (this.options.picker) {
130
+ this.$icon.css('background-color', color);
131
+ this.$icon.prop('title', title);
132
+ this.hidePicker();
133
+ }
102
134
 
103
- if (this.options.picker) {
104
- this.$icon.css('background-color', color);
105
- this.$icon.attr('title', title);
106
- this.hidePicker();
107
- }
135
+ // Change HTML select value
136
+ this.$select.val(this.rgb2hex(color)).change();
137
+ },
108
138
 
109
- // Change HTML select value
110
- this.$select.val(this.rgb2hex(color)).change();
139
+ /**
140
+ * The user clicked on a div inside $colorList.
141
+ */
142
+ click: function(e) {
143
+ var target = $(e.target);
144
+ if (target.length === 1) {
145
+ if (target[0].nodeName.toLowerCase() === 'div') {
146
+ // When you click on a color, make it the new selected one
147
+ this.selectColorDiv(target);
111
148
  }
112
149
  }
113
150
  },
@@ -144,14 +181,12 @@
144
181
  if (this.options.picker) {
145
182
  this.$icon.off('.' + this.type);
146
183
  this.$icon.remove();
147
- this.$picker.off('.' + this.type);
148
- this.$picker.remove();
149
184
  $(document).off('.' + this.type);
150
- } else {
151
- this.$inline.off('.' + this.type);
152
- this.$inline.remove();
153
185
  }
154
186
 
187
+ this.$colorList.off('.' + this.type);
188
+ this.$colorList.remove();
189
+
155
190
  this.$select.removeData(this.type);
156
191
  this.$select.show();
157
192
  }
@@ -162,6 +197,9 @@
162
197
  * How to use: $('#id').simplecolorpicker()
163
198
  */
164
199
  $.fn.simplecolorpicker = function(option) {
200
+ var args = $.makeArray(arguments);
201
+ args.shift();
202
+
165
203
  // For HTML element passed to the plugin
166
204
  return this.each(function() {
167
205
  var $this = $(this),
@@ -171,7 +209,7 @@
171
209
  $this.data('simplecolorpicker', (data = new SimpleColorPicker(this, options)));
172
210
  }
173
211
  if (typeof option === 'string') {
174
- data[option]();
212
+ data[option].apply(data, args);
175
213
  }
176
214
  });
177
215
  };
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery-simplecolorpicker-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-06 00:00:00.000000000 Z
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
16
- requirement: &70176716718180 !ruby/object:Gem::Requirement
16
+ requirement: &25882020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 3.1.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70176716718180
24
+ version_requirements: *25882020
25
25
  description: simplecolorpicker jQuery plugin
26
26
  email:
27
27
  - tkrotoff@gmail.com
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  version: '0'
60
60
  requirements: []
61
61
  rubyforge_project:
62
- rubygems_version: 1.8.15
62
+ rubygems_version: 1.8.16
63
63
  signing_key:
64
64
  specification_version: 3
65
65
  summary: simplecolorpicker packaged for the Rails 3.1+ asset pipeline