stay 0.1.3.5 → 0.1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -10,7 +10,7 @@ Installation **stay** is simple. In Rails > 3.0 just add **stay** in your Gemfil
10
10
  gem "tinymce-rails"
11
11
  gem "stay"
12
12
  ```
13
-
13
+
14
14
  Then include the following Javascript include into your assets/javascript.js.
15
15
 
16
16
  ```ruby
@@ -18,7 +18,7 @@ Then include the following Javascript include into your assets/javascript.js.
18
18
  //= require tinymce-jquery
19
19
  //= require stay
20
20
  ```
21
-
21
+
22
22
  Then add javascript calling in your Javascript file
23
23
 
24
24
  ```javascript
@@ -29,14 +29,38 @@ Then add javascript calling in your Javascript file
29
29
 
30
30
  or if you're using CoffeScript use
31
31
 
32
- ```
32
+ ```
33
33
  jQuery ->
34
34
  jQuery(".stay").stay()
35
35
  ```
36
-
36
+
37
+ ### Javascript Callback
38
+ **Stay** also accept callback arguments, currently **Stay** only have 3 callback attributes
39
+ - beforeSend
40
+ - onError
41
+ - onSuccess
42
+
43
+ Below is the examples how to implement **Stay** with callbacks, call this javascript when initalize **Stay**
44
+
45
+ ```javascript
46
+ jQuery(".stay").stay({
47
+ beforeSend: function() {
48
+ // Do whatever you want before data is being sent to server, e.g. display the loading bar
49
+ },
50
+ onError: function(errs) {
51
+ // This method takes one argument contains array of errors similar with Obj.errors in Rails
52
+ return $.each(errs, function(key) {
53
+ return alert(key + " " + errs[key]);
54
+ });
55
+ },
56
+ onSuccess: function() {
57
+ // Do whatever you want when server said OK, e.g. hide the loading bar
58
+ }
59
+ });
60
+ ```
37
61
  ## Usage
38
62
 
39
- ### call this in your view
63
+ ### In your View
40
64
 
41
65
  ```
42
66
  stay object, field, OPTIONS{}
@@ -49,7 +73,7 @@ Example:
49
73
  ```ruby
50
74
  stay @user, :name, type: :text_field
51
75
  ```
52
-
76
+
53
77
  As default **type** will take :text_field, *for now* you can only pass :text_field, :text_area, :tiny_mce
54
78
 
55
79
  **Stay** support sub-resources routes, therefore you can pass array into **object** param
@@ -59,7 +83,7 @@ Example:
59
83
  ```ruby
60
84
  stay [@user, @article], :title, type: :text_area
61
85
  ```
62
-
86
+
63
87
  You can use TinyMCE as text editor, just pass :tiny_mce to **type**
64
88
 
65
89
  Example:
@@ -67,19 +91,19 @@ Example:
67
91
  ```ruby
68
92
  stay [@user, @article], :body, type: :tiny_mce
69
93
  ```
70
-
94
+
71
95
  This will use TinyMCE editor with "simple" theme, you can also change this theme by using
72
96
 
73
97
  ```ruby
74
98
  stay [@user, @article], :body, type: [:tiny_mce, "advanced"]
75
- ```
99
+ ```
76
100
  Now you can pass width and height of tinyMCE using CSS string format
77
101
 
78
102
  Example
79
103
 
80
104
  ```ruby
81
105
  stay [@user, @article], :body, type: [:tiny_mce, "advanced", "100%", "200px"]
82
- ```
106
+ ```
83
107
 
84
108
  Remember to put width and height respectively
85
109
 
@@ -93,7 +117,7 @@ Example:
93
117
  ```ruby
94
118
  stay [@user, @article], :body, type: [:tiny_mce, "advanced"], activator: "#id_of_activator"
95
119
  ```
96
-
120
+
97
121
  To use external submit button, just passed **submitter:** followed by id of HTML element id.
