jqr-helpers 1.0.0.beta1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme.md +128 -0
- data/app/assets/images/jqr-helpers/close.png +0 -0
- data/app/assets/images/jqr-helpers/throbber.gif +0 -0
- data/app/assets/javascripts/jqr-helpers.js +227 -0
- data/app/assets/stylesheets/jqr-helpers.css +21 -0
- data/jqr-helpers.gemspec +22 -0
- data/lib/jqr-helpers.rb +1 -0
- data/lib/jqr-helpers/helpers.rb +213 -0
- data/lib/jqr-helpers/install_generator.rb +25 -0
- data/lib/jqr-helpers/railtie.rb +12 -0
- data/lib/jqr-helpers/version.rb +5 -0
- metadata +16 -7
data/Readme.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
# Jqr-Helpers
|
2
|
+
|
3
|
+
`jqr-helpers` is a set of methods that create tags that are watched by unobtrusive JavaScript
|
4
|
+
events. It is primarily designed to cut down on JavaScript event handling and
|
5
|
+
callbacks and try to allow as much as possible to happen with Rails helpers.
|
6
|
+
|
7
|
+
The two main uses of these methods are to create dialogs and handle
|
8
|
+
Ajax requests without having to write JavaScript code. In particular, the options
|
9
|
+
available to the methods provide more support for the most common ways of using
|
10
|
+
these sorts of things in a web application environment.
|
11
|
+
|
12
|
+
Although there is some overlap with Rails 3 UJS methods, the important part
|
13
|
+
is the added options available. These make more assumptions than the built-in
|
14
|
+
helper methods, but are optimized to make it much easier to use them in
|
15
|
+
common scenarios. The existing methods are not altered.
|
16
|
+
|
17
|
+
jqr-helpers was developed using jQuery 1.10 and jQuery-UI 1.10, but it should
|
18
|
+
be compatible with earlier versions as well. The assumption is that all
|
19
|
+
required JS files are included, including jQuery, jQuery-UI, and the jquery-rails
|
20
|
+
UJS adapter. jqr-helpers does not attempt to include them itself.
|
21
|
+
|
22
|
+
## Using jqr-helpers ##
|
23
|
+
|
24
|
+
If you are running Rails >= 3.1, the required assets should be installed
|
25
|
+
automatically as part of the asset pipeline. You can require them as needed:
|
26
|
+
|
27
|
+
//= require jqr-helpers
|
28
|
+
*= require jqr-helpers
|
29
|
+
|
30
|
+
If you are running Rails 3.0, you can manually copy the JavaScript and CSS
|
31
|
+
into your public folders:
|
32
|
+
|
33
|
+
rails g jqr_helpers:install
|
34
|
+
|
35
|
+
## Helper Methods ##
|
36
|
+
|
37
|
+
* `link_to_dialog` - open a dialog when a link is clicked
|
38
|
+
* `button_to_dialog` - open a dialog when a button is clicked
|
39
|
+
* `confirm_button` - open a nice jQuery confirm dialog (rather than a built-in browser one)
|
40
|
+
* `link_to_remote_dialog` - open a remote dialog when a link is clicked (i.e. load
|
41
|
+
the dialog content from a remote route)
|
42
|
+
* `button_to_remote_dialog` - open a remote dialog when a button is clicked
|
43
|
+
* `link_to_ajax` - send an Ajax request when a link is clicked
|
44
|
+
* `button_to_ajax` - send an Ajax request when a button is clicked
|
45
|
+
* `form_tag_ajax` - send an Ajax request when a form is submitted
|
46
|
+
* `form_for_ajax` - ditto but using Rails's `form_for` helper
|
47
|
+
|
48
|
+
There are two sets of options that recur throughout the methods here:
|
49
|
+
|
50
|
+
## Dialog Options ##
|
51
|
+
|
52
|
+
These are parameters to pass to the `jQuery.dialog()` function.
|
53
|
+
See <http://api.jqueryui.com/dialog/>.
|
54
|
+
|
55
|
+
An extra custom option is `:title` - setting it to `false` will hide the
|
56
|
+
title bar.
|
57
|
+
|
58
|
+
Another thing to note is the special values for buttons. Usually the buttons
|
59
|
+
must have JavaScript callbacks, but 99% of the time you want the classic
|
60
|
+
OK and Cancel buttons. Passing `submit` and `close` as the values
|
61
|
+
of the buttons (or the values of the "click" attribute of the buttons)
|
62
|
+
will do just that - submit the form inside the dialog or close it.
|
63
|
+
|
64
|
+
Example:
|
65
|
+
|
66
|
+
button_to_dialog('my-dialog-id', 'Open Dialog', :buttons =>
|
67
|
+
{'OK' => 'submit', 'Cancel' => 'close'})
|
68
|
+
|
69
|
+
You can also use a special option, `:default_buttons => true`, as a shortcut
|
70
|
+
to the above buttons, since it's so common to have an OK and Cancel
|
71
|
+
button.
|
72
|
+
|
73
|
+
Another option is `:close_x => true` - this will print a green X at the top
|
74
|
+
right of the dialog. Generally this is used when `:title => false`.
|
75
|
+
|
76
|
+
Note about dialog ID - you can always pass in the special value `:next` for
|
77
|
+
this. This will use whatever element is just after the clicked element
|
78
|
+
for the dialog contents. This can be useful for printing simple dialogs inside a
|
79
|
+
foreach loop that shouldn't require a totally separate route + view.
|
80
|
+
|
81
|
+
Dialogs will by default be centered on the page and have a max height of 80%
|
82
|
+
the page height.
|
83
|
+
|
84
|
+
## Ajax Options ##
|
85
|
+
|
86
|
+
By default, the `options` parameter in the various `_to_ajax` functions are
|
87
|
+
passed into the underlying function (e.g. `link_to_ajax` will pass them to
|
88
|
+
`link_to`), but there is support for several special options as well.
|
89
|
+
|
90
|
+
*Selector options* will act on another element once the request is complete.
|
91
|
+
Selectors can be IDs (`#selector`), or classes (`.selector`).
|
92
|
+
A class selector will be interpreted as an *ancestor* (parent) of the
|
93
|
+
element that sent the request that has the given class. So e.g. if you
|
94
|
+
are using `button_to_ajax`, giving `:update => '.my-parent'` will look for
|
95
|
+
an ancestor of the button tag with the class of `my-parent`.
|
96
|
+
|
97
|
+
* `:update` - update the given selector with the returned content.
|
98
|
+
* `:append` - insert the content as a child inside the given selector.
|
99
|
+
* `:delete` - delete all content of the given selector.
|
100
|
+
|
101
|
+
Other Ajax options:
|
102
|
+
|
103
|
+
* `:callback` (String) - the name of a JS function to call on completion.
|
104
|
+
The function will be in the scope of the original element, and
|
105
|
+
will be passed the result data of the Ajax request.
|
106
|
+
* `:use_dialog_opener` (Boolean) - if the Ajax request is sent from inside
|
107
|
+
a dialog, this indicates that the update/append/delete options should
|
108
|
+
look at the element that opened the dialog rather than the element that
|
109
|
+
fired the Ajax request. This is true by default for forms and false for
|
110
|
+
other elements.
|
111
|
+
* `:close_dialog` (Boolean) - if the Ajax request is sent from inside a dialog,
|
112
|
+
this indicates that the dialog should be closed when the request completes
|
113
|
+
successfully. This is true by default for forms and false for
|
114
|
+
other elements.
|
115
|
+
|
116
|
+
## jQuery Events ##
|
117
|
+
|
118
|
+
There are two special events triggered by jqr-helpers:
|
119
|
+
|
120
|
+
* `jqr.load` - this is triggered when a remote call populates an element with
|
121
|
+
data. The scope for the callback is the element which has just had data
|
122
|
+
populated.
|
123
|
+
* `jqr.beforedialogopen` - for remote dialogs, this is triggered when the
|
124
|
+
link or button is clicked to open the dialog but before the request is sent out.
|
125
|
+
|
126
|
+
***
|
127
|
+
|
128
|
+
jqr-helpers was developed by [Wishabi](http://www.wishabi.com).
|
Binary file
|
Binary file
|
@@ -0,0 +1,227 @@
|
|
1
|
+
(function($) {
|
2
|
+
function showThrobber(element) {
|
3
|
+
$(element).after("<img src='/images/jqr-helpers/throbber.gif' class='throbber'/>");
|
4
|
+
$(element).attr('disabled', 'disabled');
|
5
|
+
}
|
6
|
+
|
7
|
+
function hideThrobber(element) {
|
8
|
+
$(element).next().remove();
|
9
|
+
$(element).removeAttr('disabled');
|
10
|
+
}
|
11
|
+
|
12
|
+
var ujsDialogElement = null; // the element that opened the dialog
|
13
|
+
|
14
|
+
// called from dialog button value
|
15
|
+
function ujsSubmitDialogForm() {
|
16
|
+
$('.ui-dialog:visible form').first().submit();
|
17
|
+
}
|
18
|
+
|
19
|
+
// called from dialog button value
|
20
|
+
function ujsDialogClose() {
|
21
|
+
$('.ui-dialog-content:visible').dialog('destroy');
|
22
|
+
}
|
23
|
+
|
24
|
+
function ujsDialogOpen() {
|
25
|
+
if ($(this).parent().height() > $(window).height()) {
|
26
|
+
$(this).height($(window).height() * 0.8);
|
27
|
+
$(this).parent().css('top',
|
28
|
+
($(window).height() - $(this).parent().height()) / 2
|
29
|
+
);
|
30
|
+
$(this).css('overflow-y', 'auto');
|
31
|
+
}
|
32
|
+
var x = $(this).find('.ujs-dialog-x');
|
33
|
+
if (x.length) {
|
34
|
+
$(this).parent().append(x); // to keep it fixed to the dialog
|
35
|
+
// don't let the dialog be resized - the resize functionality
|
36
|
+
// clashes with the X close functionality
|
37
|
+
$(this).dialog('option', 'resizable', false);
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
function ujsDialogClick(event) {
|
42
|
+
ujsDialogElement = $(this);
|
43
|
+
var dialogID = $(this).data('dialog-id');
|
44
|
+
var dialogElement = $('#' + dialogID);
|
45
|
+
if (dialogID == 'next') dialogElement = $(this).next();
|
46
|
+
if ($(this).data('close-x')) {
|
47
|
+
dialogElement.prepend('<span class="ujs-dialog-x"></span>');
|
48
|
+
}
|
49
|
+
var dialogOptions = $(this).data('dialog-options');
|
50
|
+
var open = dialogOptions['open'];
|
51
|
+
dialogOptions = $.extend(dialogOptions, {
|
52
|
+
'open': function() {
|
53
|
+
ujsDialogOpen.call(this);
|
54
|
+
if (open) {
|
55
|
+
var openFunc = eval(open);
|
56
|
+
openFunc.call(this);
|
57
|
+
}
|
58
|
+
}
|
59
|
+
});
|
60
|
+
if (dialogOptions.buttons) {
|
61
|
+
$.each(dialogOptions.buttons, function(index, element) {
|
62
|
+
if (element == 'submit') {
|
63
|
+
dialogOptions.buttons[index] = ujsSubmitDialogForm;
|
64
|
+
}
|
65
|
+
else if (element.click == 'submit') {
|
66
|
+
dialogOptions.buttons[index].click = ujsSubmitDialogForm;
|
67
|
+
}
|
68
|
+
else if (element == 'close') {
|
69
|
+
dialogOptions.buttons[index] = ujsDialogClose;
|
70
|
+
}
|
71
|
+
else if (element.click == 'close') {
|
72
|
+
dialogOptions.buttons[index].click = ujsDialogClose;
|
73
|
+
}
|
74
|
+
});
|
75
|
+
}
|
76
|
+
var url = $(this).data('dialog-url');
|
77
|
+
if (url) {
|
78
|
+
$(this).trigger('jqr.beforedialogopen');
|
79
|
+
$(document.body).append('<div class="ui-widget-overlay ui-front">');
|
80
|
+
$(document.body).append('<div id="remote-dialog-throbber">');
|
81
|
+
if (dialogElement.length == 0) {
|
82
|
+
$('body').append("<div id='" + dialogID + "'>");
|
83
|
+
dialogElement = $('#' + dialogID);
|
84
|
+
}
|
85
|
+
dialogElement.load(url, function() {
|
86
|
+
$('.ui-widget-overlay').remove();
|
87
|
+
$('#remote-dialog-throbber').remove();
|
88
|
+
$(this).dialog(dialogOptions);
|
89
|
+
$(dialogElement).trigger('jqr.load');
|
90
|
+
});
|
91
|
+
}
|
92
|
+
else {
|
93
|
+
dialogElement.dialog(dialogOptions);
|
94
|
+
}
|
95
|
+
event.stopPropagation();
|
96
|
+
return false;
|
97
|
+
}
|
98
|
+
|
99
|
+
function ujsDialogCloseClick() {
|
100
|
+
ujsDialogClose();
|
101
|
+
return false;
|
102
|
+
}
|
103
|
+
|
104
|
+
function ujsAjaxBeforeSend() {
|
105
|
+
showThrobber(this);
|
106
|
+
var disableElement = $(this);
|
107
|
+
if ($(this).is('form'))
|
108
|
+
disableElement = $('button, input[type=submit]', this).first();
|
109
|
+
disableElement.attr('disabled', 'disabled');
|
110
|
+
}
|
111
|
+
|
112
|
+
function ujsAjaxSuccess(evt, data, status, xhr) {
|
113
|
+
hideThrobber(this);
|
114
|
+
var disableElement = $(this);
|
115
|
+
if ($(this).is('form'))
|
116
|
+
disableElement = $('button, input[type=submit]', this).first();
|
117
|
+
disableElement.attr('disabled', false);
|
118
|
+
var element = $(this);
|
119
|
+
var targetElement = element;
|
120
|
+
// if this was sent from a dialog, close the dialog and look at the
|
121
|
+
// element that opened it for update/append/delete callbacks.
|
122
|
+
if ($('.ui-dialog:visible').length) {
|
123
|
+
if (element.data('use-dialog-opener'))
|
124
|
+
targetElement = ujsDialogElement;
|
125
|
+
if (element.data('close-dialog'))
|
126
|
+
ujsDialogClose();
|
127
|
+
}
|
128
|
+
if (element.data('callback')) {
|
129
|
+
var callback = eval(element.data('callback'));
|
130
|
+
callback.call(targetElement, data);
|
131
|
+
}
|
132
|
+
if (data && data.trim().charAt(0) != '<' && data != 'success') {
|
133
|
+
alert(data);
|
134
|
+
return;
|
135
|
+
}
|
136
|
+
var selector = element.data('selector');
|
137
|
+
var target = null;
|
138
|
+
if (selector) {
|
139
|
+
if (selector[0] == '#') target = $(selector);
|
140
|
+
else target = $(targetElement).parents(selector);
|
141
|
+
switch (element.data('result-method')) {
|
142
|
+
case 'update':
|
143
|
+
target = $(data).replaceAll(target);
|
144
|
+
target.trigger('jqr.load');
|
145
|
+
break;
|
146
|
+
case 'append':
|
147
|
+
target.append(data);
|
148
|
+
target.trigger('jqr.load');
|
149
|
+
break;
|
150
|
+
case 'delete':
|
151
|
+
target.remove();
|
152
|
+
break;
|
153
|
+
}
|
154
|
+
target.effect('highlight');
|
155
|
+
}
|
156
|
+
return false;
|
157
|
+
}
|
158
|
+
|
159
|
+
function ujsAjaxError(evt, xhr, status, error) {
|
160
|
+
alert(error || 'An error occurred.');
|
161
|
+
hideThrobber(this);
|
162
|
+
}
|
163
|
+
|
164
|
+
function ujsConfirmClick() {
|
165
|
+
var div = $('<div>');
|
166
|
+
var form = this.form;
|
167
|
+
div.html($(this).data('message'));
|
168
|
+
$('body').append(div);
|
169
|
+
div.dialog({
|
170
|
+
modal: true,
|
171
|
+
width: 'auto',
|
172
|
+
maxWidth: '75%',
|
173
|
+
minWidth: '400',
|
174
|
+
minHeight: 'auto',
|
175
|
+
dialogClass: 'confirm-dialog no-title',
|
176
|
+
buttons: {
|
177
|
+
'Yes': function() {
|
178
|
+
form.submit();
|
179
|
+
},
|
180
|
+
'No': function() {
|
181
|
+
div.dialog('close');
|
182
|
+
div.remove();
|
183
|
+
}
|
184
|
+
}
|
185
|
+
});
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
|
189
|
+
$(function() {
|
190
|
+
$('.ujs-tab-container', this).tabs({
|
191
|
+
beforeLoad: function(event, ui) {
|
192
|
+
if (ui.tab.data('loaded')) {
|
193
|
+
event.preventDefault();
|
194
|
+
return;
|
195
|
+
}
|
196
|
+
ui.jqXHR.success(function() {
|
197
|
+
ui.tab.data('loaded', true);
|
198
|
+
});
|
199
|
+
$(ui.panel).html('Loading...');
|
200
|
+
ui.jqXHR.fail(function(jqXHR, textStatus, errorThrown) {
|
201
|
+
ui.panel.html('Error loading the tab: ' + errorThrown);
|
202
|
+
});
|
203
|
+
}
|
204
|
+
});
|
205
|
+
|
206
|
+
if ($().on) { // newer jQueries
|
207
|
+
$(document).on('click', '.ujs-dialog', ujsDialogClick);
|
208
|
+
$(document).on('click', '.ujs-dialog-close, .ujs-dialog-x',
|
209
|
+
ujsDialogCloseClick);
|
210
|
+
$(document).on('ajax:beforeSend', '.ujs-ajax', ujsAjaxBeforeSend);
|
211
|
+
$(document).on('ajax:success', '.ujs-ajax', ujsAjaxSuccess);
|
212
|
+
$(document).on('ajax:error', '.ujs-ajax', ujsAjaxError);
|
213
|
+
$(document).on('click', '[data-ujs-confirm=true]', ujsConfirmClick);
|
214
|
+
}
|
215
|
+
else {
|
216
|
+
$('.ujs-dialog').live('click', ujsDialogClick);
|
217
|
+
$('.ujs-dialog-close, .ujs-dialog-x').live('click', ujsDialogCloseClick);
|
218
|
+
$('.ujs-ajax').live('ajax:beforeSend', ujsAjaxBeforeSend);
|
219
|
+
$('.ujs-ajax').live('ajax:success', ujsAjaxSuccess);
|
220
|
+
$('.ujs-ajax').live('ajax:error', ujsAjaxError);
|
221
|
+
$('[data-ujs-confirm=true]').live('click', ujsConfirmClick);
|
222
|
+
}
|
223
|
+
|
224
|
+
});
|
225
|
+
|
226
|
+
}(jQuery));
|
227
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
form.ujs-ajax, form.ujs-ajax div {
|
2
|
+
display: inline;
|
3
|
+
}
|
4
|
+
|
5
|
+
.ujs-dialog-modal {
|
6
|
+
position: fixed !important;
|
7
|
+
}
|
8
|
+
|
9
|
+
.ujs-dialog-x {
|
10
|
+
position: absolute;
|
11
|
+
top: -14px;
|
12
|
+
right: -13px;
|
13
|
+
cursor: pointer;
|
14
|
+
background-image: url('/images/jqr-helpers/close.png');
|
15
|
+
width: 30px;
|
16
|
+
height: 30px;
|
17
|
+
}
|
18
|
+
|
19
|
+
.ui-dialog.no-title .ui-dialog-titlebar {
|
20
|
+
display: none;
|
21
|
+
}
|
data/jqr-helpers.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'jqr-helpers'
|
3
|
+
s.require_paths = %w(. lib lib/jqr-helpers)
|
4
|
+
s.version = '1.0.0'
|
5
|
+
s.date = '2013-11-19'
|
6
|
+
s.summary = 'Helpers to print unobtrusive jQuery-UI tags.'
|
7
|
+
s.description = <<-EOF
|
8
|
+
This gem allows the use of several helper methods.
|
9
|
+
The tags output contain classes and data attributes that are grabbed by
|
10
|
+
unobtrusive JavaScript code and turned into jQuery UI widgets.
|
11
|
+
Helpers include Ajax requests to update elements on the page, dialogs
|
12
|
+
(including remote dialogs), and tab containers.
|
13
|
+
EOF
|
14
|
+
s.authors = ['Daniel Orner']
|
15
|
+
s.email = 'daniel.orner@wishabi.com'
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
s.homepage = 'https://github.com/wishabi/jqr-helpers'
|
18
|
+
s.license = 'MIT'
|
19
|
+
|
20
|
+
s.add_dependency 'rails', '>= 3.0'
|
21
|
+
|
22
|
+
end
|
data/lib/jqr-helpers.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'jqr-helpers/railtie' if defined?(Rails)
|
@@ -0,0 +1,213 @@
|
|
1
|
+
module JqrHelpers
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
# Add a link to create a jQuery dialog.
|
5
|
+
# If a block is given, dialog_options and html_options are shifted left by
|
6
|
+
# 1 and the block is used as the html_content.
|
7
|
+
# @param dialog_id [String] The ID of the element to put in the dialog.
|
8
|
+
# @param html_content [String] Text or HTML tags to use as the link body.
|
9
|
+
# @param dialog_options [Hash] See above.
|
10
|
+
# @param html_options [Hash] Attributes to put on the link tag. There is
|
11
|
+
# a special :tag_name option that can be used to change the tag being
|
12
|
+
# created. Default is :a, but you can pass :div, :span, etc.
|
13
|
+
# @return [String]
|
14
|
+
def link_to_dialog(dialog_id, html_content='', dialog_options={},
|
15
|
+
html_options={}, &block)
|
16
|
+
|
17
|
+
if block_given?
|
18
|
+
html_options = dialog_options
|
19
|
+
dialog_options = html_content.presence || {}
|
20
|
+
html_content = capture(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
html_options[:class] ||= ''
|
24
|
+
html_options[:class] << ' ujs-dialog'
|
25
|
+
html_options[:'data-dialog-id'] = dialog_id
|
26
|
+
html_options[:'data-close-x'] = dialog_options[:close_x]
|
27
|
+
|
28
|
+
tag_name = html_options.delete(:tag_name) || :a
|
29
|
+
html_options[:href] = '#' if tag_name == :a
|
30
|
+
|
31
|
+
if dialog_options[:title] == false # not nil or blank
|
32
|
+
dialog_options[:dialogClass] ||= ''
|
33
|
+
dialog_options[:dialogClass] << ' ujs-dialog-modal no-title'
|
34
|
+
else
|
35
|
+
dialog_options[:title] ||= 'Dialog'
|
36
|
+
end
|
37
|
+
dialog_options[:modal] = true
|
38
|
+
dialog_options[:width] ||= 'auto'
|
39
|
+
if dialog_options.delete(:default_buttons)
|
40
|
+
dialog_options[:buttons] = {
|
41
|
+
:OK => 'submit',
|
42
|
+
:Cancel => 'close'
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
html_options[:'data-dialog-options'] = dialog_options.to_json
|
47
|
+
|
48
|
+
content_tag tag_name, html_content, html_options
|
49
|
+
end
|
50
|
+
|
51
|
+
# Add a button to create a jQuery dialog.
|
52
|
+
# @param dialog_id [String] The ID of the element to put in the dialog.
|
53
|
+
# @param html_content [String] Text or HTML tags to use as the button body.
|
54
|
+
# @param html_options [Hash] Attributes to put on the button tag.
|
55
|
+
# @param dialog_options [Hash] See above.
|
56
|
+
# @return [String]
|
57
|
+
def button_to_dialog(dialog_id, html_content, dialog_options={},
|
58
|
+
html_options={})
|
59
|
+
link_to_dialog(dialog_id, html_content, dialog_options,
|
60
|
+
html_options.merge(:tag_name => 'button'))
|
61
|
+
end
|
62
|
+
|
63
|
+
# Create a button that prompts a jQuery confirm dialog, which is nicer-looking
|
64
|
+
# than the default window.confirm() which is used by Rails. Done using
|
65
|
+
# button_to, so note that a form element will be added.
|
66
|
+
# @param html_content [String] the text or content to go inside the button
|
67
|
+
# @param url [String] the URL which the button should go to if confirmed
|
68
|
+
# @param message [String] the confirm message to prompt
|
69
|
+
# @param html_options [Hash] HTML attributes.
|
70
|
+
def confirm_button(html_content, url, message, html_options)
|
71
|
+
button_to html_content, url, html_options.merge(
|
72
|
+
:'data-message' => simple_format(message), # turn text into HTML
|
73
|
+
:'data-ujs-confirm' => true
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Same as link_to_dialog, but loads content from a remote URL instead of
|
78
|
+
# using content already on the page.
|
79
|
+
# If a block is given, dialog_options and html_options are shifted left by
|
80
|
+
# 1 and the block is used as the html_content.
|
81
|
+
# @param id [String] A unique ID to use to reference the dialog options.
|
82
|
+
# This is ultimately created as an element with that ID in the DOM,
|
83
|
+
# but the element does not have to exist already, unlike link_to_dialog.
|
84
|
+
# @param url [String] The URL to load the content from.
|
85
|
+
# @param html_content [String] Text or HTML tags to use as the link body.
|
86
|
+
# @param dialog_options [Hash] See above.
|
87
|
+
# @param html_options [Hash] Attributes to put on the link tag. There is
|
88
|
+
# a special :tag_name option that can be used to change the tag being
|
89
|
+
# created. Default is :a, but you can pass :div, :span, etc.
|
90
|
+
# @return [String]
|
91
|
+
def link_to_remote_dialog(id, url, html_content, dialog_options={},
|
92
|
+
html_options={}, &block)
|
93
|
+
|
94
|
+
if block_given?
|
95
|
+
html_options = dialog_options
|
96
|
+
dialog_options = html_content
|
97
|
+
html_content = capture(&block)
|
98
|
+
end
|
99
|
+
|
100
|
+
html_options[:'data-dialog-url'] = url
|
101
|
+
link_to_dialog(id, html_content, dialog_options, html_options)
|
102
|
+
end
|
103
|
+
|
104
|
+
# Same as button_to_dialog, but loads content from a remote URL instead of
|
105
|
+
# using content already on the page.
|
106
|
+
# @param id [String] A unique ID to use to reference the dialog options.
|
107
|
+
# This is ultimately created as an element with that ID in the DOM,
|
108
|
+
# but the element does not have to exist already, unlike button_to_dialog.
|
109
|
+
# @param url [String] The URL to load the content from.
|
110
|
+
# @param html_content [String] Text or HTML tags to use as the button body.
|
111
|
+
# @param dialog_options [Hash] See above.
|
112
|
+
# @param html_options [Hash] Attributes to put on the button tag.
|
113
|
+
# @return [String]
|
114
|
+
def button_to_remote_dialog(id, url, html_content, dialog_options={},
|
115
|
+
html_options={})
|
116
|
+
link_to_remote_dialog(id, url, html_content, dialog_options,
|
117
|
+
html_options.merge(:tag_name => 'button'))
|
118
|
+
end
|
119
|
+
|
120
|
+
# Create a link that fires off a jQuery Ajax request. This is basically
|
121
|
+
# a wrapper around link_to :remote => true.
|
122
|
+
# If a block is given, url and options will be shifted left by 1 position
|
123
|
+
# and the block contents will be used for the body.
|
124
|
+
# @param body [String] the text/content that goes inside the tag.
|
125
|
+
# @param url [String] the URL to connect to.
|
126
|
+
# @param options [Hash] Ajax options - see above.
|
127
|
+
# @return [String]
|
128
|
+
def link_to_ajax(body, url, options={}, &block)
|
129
|
+
if block_given?
|
130
|
+
options = url
|
131
|
+
url = body
|
132
|
+
body = capture(&block)
|
133
|
+
end
|
134
|
+
|
135
|
+
options[:remote] = true
|
136
|
+
options.merge!(_process_ajax_options(options))
|
137
|
+
|
138
|
+
link_to body, url, options
|
139
|
+
end
|
140
|
+
|
141
|
+
# Create a button that fires off a jQuery Ajax request. This is basically
|
142
|
+
# a wrapper around button_to :remote => true.
|
143
|
+
# @param body [String] the text/content that goes inside the tag.
|
144
|
+
# @param url [String] the URL to connect to.
|
145
|
+
# @param options [Hash] Ajax options - see above.
|
146
|
+
# @return [String]
|
147
|
+
def button_to_ajax(body, url, options={})
|
148
|
+
|
149
|
+
options[:remote] = true
|
150
|
+
options[:form] ||= {}
|
151
|
+
options[:form].merge!(_process_ajax_options(options))
|
152
|
+
|
153
|
+
button_to body, url, options
|
154
|
+
end
|
155
|
+
|
156
|
+
# Create a form tag that submits to an Ajax request. Basically a wrapper for
|
157
|
+
# form_tag with :remote => true.
|
158
|
+
# @param url [String] the URL to connect to.
|
159
|
+
# @param options [Hash] Ajax options - see above.
|
160
|
+
# @return [String]
|
161
|
+
def form_tag_ajax(url, options={}, &block)
|
162
|
+
|
163
|
+
options[:remote] = true
|
164
|
+
# note that we only override if nil - not false
|
165
|
+
options[:close_dialog] = true if options[:close_dialog].nil?
|
166
|
+
options[:use_dialog_opener] = true if options[:use_dialog_opener].nil?
|
167
|
+
options.merge!(_process_ajax_options(options))
|
168
|
+
|
169
|
+
form_tag url, options, &block
|
170
|
+
end
|
171
|
+
|
172
|
+
# Identical to form_tag_ajax except that this passes the given model into
|
173
|
+
# form_for instead of form_tag.
|
174
|
+
# @param record [ActiveRecord::Base]
|
175
|
+
# @param options [Hash]
|
176
|
+
# @return [String]
|
177
|
+
def form_for_ajax(record, options={}, &block)
|
178
|
+
options[:remote] = true
|
179
|
+
# note that we only override if nil - not false
|
180
|
+
options[:close_dialog] = true if options[:close_dialog].nil?
|
181
|
+
options[:use_dialog_opener] = true if options[:use_dialog_opener].nil?
|
182
|
+
options[:html] ||= {}
|
183
|
+
options[:html].merge!(_process_ajax_options(options))
|
184
|
+
|
185
|
+
form_for record, options, &block
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
private
|
190
|
+
|
191
|
+
# Process options related to Ajax requests (e.g. button_to_ajax).
|
192
|
+
# @param options [Hash]
|
193
|
+
# @return [Hash] HTML options to inject.
|
194
|
+
def _process_ajax_options(options)
|
195
|
+
new_options = {}
|
196
|
+
new_options[:class] = options[:class] || ''
|
197
|
+
new_options[:class] << ' ujs-ajax'
|
198
|
+
new_options[:'data-type'] = 'html'
|
199
|
+
new_options[:'data-callback'] = options.delete(:callback)
|
200
|
+
new_options[:'data-close-dialog'] = options.delete(:close_dialog)
|
201
|
+
new_options[:'data-use-dialog-opener'] = options.delete(:use_dialog_opener)
|
202
|
+
|
203
|
+
[:update, :append, :delete].each do |result_method|
|
204
|
+
selector = options.delete(result_method)
|
205
|
+
if selector
|
206
|
+
new_options[:'data-selector'] = selector
|
207
|
+
new_options[:'data-result-method'] = result_method
|
208
|
+
end
|
209
|
+
end
|
210
|
+
new_options
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'rails/generators'
|
3
|
+
|
4
|
+
module JqrHelpers
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < ::Rails::Generators::Base
|
7
|
+
|
8
|
+
desc 'This generator installs jqr-helper JavaScript, CSS, and image files.'
|
9
|
+
source_root File.expand_path('../../../app/assets', __FILE__)
|
10
|
+
|
11
|
+
def copy_files
|
12
|
+
log 'Copying files...'
|
13
|
+
files = [
|
14
|
+
'javascripts/jqr-helpers.js',
|
15
|
+
'stylesheets/jqr-helpers.css',
|
16
|
+
'images/jqr-helpers/close.png',
|
17
|
+
'images/jqr-helpers/throbber.gif'
|
18
|
+
]
|
19
|
+
files.each do |file|
|
20
|
+
copy_file file, "public/#{file}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jqr-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
version: 1.0.0
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Daniel Orner
|
@@ -36,7 +36,18 @@ email: daniel.orner@wishabi.com
|
|
36
36
|
executables: []
|
37
37
|
extensions: []
|
38
38
|
extra_rdoc_files: []
|
39
|
-
files:
|
39
|
+
files:
|
40
|
+
- Readme.md
|
41
|
+
- app/assets/images/jqr-helpers/close.png
|
42
|
+
- app/assets/images/jqr-helpers/throbber.gif
|
43
|
+
- app/assets/javascripts/jqr-helpers.js
|
44
|
+
- app/assets/stylesheets/jqr-helpers.css
|
45
|
+
- jqr-helpers.gemspec
|
46
|
+
- lib/jqr-helpers.rb
|
47
|
+
- lib/jqr-helpers/helpers.rb
|
48
|
+
- lib/jqr-helpers/install_generator.rb
|
49
|
+
- lib/jqr-helpers/railtie.rb
|
50
|
+
- lib/jqr-helpers/version.rb
|
40
51
|
homepage: https://github.com/wishabi/jqr-helpers
|
41
52
|
licenses:
|
42
53
|
- MIT
|
@@ -46,8 +57,6 @@ require_paths:
|
|
46
57
|
- .
|
47
58
|
- lib
|
48
59
|
- lib/jqr-helpers
|
49
|
-
- lib/generators
|
50
|
-
- app/helpers
|
51
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
61
|
none: false
|
53
62
|
requirements:
|
@@ -57,9 +66,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
66
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
67
|
none: false
|
59
68
|
requirements:
|
60
|
-
- - ! '
|
69
|
+
- - ! '>='
|
61
70
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
71
|
+
version: '0'
|
63
72
|
requirements: []
|
64
73
|
rubyforge_project:
|
65
74
|
rubygems_version: 1.8.25
|