very_best_in_place 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +14 -0
  5. data/CHANGELOG.md +36 -0
  6. data/Gemfile +8 -0
  7. data/README.md +438 -0
  8. data/Rakefile +8 -0
  9. data/best_in_place.gemspec +26 -0
  10. data/lib/assets/javascripts/best_in_place.js +722 -0
  11. data/lib/assets/javascripts/best_in_place.purr.js +10 -0
  12. data/lib/assets/javascripts/jquery.purr.js +161 -0
  13. data/lib/best_in_place.rb +12 -0
  14. data/lib/best_in_place/check_version.rb +8 -0
  15. data/lib/best_in_place/controller_extensions.rb +28 -0
  16. data/lib/best_in_place/display_methods.rb +44 -0
  17. data/lib/best_in_place/engine.rb +8 -0
  18. data/lib/best_in_place/helper.rb +128 -0
  19. data/lib/best_in_place/railtie.rb +7 -0
  20. data/lib/best_in_place/test_helpers.rb +41 -0
  21. data/lib/best_in_place/utils.rb +21 -0
  22. data/lib/best_in_place/version.rb +3 -0
  23. data/spec/helpers/best_in_place_spec.rb +407 -0
  24. data/spec/integration/double_init_spec.rb +34 -0
  25. data/spec/integration/js_spec.rb +959 -0
  26. data/spec/integration/live_spec.rb +40 -0
  27. data/spec/integration/text_area_spec.rb +40 -0
  28. data/spec/spec_helper.rb +23 -0
  29. data/spec/support/retry_on_timeout.rb +10 -0
  30. data/test_app/Gemfile +16 -0
  31. data/test_app/README +256 -0
  32. data/test_app/Rakefile +7 -0
  33. data/test_app/app/assets/images/no.png +0 -0
  34. data/test_app/app/assets/images/red_pen.png +0 -0
  35. data/test_app/app/assets/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  36. data/test_app/app/assets/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  37. data/test_app/app/assets/images/ui-bg_flat_10_000000_40x100.png +0 -0
  38. data/test_app/app/assets/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  39. data/test_app/app/assets/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  40. data/test_app/app/assets/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  41. data/test_app/app/assets/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  42. data/test_app/app/assets/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  43. data/test_app/app/assets/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  44. data/test_app/app/assets/images/ui-icons_222222_256x240.png +0 -0
  45. data/test_app/app/assets/images/ui-icons_228ef1_256x240.png +0 -0
  46. data/test_app/app/assets/images/ui-icons_ef8c08_256x240.png +0 -0
  47. data/test_app/app/assets/images/ui-icons_ffd27a_256x240.png +0 -0
  48. data/test_app/app/assets/images/ui-icons_ffffff_256x240.png +0 -0
  49. data/test_app/app/assets/images/yes.png +0 -0
  50. data/test_app/app/assets/javascripts/application.js +35 -0
  51. data/test_app/app/assets/stylesheets/.gitkeep +0 -0
  52. data/test_app/app/assets/stylesheets/jquery-ui-1.8.16.custom.css.erb +357 -0
  53. data/test_app/app/assets/stylesheets/scaffold.css +60 -0
  54. data/test_app/app/assets/stylesheets/style.css.erb +79 -0
  55. data/test_app/app/controllers/admin/users_controller.rb +14 -0
  56. data/test_app/app/controllers/application_controller.rb +3 -0
  57. data/test_app/app/controllers/cuca/cars_controller.rb +16 -0
  58. data/test_app/app/controllers/users_controller.rb +99 -0
  59. data/test_app/app/helpers/application_helper.rb +2 -0
  60. data/test_app/app/helpers/users_helper.rb +29 -0
  61. data/test_app/app/models/cuca/car.rb +5 -0
  62. data/test_app/app/models/user.rb +27 -0
  63. data/test_app/app/views/admin/users/show.html.erb +20 -0
  64. data/test_app/app/views/cuca/cars/show.html.erb +13 -0
  65. data/test_app/app/views/layouts/application.html.erb +14 -0
  66. data/test_app/app/views/users/_form.html.erb +51 -0
  67. data/test_app/app/views/users/double_init.html.erb +72 -0
  68. data/test_app/app/views/users/email_field.html.erb +1 -0
  69. data/test_app/app/views/users/index.html.erb +25 -0
  70. data/test_app/app/views/users/new.html.erb +5 -0
  71. data/test_app/app/views/users/show.html.erb +135 -0
  72. data/test_app/app/views/users/show_ajax.html.erb +12 -0
  73. data/test_app/config.ru +4 -0
  74. data/test_app/config/application.rb +51 -0
  75. data/test_app/config/boot.rb +13 -0
  76. data/test_app/config/database.yml +22 -0
  77. data/test_app/config/environment.rb +5 -0
  78. data/test_app/config/environments/development.rb +25 -0
  79. data/test_app/config/environments/production.rb +49 -0
  80. data/test_app/config/environments/test.rb +35 -0
  81. data/test_app/config/initializers/backtrace_silencers.rb +7 -0
  82. data/test_app/config/initializers/countries.rb +1 -0
  83. data/test_app/config/initializers/default_date_format.rb +2 -0
  84. data/test_app/config/initializers/inflections.rb +10 -0
  85. data/test_app/config/initializers/mime_types.rb +5 -0
  86. data/test_app/config/initializers/secret_token.rb +7 -0
  87. data/test_app/config/initializers/session_store.rb +8 -0
  88. data/test_app/config/locales/en.yml +5 -0
  89. data/test_app/config/routes.rb +20 -0
  90. data/test_app/db/migrate/20101206205922_create_users.rb +18 -0
  91. data/test_app/db/migrate/20101212170114_add_receive_email_to_user.rb +9 -0
  92. data/test_app/db/migrate/20110115204441_add_description_to_user.rb +9 -0
  93. data/test_app/db/migrate/20111210084202_add_favorite_color_to_users.rb +5 -0
  94. data/test_app/db/migrate/20111210084251_add_favorite_books_to_users.rb +5 -0
  95. data/test_app/db/migrate/20111217215935_add_birth_date_to_users.rb +5 -0
  96. data/test_app/db/migrate/20111224181356_add_money_to_user.rb +5 -0
  97. data/test_app/db/migrate/20120513003308_create_cars.rb +11 -0
  98. data/test_app/db/migrate/20120607172609_add_favorite_movie_to_users.rb +5 -0
  99. data/test_app/db/migrate/20120616170454_add_money_proc_to_users.rb +6 -0
  100. data/test_app/db/migrate/20120620165212_add_height_to_user.rb +5 -0
  101. data/test_app/db/schema.rb +40 -0
  102. data/test_app/db/seeds.rb +19 -0
  103. data/test_app/doc/README_FOR_APP +2 -0
  104. data/test_app/lib/tasks/.gitkeep +0 -0
  105. data/test_app/lib/tasks/cron.rake +7 -0
  106. data/test_app/public/404.html +26 -0
  107. data/test_app/public/422.html +26 -0
  108. data/test_app/public/500.html +26 -0
  109. data/test_app/public/favicon.ico +0 -0
  110. data/test_app/public/robots.txt +5 -0
  111. data/test_app/script/rails +6 -0
  112. data/test_app/test/fixtures/users.yml +17 -0
  113. data/test_app/test/functional/users_controller_test.rb +49 -0
  114. data/test_app/test/performance/browsing_test.rb +9 -0
  115. data/test_app/test/test_helper.rb +13 -0
  116. data/test_app/test/unit/helpers/users_helper_test.rb +4 -0
  117. data/test_app/test/unit/user_test.rb +8 -0
  118. data/test_app/vendor/plugins/.gitkeep +0 -0
  119. metadata +241 -0
