jqr-helpers 1.0.9 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -108,30 +108,65 @@
108
108
  return false;
109
109
  }
110
110
 
111
+ function ujsButtonClick(event) {
112
+ var element = $(this);
113
+ element.uniqueId(); // to store for later
114
+ if ($.rails.allowAction(element)) {
115
+ // largely copied from rails_jquery.js
116
+ var href = element.data('url');
117
+ var method = element.data('method');
118
+ var csrf_token = $('meta[name=csrf-token]').attr('content');
119
+ var csrf_param = $('meta[name=csrf-param]').attr('content');
120
+ var form = $('<form method="post" action="' + href + '"></form>');
121
+ var metadata_input =
122
+ '<input name="_method" value="' + method + '" type="hidden" />';
123
+
124
+ if (csrf_param !== undefined && csrf_token !== undefined) {
125
+ metadata_input += '<input name="' + csrf_param + '" value="' +
126
+ csrf_token + '" type="hidden" />';
127
+ }
128
+
129
+ form.hide().append(metadata_input).appendTo('body');
130
+ $(form).data(element.data()); // copy to form
131
+ $(form).data('remote', true);
132
+ $(form).addClass('ujs-ajax');
133
+ $(form).data('real-element', element.attr('id'));
134
+ form.submit();
135
+ }
136
+ event.preventDefault();
137
+ return false;
138
+ }
139
+
111
140
  function ujsAjaxBeforeSend() {
112
- if ($j(this).is('form')) {
113
- var submit = $j('input[type=submit]', this);
141
+ var element = $(this);
142
+ if (element.data('real-element')) {
143
+ element = $('#' + element.data('real-element'));
144
+ }
145
+ if (element.is('form')) {
146
+ var submit = $('input[type=submit]', element);
114
147
  if (submit.length)
115
148
  showThrobber(submit);
116
149
  else
117
- showThrobber(this);
150
+ showThrobber(element);
118
151
  }
119
152
  else {
120
- showThrobber(this);
153
+ showThrobber(element);
121
154
  }
122
- var disableElement = $(this);
123
- if ($(this).is('form'))
124
- disableElement = $('button, input[type=submit]', this).first();
125
- disableElement.attr('disabled', 'disabled');
155
+ if (element.is('form'))
156
+ element = $('button, input[type=submit]', element).first();
157
+ element.attr('disabled', 'disabled');
126
158
  }
127
159
 
128
160
  function ujsAjaxSuccess(evt, data, status, xhr) {
129
- hideThrobber(this);
130
- var disableElement = $(this);
131
- if ($(this).is('form'))
132
- disableElement = $('button, input[type=submit]', this).first();
133
- disableElement.attr('disabled', false);
134
161
  var element = $(this);
162
+ if (element.data('real-element')) {
163
+ element = $('#' + element.data('real-element'));
164
+ }
165
+ hideThrobber(element);
166
+ var disableElement = element;
167
+ if (element.is('form'))
168
+ disableElement = $('button, input[type=submit]', element).first();
169
+ disableElement.attr('disabled', false);
135
170
  var targetElement = element;
136
171
  // if this was sent from a dialog, close the dialog and look at the
137
172
  // element that opened it for update/append/delete callbacks.
@@ -164,7 +199,7 @@
164
199
  target.trigger('jqr.load');
165
200
  break;
166
201
  case 'delete':
167
- target.remove();
202
+ target.fadeOut(500, function() {$(this).remove()});
168
203
  break;
169
204
  }
170
205
  target.effect('highlight');
@@ -207,6 +242,7 @@
207
242
  var options = $(this).data('date-options');
208
243
  $(this).datepicker(options);
209
244
  });
245
+
210
246
  $('.ujs-button-set', event.target).each(function() {
211
247
  $(this).buttonset();
212
248
  });
@@ -237,6 +273,7 @@
237
273
  $(document).on('click', '.ujs-dialog', ujsDialogClick);
238
274
  $(document).on('click', '.ujs-dialog-close, .ujs-dialog-x',
239
275
  ujsDialogCloseClick);
276
+ $(document).on('click', '.ujs-ajax-button', ujsButtonClick);
240
277
  $(document).on('ajax:beforeSend', '.ujs-ajax', ujsAjaxBeforeSend);
241
278
  $(document).on('ajax:success', '.ujs-ajax', ujsAjaxSuccess);
242
279
  $(document).on('ajax:error', '.ujs-ajax', ujsAjaxError);
@@ -246,6 +283,7 @@
246
283
  $('body').live('jqr.load', ujsLoadPlugins);
247
284
  $('.ujs-dialog').live('click', ujsDialogClick);
248
285
  $('.ujs-dialog-close, .ujs-dialog-x').live('click', ujsDialogCloseClick);
286
+ $('.ujs-ajax-button').live('click', ujsButtonClick);
249
287
  $('.ujs-ajax').live('ajax:beforeSend', ujsAjaxBeforeSend);
250
288
  $('.ujs-ajax').live('ajax:success', ujsAjaxSuccess);
251
289
  $('.ujs-ajax').live('ajax:error', ujsAjaxError);
data/jqr-helpers.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'jqr-helpers'
3
3
  s.require_paths = %w(. lib lib/jqr-helpers)
4
- s.version = '1.0.9'
4
+ s.version = '1.0.10'
5
5
  s.date = '2013-11-19'
6
6
  s.summary = 'Helpers to print unobtrusive jQuery-UI tags.'
7
7
  s.description = <<-EOF
@@ -176,19 +176,25 @@ module JqrHelpers
176
176
  link_to body, url, options
177
177
  end
178
178
 
179
- # Create a button that fires off a jQuery Ajax request. This is basically
180
- # a wrapper around button_to :remote => true.
179
+ # Create a button that fires off a jQuery Ajax request. This does not use
180
+ # button_to, so it can be used inside forms.
181
181
  # @param body [String] the text/content that goes inside the tag.
182
182
  # @param url [String] the URL to connect to.
183
183
  # @param options [Hash] Ajax options - see above.
184
184
  # @return [String]
185
185
  def button_to_ajax(body, url, options={})
186
186
 
187
- options[:remote] = true
188
- options[:form] ||= {}
189
- options[:form].merge!(_process_ajax_options(options))
187
+ # Specifically do not add data-remote
188
+ options[:'data-method'] = options.delete(:method)
189
+ options[:'class'] ||= ''
190
+ options[:'class'] << ' ujs-ajax-button'
191
+ options[:'data-url'] = url
192
+ if options.key?(:confirm)
193
+ options[:'data-confirm'] = options.delete(:confirm)
194
+ end
195
+ options.merge!(_process_ajax_options(options))
190
196
 
191
- button_to body, url, options
197
+ content_tag :button, body, options
192
198
  end
193
199
 
194
200
  # Create a form tag that submits to an Ajax request. Basically a wrapper for
@@ -1,5 +1,5 @@
1
1
  module JqrHelpers
2
2
  module Rails
3
- VERSION = '1.0.9'
3
+ VERSION = '1.0.10'
4
4
  end
5
5
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jqr-helpers
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.9
5
+ version: 1.0.10
6
6
  platform: ruby
7
7
  authors:
8
8
  - Daniel Orner