98
122
  Note that this external submit button will be hidden by **Stay** and will be visible once **Stay** editor is visible (stay triggered).
99
123
 
@@ -108,7 +132,7 @@ Due to **Stay** default submit event is triggered by blur **Stay** will raise Ar
108
132
  ```ruby
109
133
  stay [@user, @article], :body, type: [:tiny_mce, "advanced"], activator: "#id_of_activator", submitter: "#id_of_submit_button", canceller: "#id_of_cancel_button"
110
134
  ```
111
-
135
+
112
136
  Example of complete use:
113
137
 
114
138
  ```ruby
@@ -117,10 +141,10 @@ Example of complete use:
117
141
  <%= link_to "Click me to submit", "#", id: "submit_here" #this will be hide by Stay %>
118
142
  ```
119
143
 
120
- ### call this in your controller
144
+ ### In your Controller
121
145
 
122
146
  stay_response object
123
-
147
+
124
148
  Example of complete use:
125
149
 
126
150
  ```ruby
@@ -137,10 +161,19 @@ Example of complete use:
137
161
  end
138
162
  ```
139
163
 
140
- ### JSON callback
141
- When Rails Model refuse to save object due to validation errors, **Stay** will carry this errors in its response text, you can evaluate the error by passing this response text as JSON and call the errors properties. You can do whatever you wish with the errors messages.
164
+ ## Changelog
165
+ Just some good releases changelog:
142
166
 
143
- ```javascript
144
- cb_data = jQuery.parseJSON(data.responseText);
145
- errs = cb_data.errors; //this contains array of errors
146
- ```
167
+ **v0.1.4.0**
168
+ - Complete rewrite Javascript, made it modular
169
+ - Add ability to initialize Stay with callback
170
+
171
+ **v0.1.3.5**
172
+ - Add Canceller
173
+ - Hide Submitter and Canceller until Stay is activated
174
+
175
+ **v0.1.3.4.4**
176
+ - Add error callback to the JSON
177
+
178
+ **v0.1.0.0**
179
+ - Initial release, February 4th 2012
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('stay', '0.1.3.5') do |p|
5
+ Echoe.new('stay', '0.1.4.0') do |p|
6
6
  p.description = "Form ajax helper with tinymce support"
7
7
  p.summary = "Helper for building ajax form, inspired by best_in_place, with some modifications, support tinymce as editor"
8
8
  p.url = "http://github.com/tejanium/stay"