@@ -0,0 +1,8 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "best_in_place/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "very_best_in_place"
7
+ s.version = BestInPlace::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bernat Farrero"]
10
+ s.email = ["bernat@itnig.net"]
11
+ s.homepage = "http://github.com/cavneb/best_in_place"
12
+ s.summary = %q{It makes any field in place editable by clicking on it, it works for inputs, textareas, select dropdowns and checkboxes}
13
+ s.description = %q{BestInPlace is a jQuery script and a Rails 3 helper that provide the method best_in_place to display any object field easily editable for the user by just clicking on it. It supports input data, text data, boolean data and custom dropdown data. It works with RESTful controllers.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "rails", "~> 4"
21
+ s.add_dependency "jquery-rails"
22
+
23
+ s.add_development_dependency "rspec-rails"
24
+ s.add_development_dependency "nokogiri"
25
+ s.add_development_dependency "capybara"
26
+ end
@@ -0,0 +1,722 @@
1
+ /*
2
+ BestInPlace (for jQuery)
3
+ version: 0.1.0 (01/01/2011)
4
+ @requires jQuery >= v1.4
5
+ @requires jQuery.purr to display pop-up windows
6
+
7
+ By Bernat Farrero based on the work of Jan Varwig.
8
+ Examples at http://bernatfarrero.com
9
+
10
+ Licensed under the MIT:
11
+ http://www.opensource.org/licenses/mit-license.php
12
+
13
+ Usage:
14
+
15
+ Attention.
16
+ The format of the JSON object given to the select inputs is the following:
17
+ [["key", "value"],["key", "value"]]
18
+ The format of the JSON object given to the checkbox inputs is the following:
19
+ ["falseValue", "trueValue"]
20
+ */
21
+
22
+ function BestInPlaceEditor(e) {
23
+ this.element = e;
24
+ this.initOptions();
25
+ this.bindForm();
26
+ this.initNil();
27
+ jQuery(this.activator).bind('click', {editor: this}, this.clickHandler);
28
+ }
29
+
30
+ BestInPlaceEditor.prototype = {
31
+ // Public Interface Functions //////////////////////////////////////////////
32
+
33
+ activate : function() {
34
+ var to_display = "";
35
+ if (this.isNil) {
36
+ to_display = "";
37
+ }
38
+ else if (this.original_content) {
39
+ to_display = this.original_content;
40
+ }
41
+ else {
42
+ if (this.sanitize) {
43
+ to_display = this.element.text();
44
+ } else {
45
+ to_display = this.element.html();
46
+ }
47
+ }
48
+
49
+ var elem = this.isNil ? "-" : this.element.html();
50
+ this.oldValue = elem;
51
+ this.display_value = to_display;
52
+ jQuery(this.activator).unbind("click", this.clickHandler);
53
+ this.activateForm();
54
+ this.element.trigger(jQuery.Event("best_in_place:activate"));
55
+ },
56
+
57
+ abort : function() {
58
+ if (this.isNil) this.element.html(this.nil);
59
+ else this.element.html(this.oldValue);
60
+ jQuery(this.activator).bind('click', {editor: this}, this.clickHandler);
61
+ this.element.trigger(jQuery.Event("best_in_place:abort"));
62
+ this.element.trigger(jQuery.Event("best_in_place:deactivate"));
63
+ },
64
+
65
+ abortIfConfirm : function () {
66
+ if (!this.useConfirm) {
67
+ this.abort();
68
+ return;
69
+ }
70
+
71
+ if (confirm("Are you sure you want to discard your changes?")) {
72
+ this.abort();
73
+ }
74
+ },
75
+
76
+ update : function() {
77
+ var editor = this;
78
+ if (this.formType in {"input":1, "textarea":1} && this.getValue() == this.oldValue)
79
+ { // Avoid request if no change is made
80
+ this.abort();
81
+ return true;
82
+ }
83
+ this.isNil = false;
84
+ editor.ajax({
85
+ "type" : "post",
86
+ "dataType" : "text",
87
+ "data" : editor.requestData(),
88
+ "success" : function(data){ editor.loadSuccessCallback(data); },
89
+ "error" : function(request, error){ editor.loadErrorCallback(request, error); }
90
+ });
91
+ if (this.formType == "select") {
92
+ var value = this.getValue();
93
+ this.previousCollectionValue = value;
94
+
95
+ jQuery.each(this.values, function(i, v) {
96
+ if (value == v[0]) {
97
+ editor.element.html(v[1]);
98
+ }
99
+ }
100
+ );
101
+ } else if (this.formType == "checkbox") {
102
+ editor.element.html(this.getValue() ? this.values[1] : this.values[0]);
103
+ } else {
104
+ if (this.getValue() !== "") {
105
+ editor.element.text(this.getValue());
106
+ } else {
107
+ editor.element.html(this.nil);
108
+ }
109
+ }
110
+ editor.element.trigger(jQuery.Event("best_in_place:update"));
111
+ },
112
+
113
+ activateForm : function() {
114
+ alert("The form was not properly initialized. activateForm is unbound");
115
+ },
116
+
117
+ // Helper Functions ////////////////////////////////////////////////////////
118
+
119
+ initOptions : function() {
120
+ // Try parent supplied info
121
+ var self = this;
122
+ self.element.parents().each(function(){
123
+ $parent = jQuery(this);
124
+ self.url = self.url || $parent.attr("data-url");
125
+ self.collection = self.collection || $parent.attr("data-collection");
126
+ self.formType = self.formType || $parent.attr("data-type");
127
+ self.objectName = self.objectName || $parent.attr("data-object");
128
+ self.attributeName = self.attributeName || $parent.attr("data-attribute");
129
+ self.activator = self.activator || $parent.attr("data-activator");
130
+ self.okButton = self.okButton || $parent.attr("data-ok-button");
131
+ self.cancelButton = self.cancelButton || $parent.attr("data-cancel-button");
132
+ self.nil = self.nil || $parent.attr("data-nil");
133
+ self.inner_class = self.inner_class || $parent.attr("data-inner-class");
134
+ self.html_attrs = self.html_attrs || $parent.attr("data-html-attrs");
135
+ self.original_content = self.original_content || $parent.attr("data-original-content");
136
+ self.collectionValue = self.collectionValue || $parent.attr("data-value");
137
+ });
138
+
139
+ // Try Rails-id based if parents did not explicitly supply something
140
+ self.element.parents().each(function(){
141
+ var res = this.id.match(/^(\w+)_(\d+)$/i);
142
+ if (res) {
143
+ self.objectName = self.objectName || res[1];
144
+ }
145
+ });
146
+
147
+ // Load own attributes (overrides all others)
148
+ self.url = self.element.attr("data-url") || self.url || document.location.pathname;
149
+ self.collection = self.element.attr("data-collection") || self.collection;
150
+ self.formType = self.element.attr("data-type") || self.formtype || "input";
151
+ self.objectName = self.element.attr("data-object") || self.objectName;
152
+ self.attributeName = self.element.attr("data-attribute") || self.attributeName;
153
+ self.activator = self.element.attr("data-activator") || self.element;
154
+ self.okButton = self.element.attr("data-ok-button") || self.okButton;
155
+ self.cancelButton = self.element.attr("data-cancel-button") || self.cancelButton;
156
+ self.nil = self.element.attr("data-nil") || self.nil || "-";
157
+ self.inner_class = self.element.attr("data-inner-class") || self.inner_class || null;
158
+ self.html_attrs = self.element.attr("data-html-attrs") || self.html_attrs;
159
+ self.original_content = self.element.attr("data-original-content") || self.original_content;
160
+ self.collectionValue = self.element.attr("data-value") || self.collectionValue;
161
+
162
+ if (!self.element.attr("data-sanitize")) {
163
+ self.sanitize = true;
164
+ }
165
+ else {
166
+ self.sanitize = (self.element.attr("data-sanitize") == "true");
167
+ }
168
+
169
+ if (!self.element.attr("data-use-confirm")) {
170
+ self.useConfirm = true;
171
+ } else {
172
+ self.useConfirm = (self.element.attr("data-use-confirm") != "false");
173
+ }
174
+
175
+ if ((self.formType == "select" || self.formType == "checkbox") && self.collection !== null)
176
+ {
177
+ self.values = jQuery.parseJSON(self.collection);
178
+ }
179
+
180
+ },
181
+
182
+ bindForm : function() {
183
+ this.activateForm = BestInPlaceEditor.forms[this.formType].activateForm;
184
+ this.getValue = BestInPlaceEditor.forms[this.formType].getValue;
185
+ },
186
+
187
+ initNil: function() {
188
+ if (this.element.html() === "")
189
+ {
190
+ this.isNil = true;
191
+ this.element.html(this.nil);
192
+ }
193
+ },
194
+
195
+ getValue : function() {
196
+ alert("The form was not properly initialized. getValue is unbound");
197
+ },
198
+
199
+ // Trim and Strips HTML from text
200
+ sanitizeValue : function(s) {
201
+ return jQuery.trim(s);
202
+ },
203
+
204
+ /* Generate the data sent in the POST request */
205
+ requestData : function() {
206
+ // To prevent xss attacks, a csrf token must be defined as a meta attribute
207
+ csrf_token = jQuery('meta[name=csrf-token]').attr('content');
208
+ csrf_param = jQuery('meta[name=csrf-param]').attr('content');
209
+
210
+ var data = "_method=put";
211
+ data += "&" + this.objectName + '[' + this.attributeName + ']=' + encodeURIComponent(this.getValue());
212
+
213
+ if (csrf_param !== undefined && csrf_token !== undefined) {
214
+ data += "&" + csrf_param + "=" + encodeURIComponent(csrf_token);
215
+ }
216
+ return data;
217
+ },
218
+
219
+ ajax : function(options) {
220
+ options.url = this.url;
221
+ options.beforeSend = function(xhr){ xhr.setRequestHeader("Accept", "application/json"); };
222
+ return jQuery.ajax(options);
223
+ },
224
+
225
+ // Handlers ////////////////////////////////////////////////////////////////
226
+
227
+ loadSuccessCallback : function(data) {
228
+ data = $.trim(data);
229
+
230
+ if(data && data!=""){
231
+ var response = jQuery.parseJSON(jQuery.trim(data));
232
+ if (response !== null && response.hasOwnProperty("display_as")) {
233
+ this.element.attr("data-original-content", this.element.text());
234
+ this.original_content = this.element.text();
235
+ this.element.html(response["display_as"]);
236
+ }
237
+ this.element.trigger(jQuery.Event("best_in_place:success"), data);
238
+ this.element.trigger(jQuery.Event("ajax:success"), data);
239
+ }
240
+
241
+ // Binding back after being clicked
242
+ jQuery(this.activator).bind('click', {editor: this}, this.clickHandler);
243
+ this.element.trigger(jQuery.Event("best_in_place:deactivate"));
244
+
245
+ if (this.collectionValue !== null && this.formType == "select") {
246
+ this.collectionValue = this.previousCollectionValue;
247
+ this.previousCollectionValue = null;
248
+ }
249
+ },
250
+
251
+ loadErrorCallback : function(request, error) {
252
+ this.element.html(this.oldValue);
253
+
254
+ this.element.trigger(jQuery.Event("best_in_place:error"), [request, error]);
255
+ this.element.trigger(jQuery.Event("ajax:error"), request, error);
256
+
257
+ // Binding back after being clicked
258
+ jQuery(this.activator).bind('click', {editor: this}, this.clickHandler);
259
+ this.element.trigger(jQuery.Event("best_in_place:deactivate"));
260
+ },
261
+
262
+ clickHandler : function(event) {
263
+ event.preventDefault();
264
+ event.data.editor.activate();
265
+ },
266
+
267
+ setHtmlAttributes : function() {
268
+ var formField = this.element.find(this.formType);
269
+
270
+ if(this.html_attrs){
271
+ var attrs = jQuery.parseJSON(this.html_attrs);
272
+ for(var key in attrs){
273
+ formField.attr(key, attrs[key]);
274
+ }
275
+ }
276
+ }
277
+ };
278
+
279
+
280
+ // Button cases:
281
+ // If no buttons, then blur saves, ESC cancels
282
+ // If just Cancel button, then blur saves, ESC or clicking Cancel cancels (careful of blur event!)
283
+ // If just OK button, then clicking OK saves (careful of blur event!), ESC or blur cancels
284
+ // If both buttons, then clicking OK saves, ESC or clicking Cancel or blur cancels
285
+ BestInPlaceEditor.forms = {
286
+ "input" : {
287
+ activateForm : function() {
288
+ var output = jQuery(document.createElement('form'))
289
+ .addClass('form_in_place')
290
+ .attr('action', 'javascript:void(0);')
291
+ .attr('style', 'display:inline');
292
+ var input_elt = jQuery(document.createElement('input'))
293
+ .attr('type', 'text')
294
+ .attr('name', this.attributeName)
295
+ .val(this.display_value);
296
+ if(this.inner_class !== null) {
297
+ input_elt.addClass(this.inner_class);
298
+ }
299
+ output.append(input_elt);
300
+ if(this.okButton) {
301
+ output.append(
302
+ jQuery(document.createElement('input'))
303
+ .attr('type', 'submit')
304
+ .attr('value', this.okButton)
305
+ )
306
+ }
307
+ if(this.cancelButton) {
308
+ output.append(
309
+ jQuery(document.createElement('input'))
310
+ .attr('type', 'button')
311
+ .attr('value', this.cancelButton)
312
+ )
313
+ }
314
+
315
+ this.element.html(output);
316
+ this.setHtmlAttributes();
317
+ this.element.find("input[type='text']")[0].select();
318
+ this.element.find("form").bind('submit', {editor: this}, BestInPlaceEditor.forms.input.submitHandler);
319
+ if (this.cancelButton) {
320
+ this.element.find("input[type='button']").bind('click', {editor: this}, BestInPlaceEditor.forms.input.cancelButtonHandler);
321
+ }
322
+ this.element.find("input[type='text']").bind('blur', {editor: this}, BestInPlaceEditor.forms.input.inputBlurHandler);
323
+ this.element.find("input[type='text']").bind('keyup', {editor: this}, BestInPlaceEditor.forms.input.keyupHandler);
324
+ this.blurTimer = null;
325
+ this.userClicked = false;
326
+ },
327
+
328
+ getValue : function() {
329
+ return this.sanitizeValue(this.element.find("input").val());
330
+ },
331
+
332
+ // When buttons are present, use a timer on the blur event to give precedence to clicks
333
+ inputBlurHandler : function(event) {
334
+ if (event.data.editor.okButton) {
335
+ event.data.editor.blurTimer = setTimeout(function () {
336
+ if (!event.data.editor.userClicked) {
337
+ event.data.editor.abort();
338
+ }
339
+ }, 500);
340
+ } else {
341
+ if (event.data.editor.cancelButton) {
342
+ event.data.editor.blurTimer = setTimeout(function () {
343
+ if (!event.data.editor.userClicked) {
344
+ event.data.editor.update();
345
+ }
346
+ }, 500);
347
+ } else {
348
+ event.data.editor.update();
349
+ }
350
+ }
351
+ },
352
+
353
+ submitHandler : function(event) {
354
+ event.data.editor.userClicked = true;
355
+ clearTimeout(event.data.editor.blurTimer);
356
+ event.data.editor.update();
357
+ },
358
+
359
+ cancelButtonHandler : function(event) {
360
+ event.data.editor.userClicked = true;
361
+ clearTimeout(event.data.editor.blurTimer);
362
+ event.data.editor.abort();
363
+ event.stopPropagation(); // Without this, click isn't handled
364
+ },
365
+
366
+ keyupHandler : function(event) {
367
+ if (event.keyCode == 27) {
368
+ event.data.editor.abort();
369
+ }
370
+ }
371
+ },
372
+
373
+ "date" : {
374
+ activateForm : function() {
375
+ var that = this,
376
+ output = jQuery(document.createElement('form'))
377
+ .addClass('form_in_place')
378
+ .attr('action', 'javascript:void(0);')
379
+ .attr('style', 'display:inline'),
380
+ input_elt = jQuery(document.createElement('input'))
381
+ .attr('type', 'text')
382
+ .attr('name', this.attributeName)
383
+ .attr('value', this.sanitizeValue(this.display_value));
384
+ if(this.inner_class !== null) {
385
+ input_elt.addClass(this.inner_class);
386
+ }
387
+ output.append(input_elt)
388
+
389
+ this.element.html(output);
390
+ this.setHtmlAttributes();
391
+ this.element.find('input')[0].select();
392
+ this.element.find("form").bind('submit', {editor: this}, BestInPlaceEditor.forms.input.submitHandler);
393
+ this.element.find("input").bind('keyup', {editor: this}, BestInPlaceEditor.forms.input.keyupHandler);
394
+
395
+ this.element.find('input')
396
+ .datepicker({
397
+ onClose: function() {
398
+ that.update();
399
+ }
400
+ })
401
+ .datepicker('show');
402
+ },
403
+
404
+ getValue : function() {
405
+ return this.sanitizeValue(this.element.find("input").val());
406
+ },
407
+
408
+ submitHandler : function(event) {
409
+ event.data.editor.update();
410
+ },
411
+
412
+ keyupHandler : function(event) {
413
+ if (event.keyCode == 27) {
414
+ event.data.editor.abort();
415
+ }
416
+ }
417
+ },
418
+
419
+ "select" : {
420
+ activateForm : function() {
421
+ var output = jQuery(document.createElement('form'))
422
+ .attr('action', 'javascript:void(0)')
423
+ .attr('style', 'display:inline');
424
+ selected = '',
425
+ oldValue = this.oldValue,
426
+ select_elt = jQuery(document.createElement('select')),
427
+ currentCollectionValue = this.collectionValue;
428
+
429
+ jQuery.each(this.values, function (index, value) {
430
+ var option_elt = jQuery(document.createElement('option'))
431
+ // .attr('value', value[0])
432
+ .val(value[0])
433
+ .html(value[1]);
434
+ if(value[0] == currentCollectionValue) {
435
+ option_elt.attr('selected', 'selected');
436
+ }
437
+ select_elt.append(option_elt);
438
+ });
439
+ output.append(select_elt);
440
+
441
+ this.element.html(output);
442
+ this.setHtmlAttributes();
443
+ this.element.find("select").bind('change', {editor: this}, BestInPlaceEditor.forms.select.blurHandler);
444
+ this.element.find("select").bind('blur', {editor: this}, BestInPlaceEditor.forms.select.blurHandler);
445
+ this.element.find("select").bind('keyup', {editor: this}, BestInPlaceEditor.forms.select.keyupHandler);
446
+ this.element.find("select")[0].focus();
447
+ },
448
+
449
+ getValue : function() {
450
+ return this.sanitizeValue(this.element.find("select").val());
451
+ // return this.element.find("select").val();
452
+ },
453
+
454
+ blurHandler : function(event) {
455
+ event.data.editor.update();
456
+ },
457
+
458
+ keyupHandler : function(event) {
459
+ if (event.keyCode == 27) event.data.editor.abort();
460
+ }
461
+ },
462
+
463
+ "checkbox" : {
464
+ activateForm : function() {
465
+ this.collectionValue = !this.getValue();
466
+ this.setHtmlAttributes();
467
+ this.update();
468
+ },
469
+
470
+ getValue : function() {
471
+ return this.collectionValue;
472
+ }
473
+ },
474
+
475
+ "textarea" : {
476
+ activateForm : function() {
477
+ // grab width and height of text
478
+ width = this.element.css('width');
479
+ height = this.element.css('height');
480
+
481
+ // construct form
482
+ var output = jQuery(document.createElement('form'))
483
+ .attr('action', 'javascript:void(0)')
484
+ .attr('style', 'display:inline')
485
+ .append(jQuery(document.createElement('textarea'))
486
+ .val(this.sanitizeValue(this.display_value)));
487
+ if(this.okButton) {
488
+ output.append(
489
+ jQuery(document.createElement('input'))
490
+ .attr('type', 'submit')
491
+ .attr('value', this.okButton)
492
+ );
493
+ }
494
+ if(this.cancelButton) {
495
+ output.append(
496
+ jQuery(document.createElement('input'))
497
+ .attr('type', 'button')
498
+ .attr('value', this.cancelButton)
499
+ )
500
+ }
501
+
502
+ this.element.html(output);
503
+ this.setHtmlAttributes();
504
+
505
+ // set width and height of textarea
506
+ jQuery(this.element.find("textarea")[0]).css({ 'min-width': width, 'min-height': height });
507
+ jQuery(this.element.find("textarea")[0]).elastic();
508
+
509
+ this.element.find("textarea")[0].focus();
510
+ this.element.find("form").bind('submit', {editor: this}, BestInPlaceEditor.forms.textarea.submitHandler);
511
+ if (this.cancelButton) {
512
+ this.element.find("input[type='button']").bind('click', {editor: this}, BestInPlaceEditor.forms.textarea.cancelButtonHandler);
513
+ }
514
+ this.element.find("textarea").bind('blur', {editor: this}, BestInPlaceEditor.forms.textarea.blurHandler);
515
+ this.element.find("textarea").bind('keyup', {editor: this}, BestInPlaceEditor.forms.textarea.keyupHandler);
516
+ this.blurTimer = null;
517
+ this.userClicked = false;
518
+ },
519
+
520
+ getValue : function() {
521
+ return this.sanitizeValue(this.element.find("textarea").val());
522
+ },
523
+
524
+ // When buttons are present, use a timer on the blur event to give precedence to clicks
525
+ blurHandler : function(event) {
526
+ if (event.data.editor.okButton) {
527
+ event.data.editor.blurTimer = setTimeout(function () {
528
+ if (!event.data.editor.userClicked) {
529
+ event.data.editor.abortIfConfirm();
530
+ }
531
+ }, 500);
532
+ } else {
533
+ if (event.data.editor.cancelButton) {
534
+ event.data.editor.blurTimer = setTimeout(function () {
535
+ if (!event.data.editor.userClicked) {
536
+ event.data.editor.update();
537
+ }
538
+ }, 500);
539
+ } else {
540
+ event.data.editor.update();
541
+ }
542
+ }
543
+ },
544
+
545
+ submitHandler : function(event) {
546
+ event.data.editor.userClicked = true;
547
+ clearTimeout(event.data.editor.blurTimer);
548
+ event.data.editor.update();
549
+ },
550
+
551
+ cancelButtonHandler : function(event) {
552
+ event.data.editor.userClicked = true;
553
+ clearTimeout(event.data.editor.blurTimer);
554
+ event.data.editor.abortIfConfirm();
555
+ event.stopPropagation(); // Without this, click isn't handled
556
+ },
557
+
558
+ keyupHandler : function(event) {
559
+ if (event.keyCode == 27) {
560
+ event.data.editor.abortIfConfirm();
561
+ }
562
+ }
563
+ }
564
+ };
565
+
566
+ jQuery.fn.best_in_place = function() {
567
+
568
+ function setBestInPlace(element) {
569
+ if (!element.data('bestInPlaceEditor')) {
570
+ element.data('bestInPlaceEditor', new BestInPlaceEditor(element));
571
+ return true;
572
+ }
573
+ }
574
+
575
+ jQuery(this.context).delegate(this.selector, 'click', function () {
576
+ var el = jQuery(this);
577
+ if (setBestInPlace(el))
578
+ el.click();
579
+ });
580
+
581
+ this.each(function () {
582
+ setBestInPlace(jQuery(this));
583
+ });
584
+
585
+ return this;
586
+ };
587
+
588
+
589
+
590
+ /**
591
+ * @name Elastic
592
+ * @descripton Elastic is Jquery plugin that grow and shrink your textareas automaticliy
593
+ * @version 1.6.5
594
+ * @requires Jquery 1.2.6+
595
+ *
596
+ * @author Jan Jarfalk
597
+ * @author-email jan.jarfalk@unwrongest.com
598
+ * @author-website http://www.unwrongest.com
599
+ *
600
+ * @licens MIT License - http://www.opensource.org/licenses/mit-license.php
601
+ */
602
+
603
+ (function(jQuery){
604
+ if (typeof jQuery.fn.elastic !== 'undefined') return;
605
+
606
+ jQuery.fn.extend({
607
+ elastic: function() {
608
+ // We will create a div clone of the textarea
609
+ // by copying these attributes from the textarea to the div.
610
+ var mimics = [
611
+ 'paddingTop',
612
+ 'paddingRight',
613
+ 'paddingBottom',
614
+ 'paddingLeft',
615
+ 'fontSize',
616
+ 'lineHeight',
617
+ 'fontFamily',
618
+ 'width',
619
+ 'fontWeight'];
620
+
621
+ return this.each( function() {
622
+
623
+ // Elastic only works on textareas
624
+ if ( this.type != 'textarea' ) {
625
+ return false;
626
+ }
627
+
628
+ var $textarea = jQuery(this),
629
+ $twin = jQuery('<div />').css({'position': 'absolute','display':'none','word-wrap':'break-word'}),
630
+ lineHeight = parseInt($textarea.css('line-height'),10) || parseInt($textarea.css('font-size'),'10'),
631
+ minheight = parseInt($textarea.css('height'),10) || lineHeight*3,
632
+ maxheight = parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE,
633
+ goalheight = 0,
634
+ i = 0;
635
+
636
+ // Opera returns max-height of -1 if not set
637
+ if (maxheight < 0) { maxheight = Number.MAX_VALUE; }
638
+
639
+ // Append the twin to the DOM
640
+ // We are going to meassure the height of this, not the textarea.
641
+ $twin.appendTo($textarea.parent());
642
+
643
+ // Copy the essential styles (mimics) from the textarea to the twin
644
+ i = mimics.length;
645
+ while(i--){
646
+ $twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));
647
+ }
648
+
649
+
650
+ // Sets a given height and overflow state on the textarea
651
+ function setHeightAndOverflow(height, overflow){
652
+ curratedHeight = Math.floor(parseInt(height,10));
653
+ if($textarea.height() != curratedHeight){
654
+ $textarea.css({'height': curratedHeight + 'px','overflow':overflow});
655
+
656
+ }
657
+ }
658
+
659
+
660
+ // This function will update the height of the textarea if necessary
661
+ function update() {
662
+
663
+ // Get curated content from the textarea.
664
+ var textareaContent = $textarea.val().replace(/&/g,'&amp;').replace(/ /g, '&nbsp;').replace(/<|>/g, '&gt;').replace(/\n/g, '<br />');
665
+
666
+ // Compare curated content with curated twin.
667
+ var twinContent = $twin.html().replace(/<br>/ig,'<br />');
668
+
669
+ if(textareaContent+'&nbsp;' != twinContent){
670
+
671
+ // Add an extra white space so new rows are added when you are at the end of a row.
672
+ $twin.html(textareaContent+'&nbsp;');
673
+
674
+ // Change textarea height if twin plus the height of one line differs more than 3 pixel from textarea height
675
+ if(Math.abs($twin.height() + lineHeight - $textarea.height()) > 3){
676
+
677
+ var goalheight = $twin.height()+lineHeight;
678
+ if(goalheight >= maxheight) {
679
+ setHeightAndOverflow(maxheight,'auto');
680
+ } else if(goalheight <= minheight) {
681
+ setHeightAndOverflow(minheight,'hidden');
682
+ } else {
683
+ setHeightAndOverflow(goalheight,'hidden');
684
+ }
685
+
686
+ }
687
+
688
+ }
689
+
690
+ }
691
+
692
+ // Hide scrollbars
693
+ $textarea.css({'overflow':'hidden'});
694
+
695
+ // Update textarea size on keyup, change, cut and paste
696
+ $textarea.bind('keyup change cut paste', function(){
697
+ update();
698
+ });
699
+
700
+ // Compact textarea on blur
701
+ // Lets animate this....
702
+ $textarea.bind('blur',function(){
703
+ if($twin.height() < maxheight){
704
+ if($twin.height() > minheight) {
705
+ $textarea.height($twin.height());
706
+ } else {
707
+ $textarea.height(minheight);
708
+ }
709
+ }
710
+ });
711
+
712
+ // And this line is to catch the browser paste event
713
+ $textarea.on("input paste", function(e){ setTimeout( update, 250); });
714
+
715
+ // Run update once when elastic is initialized
716
+ update();
717
+
718
+ });
719
+
720
+ }
721
+ });
722
+ })(jQuery);