@@ -1,140 +1,175 @@
1
- jQuery.fn.stay = function(){
2
- this.each(function(){
3
- new Stay(this);
1
+ var Stay, isset;
2
+ jQuery.fn.stay = function(args) {
3
+ return this.each(function() {
4
+ return new Stay(this, args);
4
5
  });
5
- }
6
-
7
- jQuery.fn.disable = function(){
8
- this.attr("disabled", "")
9
- }
10
-
11
- jQuery.fn.enable = function(){
12
- this.removeAttr("disabled")
13
- }
14
-
15
- function Stay(e){
16
- this.init(e);
17
- }
18
-
19
- Stay.prototype = {
20
- init: function(e){
21
- var self = this;
22
-
23
- self.stayWrapper = jQuery(e);
24
- self.stayObject = self.stayWrapper.find(".stay-object");
25
- self.stayForm = self.stayWrapper.find(".stay-form");
26
- self.stayFormForm = self.stayForm.find("form");
27
- self.stayTinyMCE = self.stayForm.find(".stay-input.stay-tiny-mce")
28
- self.tinyMCEUsed = self.stayTinyMCE.length > 0;
29
- self.stayFormInput = self.stayFormForm.find(".stay-input");
30
-
31
- self.tinyMCETheme = self.stayWrapper.data("tinymce-theme") || "simple"
32
-
33
- self.activator_id = self.stayWrapper.data("activator")
34
- self.activator = (typeof self.activator_id === "undefined") ? self.stayObject : jQuery(self.activator_id)
35
-
36
- self.submitter_id = this.stayWrapper.data("submitter")
37
- self.submitter = (typeof self.submitter_id === "undefined") ? self.stayFormInput : jQuery(self.submitter_id)
38
-
39
- self.canceller_id = this.stayWrapper.data("canceller")
40
-
41
- if(typeof self.submitter_id !== "undefined"){jQuery(self.submitter_id).hide()}
42
- if(typeof self.canceller_id !== "undefined"){jQuery(self.canceller_id).hide()}
43
-
44
- if(self.tinyMCEUsed){
45
- this.stayTinyMCE.tinymce({
46
- theme: self.tinyMCETheme,
47
- setup : function(ed) {
48
- ed.onInit.add(function(ed, evt) {
49
- var dom = ed.dom;
50
- var doc = tinymce.isGecko ? ed.getDoc() : ed.getWin();
51
- if(typeof self.submitter_id === "undefined"){
52
- tinymce.dom.Event.add(doc, 'blur', function(e) {
53
- self.stayFormForm.submit();
54
- });
55
- }else{
56
- self.submitter.click(function(){
57
- self.stayFormForm.submit();
58
- });
59
- }
60
- if(typeof self.canceller_id === "undefined"){
61
- tinymce.dom.Event.add(doc, 'keydown', function(e) {
62
- if (e.keyCode == 27) {
63
- self.reShowObject();
64
- }
65
- });
66
- }else{
67
- jQuery(self.canceller_id).click(function(){
68
- self.reShowObject();
69
- });
70
- }
71
- });
72
- }
73
- });
6
+ };
7
+ jQuery.fn.disable = function() {
8
+ return this.attr("disabled", "disabled");
9
+ };
10
+ jQuery.fn.enable = function() {
11
+ return this.removeAttr("disabled");
12
+ };
13
+ isset = function(o) {
14
+ return typeof o !== "undefined";
15
+ };
16
+ Stay = (function() {
17
+ function Stay(e, args) {
18
+ var activator, canceller, submitter;
19
+ this.hooks = args || {};
20
+ this.stayWrapper = jQuery(e);
21
+ this.stayObjectContainer = this.stayWrapper.find(".stay-object");
22
+ this.stayFormContainer = this.stayWrapper.find(".stay-form");
23
+ this.stayActualForm = this.stayFormContainer.find("form");
24
+ this.stayFormInput = this.stayActualForm.find(".stay-input");
25
+ this.usingTinyMCE = this.stayFormInput.hasClass("stay-tiny-mce");
26
+ activator = this.stayWrapper.data("activator");
27
+ this.usingActivator = isset(activator);
28
+ this.activator = (this.usingActivator ? jQuery(activator) : this.stayObjectContainer);
29
+ submitter = this.stayWrapper.data("submitter");
30
+ if (this.usingSubmitter = isset(submitter)) {
31
+ this.submitter = jQuery(submitter);
32
+ this.submitter.hide();
33
+ } else {
34
+ this.submitter = this.stayFormInput;
35
+ }
36
+ canceller = this.stayWrapper.data("canceller");
37
+ if (this.usingCanceller = isset(canceller)) {
38
+ this.canceller = jQuery(canceller);
39
+ this.canceller.hide();
40
+ }
41
+ this.bindActivator();
42
+ if (this.usingTinyMCE) {
43
+ this.initTinyMCE();
44
+ } else {
45
+ this.initDefault();
74
46
  }
75
- self.bindTrigger();
76
- self.bindCallback();
77
-
78
- },
79
-
80
- bindTrigger: function(){
81
- var self = this;
82
-
83
- self.activator.click(function(){
84
- self.stayObject.hide();
85
- self.stayForm.show();
86
- if(typeof self.submitter_id !== "undefined"){jQuery(self.submitter_id).show()}
87
- if(typeof self.canceller_id !== "undefined"){jQuery(self.canceller_id).show()}
88
- if(self.tinyMCEUsed){
89
- self.stayTinyMCE.tinymce().focus();
90
- }else{
91
- self.stayFormInput.focus();
47
+ this.bindCallback();
48
+ }
49
+ Stay.prototype.initTinyMCE = function() {
50
+ var stay;
51
+ stay = this;
52
+ return stay.stayFormInput.tinymce({
53
+ theme: stay.stayWrapper.data("tinymce-theme") || "simple",
54
+ setup: function(ed) {
55
+ return ed.onInit.add(function(ed, ev) {
56
+ var doc;
57
+ doc = (tinymce.isGecko ? ed.getDoc() : ed.getWin());
58
+ stay.bindSubmitter(doc);
59
+ return stay.bindCanceller(doc);
60
+ });
61
+ }
62
+ });
63
+ };
64
+ Stay.prototype.initDefault = function() {
65
+ this.bindSubmitter();
66
+ return this.bindCanceller();
67
+ };
68
+ Stay.prototype.bindActivator = function() {
69
+ var stay;
70
+ stay = this;
71
+ return stay.activator.click(function() {
72
+ stay.hideObject();
73
+ if (stay.usingTinyMCE) {
74
+ return stay.stayFormInput.tinymce().focus();
75
+ } else {
76
+ return stay.stayFormInput.focus();
92
77
  }
93
78
  });
94
-
95
- if(!self.tinyMCEUsed){
96
- var ev = (typeof self.submitter_id === "undefined") ? "blur" : "click"
97
- self.submitter.bind(ev, function(){
98
- self.stayFormForm.submit();
99
- self.stayFormInput.disable();
79
+ };
80
+ Stay.prototype.bindSubmitter = function(doc) {
81
+ var stay;
82
+ stay = this;
83
+ if (stay.usingSubmitter) {
84
+ return stay.submitter.click(function() {
85
+ return stay.submitEvent();
100
86
  });
101
-
102
- if(typeof self.canceller_id === "undefined"){
103
- self.stayFormInput.keydown(function(e) {
104
- if (e.keyCode == 27) {
105
- self.reShowObject();
106
- }
107
- });
108
- }else{
109
- jQuery(self.canceller_id).click(function(){
110
- self.reShowObject();
87
+ } else {
88
+ if (isset(doc)) {
89
+ return tinymce.dom.Event.add(doc, "blur", function(e) {
90
+ return stay.submitEvent();
91
+ });
92
+ } else {
93
+ return stay.submitter.blur(function() {
94
+ return stay.submitEvent();
111
95
  });
112
96
  }
113
97
  }
114
- },
115
-
116
- bindCallback: function(){
117
- var self = this;
118
-
119
- self.stayFormForm.live("ajax:success", function(event, data, status, xhr){
120
- self.stayFormInput.val(data.input);
121
- self.stayObject.html(data.display);
122
- self.reShowObject();
98
+ };
99
+ Stay.prototype.bindCanceller = function(doc) {
100
+ var stay;
101
+ stay = this;
102
+ if (stay.usingCanceller) {
103
+ return stay.canceller.click(function() {
104
+ return stay.showObject();
105
+ });
106
+ } else {
107
+ if (isset(doc)) {
108
+ return tinymce.dom.Event.add(doc, "keydown", function(e) {
109
+ return stay.cancelEvent(e);
110
+ });
111
+ } else {
112
+ return stay.stayFormInput.keydown(function(e) {
113
+ return stay.cancelEvent(e);
114
+ });
115
+ }
116
+ }
117
+ };
118
+ Stay.prototype.bindCallback = function() {
119
+ var stay;
120
+ stay = this;
121
+ stay.stayActualForm.bind("ajax:success", function(e, data) {
122
+ stay.processCallback(data);
123
+ if (isset(stay.hooks.onSuccess)) {
124
+ return stay.hooks.onSuccess();
125
+ }
123
126
  });
124
-
125
- self.stayFormForm.live("ajax:error", function(event, data, status, xhr){
126
- var cb = jQuery.parseJSON(data.responseText);
127
- self.stayFormInput.val(cb.input || "");
128
- self.stayObject.html(cb.display || "");
129
- self.reShowObject();
127
+ return stay.stayActualForm.bind("ajax:error", function(e, data) {
128
+ var cb;
129
+ cb = jQuery.parseJSON(data.responseText);
130
+ stay.processCallback(cb);
131
+ if (isset(stay.hooks.onError)) {
132
+ return stay.hooks.onError(cb.errors);
133
+ }
130
134
  });
131
- },
132
-
133
- reShowObject: function(){
134
- this.stayForm.hide();
135
- if(typeof this.submitter_id !== "undefined"){jQuery(this.submitter_id).hide()}
136
- if(typeof this.canceller_id !== "undefined"){jQuery(this.canceller_id).hide()}
137
- this.stayObject.show();
138
- this.stayFormInput.enable();
139
- }
140
- };
135
+ };
136
+ Stay.prototype.processCallback = function(cb) {
137
+ this.stayFormInput.val(cb.input || "");
138
+ this.stayObjectContainer.html(cb.display || "");
139
+ return this.showObject();
140
+ };
141
+ Stay.prototype.hideObject = function() {
142
+ this.stayFormContainer.show();
143
+ if (this.usingSubmitter) {
144
+ this.submitter.show();
145
+ }
146
+ if (this.usingCanceller) {
147
+ this.canceller.show();
148
+ }
149
+ return this.stayObjectContainer.hide();
150
+ };
151
+ Stay.prototype.showObject = function() {
152
+ this.stayFormContainer.hide();
153
+ if (this.usingSubmitter) {
154
+ this.submitter.hide();
155
+ }
156
+ if (this.usingCanceller) {
157
+ this.canceller.hide();
158
+ }
159
+ this.stayObjectContainer.show();
160
+ return this.stayFormInput.enable();
161
+ };
162
+ Stay.prototype.cancelEvent = function(e) {
163
+ if (e.keyCode === 27) {
164
+ return this.showObject();
165
+ }
166
+ };
167
+ Stay.prototype.submitEvent = function(e) {
168
+ if (isset(this.hooks.beforeSend)) {
169
+ this.hooks.beforeSend();
170
+ }
171
+ this.stayActualForm.submit();
172
+ return this.stayFormInput.disable();
173
+ };
174
+ return Stay;
175
+ })();
data/stay.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "stay"
5
- s.version = "0.1.3.5"
5
+ s.version = "0.1.4.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Teja Sophista"]
9
- s.date = "2012-03-09"
9
+ s.date = "2012-03-10"
10
10
  s.description = "Form ajax helper with tinymce support"
11
11
  s.email = "tejanium@yahoo.com"
12
12
  s.extra_rdoc_files = ["README.md", "README.rdoc", "lib/assets/javascripts/stay.js", "lib/stay.rb", "lib/stay/controller_extensions.rb", "lib/stay/engine.rb", "lib/stay/helper.rb", "lib/stay/string_extensions.rb"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3.5
4
+ version: 0.1.4.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-03-09 00:00:00.000000000Z
12
+ date: 2012-03-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jquery-rails
16
- requirement: &28148600 !ruby/object:Gem::Requirement
16
+ requirement: &16640280 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *28148600
24
+ version_requirements: *16640280
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: tinymce-rails
27
- requirement: &28148140 !ruby/object:Gem::Requirement
27
+ requirement: &16639820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *28148140
35
+ version_requirements: *16639820
36
36
  description: Form ajax helper with tinymce support
37
37
  email: tejanium@yahoo.com
38
38
  executables: []