compass-jquery-plugin 0.2.4.5 → 0.2.4.99
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +36 -4
- data/VERSION.yml +1 -1
- data/compass-jquery-plugin.gemspec +49 -3
- data/gem_tasks/calendar.rake +2 -2
- data/gem_tasks/dynatree.rake +2 -2
- data/gem_tasks/jqgrid.rake +2 -2
- data/gem_tasks/jqtouch.rake +114 -0
- data/gem_tasks/jrails.rake +20 -4
- data/gem_tasks/rubygems.rake +4 -1
- data/gem_tasks/secret_sauce.rake +2 -2
- data/lib/jquery/jqgrid/jqgrid.rb +435 -0
- data/lib/jquery/jqgrid/jqgrid2xml.rb +50 -0
- data/lib/jquery/jqgrid.rb +3 -419
- data/lib/jquery/jqtouch.rb +4 -0
- data/templates/dynatree/jquery.ui/dynatree.vista/folder.png +0 -0
- data/templates/dynatree/manifest.rb +0 -1
- data/templates/jqtouch/config/initializers/jqtouch.rb +3 -0
- data/templates/jqtouch/jqtouch/apple/backButton.png +0 -0
- data/templates/jqtouch/jqtouch/apple/blueButton.png +0 -0
- data/templates/jqtouch/jqtouch/apple/cancel.png +0 -0
- data/templates/jqtouch/jqtouch/apple/chevron.png +0 -0
- data/templates/jqtouch/jqtouch/apple/grayButton.png +0 -0
- data/templates/jqtouch/jqtouch/apple/listArrowSel.png +0 -0
- data/templates/jqtouch/jqtouch/apple/listGroup.png +0 -0
- data/templates/jqtouch/jqtouch/apple/loading.gif +0 -0
- data/templates/jqtouch/jqtouch/apple/on_off.png +0 -0
- data/templates/jqtouch/jqtouch/apple/pinstripes.png +0 -0
- data/templates/jqtouch/jqtouch/apple/selection.png +0 -0
- data/templates/jqtouch/jqtouch/apple/thumb.png +0 -0
- data/templates/jqtouch/jqtouch/apple/toggle.png +0 -0
- data/templates/jqtouch/jqtouch/apple/toggleOn.png +0 -0
- data/templates/jqtouch/jqtouch/apple/toolButton.png +0 -0
- data/templates/jqtouch/jqtouch/apple/toolbar.png +0 -0
- data/templates/jqtouch/jqtouch/apple/whiteButton.png +0 -0
- data/templates/jqtouch/jqtouch/apple.sass +651 -0
- data/templates/jqtouch/jqtouch/icons/iphone_16x16.png +0 -0
- data/templates/jqtouch/jqtouch/iphone-emulator.sass +17 -0
- data/templates/jqtouch/jqtouch/iphone_fullsize.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/back_button.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/back_button_clicked.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/button.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/button_clicked.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/chevron.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/chevron_circle.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/grayButton.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/loading.gif +0 -0
- data/templates/jqtouch/jqtouch/jqt/on_off.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/rowhead.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/toggle.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/toggleOn.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/toolbar.png +0 -0
- data/templates/jqtouch/jqtouch/jqt/whiteButton.png +0 -0
- data/templates/jqtouch/jqtouch/jqt.sass +580 -0
- data/templates/jqtouch/jqtouch/jqtouch.sass +75 -0
- data/templates/jqtouch/jquery.jqtouch.js +1007 -0
- data/templates/jqtouch/jquery.jqtouch.min.js +1 -0
- data/templates/jqtouch/manifest.rb +43 -0
- data/templates/jrails/config/initializers/jrails.rb +2 -0
- data/templates/jrails/jquery.compat-1.3.js +288 -0
- data/templates/jrails/jquery.compat-1.3.min.js +1 -0
- data/templates/jrails/jquery.js +4483 -2570
- data/templates/jrails/jquery.min.js +12 -8
- data/templates/jrails/manifest.rb +2 -0
- metadata +49 -3
- data/templates/dynatree/jquery.ui/dynatree.vista/folder_open.png +0 -0
@@ -0,0 +1,435 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
def jqgrid_javascripts(locale)
|
5
|
+
js = capture { javascript_include_tag "i18n/jqgrid/locale-#{locale}.min" }
|
6
|
+
js << capture {javascript_tag "jQuery.jgrid.no_legacy_api = true;" }
|
7
|
+
js << capture { javascript_include_tag 'jquery.jqGrid.min' }
|
8
|
+
end
|
9
|
+
|
10
|
+
def jqgrid_from_xml(name, opts={})
|
11
|
+
@@grid_name = name.to_s
|
12
|
+
grid_data = YAML::load_file("#{RAILS_ROOT}/config/jquery/jqGrid/#{name}.yml")
|
13
|
+
|
14
|
+
before = grid_data[:before]
|
15
|
+
params = grid_data[:params]
|
16
|
+
|
17
|
+
def param_body(value)
|
18
|
+
value.scan(/^\((.+?)\)(.+)/).flatten
|
19
|
+
end
|
20
|
+
|
21
|
+
jquery_grid_code = <<-CODE
|
22
|
+
<script type="text/javascript">
|
23
|
+
<%= before %>_n_
|
24
|
+
jQuery(document).ready(function(){
|
25
|
+
jQuery("#<%= name %>").jqGrid({
|
26
|
+
<% params.each do |key,value| %>
|
27
|
+
<% if opts.keys.include?(key) %>
|
28
|
+
_ss_<%= key %>: <%= opts[key].to_json %>,
|
29
|
+
<% elsif (value.is_a? String) && (value[0..0] == "$") %>
|
30
|
+
_ss_<%= key %>: <%= "jQuery"+value[1,value.length] %>,
|
31
|
+
<% elsif (key.to_s[0..8] == 'function_') %>
|
32
|
+
<% js_params, body = param_body(value) %>
|
33
|
+
_ss_<%= key.to_s[9..-1] %>: function(<%= js_params %>) {
|
34
|
+
_ss__ss_<%= body %>
|
35
|
+
_ss_},
|
36
|
+
<% else %>
|
37
|
+
_ss_<%= key %>: <%= value.to_json %>,
|
38
|
+
<% end %>
|
39
|
+
<% end %>
|
40
|
+
<% opts.each do |key,value| %>
|
41
|
+
<% unless params.keys.include?(key) %>
|
42
|
+
_ss_<%= key %>: <%= value.to_json %>,
|
43
|
+
<% end %>
|
44
|
+
<% end %> });
|
45
|
+
});
|
46
|
+
</script>
|
47
|
+
CODE
|
48
|
+
::ERB.new(jquery_grid_code, nil, '>').result(binding).gsub("_n_", "\n").
|
49
|
+
gsub(/[ \t]{2,}/,' ').
|
50
|
+
gsub(/,\s+\}\);/, "\n });").
|
51
|
+
gsub(/\n\s*\n/m, "\n").
|
52
|
+
gsub("_ss_", " ").
|
53
|
+
gsub("<grid_name>", @@grid_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def jqgrid(title, id, action, columns = {},
|
58
|
+
options = {}, edit_options = "", add_options = "", del_options = "", search_options = "",
|
59
|
+
custom1_button = nil, custom2_button = nil, custom3_button = nil, custom4_button = nil, custom5_button = nil )
|
60
|
+
|
61
|
+
# Default options
|
62
|
+
options =
|
63
|
+
{
|
64
|
+
:rows_per_page => '10',
|
65
|
+
:sort_column => '',
|
66
|
+
:sort_order => '',
|
67
|
+
:height => '150',
|
68
|
+
:gridview => 'false',
|
69
|
+
:error_handler => 'null',
|
70
|
+
:inline_edit_handler => 'null',
|
71
|
+
:add => 'false',
|
72
|
+
:delete => 'false',
|
73
|
+
:search => 'true',
|
74
|
+
:edit => 'false',
|
75
|
+
:inline_edit => 'false',
|
76
|
+
:autowidth => 'false',
|
77
|
+
:hidegrid => 'false',
|
78
|
+
:rownumbers => 'false'
|
79
|
+
}.merge(options)
|
80
|
+
|
81
|
+
# Stringify options values
|
82
|
+
options.inject({}) do |options, (key, value)|
|
83
|
+
options[key] = (key != :subgrid) ? value.to_s : value
|
84
|
+
options
|
85
|
+
end
|
86
|
+
|
87
|
+
options[:error_handler_return_value] = (options[:error_handler] == 'null') ? 'true;' : options[:error_handler]
|
88
|
+
edit_button = (options[:edit] == 'true' && options[:inline_edit] == 'false').to_s
|
89
|
+
|
90
|
+
# Generate columns data
|
91
|
+
col_names, col_model = gen_columns(columns)
|
92
|
+
|
93
|
+
# Enable multi-selection (checkboxes)
|
94
|
+
multiselect = "multiselect: false,"
|
95
|
+
if options[:multi_selection]
|
96
|
+
multiselect = "multiselect: true,"
|
97
|
+
multihandler = %Q/
|
98
|
+
jQuery("##{id}_select_button").click( function() {
|
99
|
+
var s; s = jQuery("##{id}").jqGrid('getGridParam', 'selarrrow');
|
100
|
+
#{options[:selection_handler]}(s);
|
101
|
+
return false;
|
102
|
+
});/
|
103
|
+
end
|
104
|
+
|
105
|
+
# Enable master-details
|
106
|
+
masterdetails = ""
|
107
|
+
if options[:master_details]
|
108
|
+
masterdetails = %Q/
|
109
|
+
onSelectRow: function(ids) {
|
110
|
+
if(ids == null) {
|
111
|
+
ids=0;
|
112
|
+
if(jQuery("##{id}_details").jqGrid('getGridParam', 'records') >0 )
|
113
|
+
{
|
114
|
+
jQuery("##{id}_details").jqGrid('setGridParam', {url:"#{options[:details_url]}?q=1&id="+ids,page:1})
|
115
|
+
jQuery("##{id}_details").jqGrid('setCaption', "#{options[:details_caption]}: "+ids)
|
116
|
+
jQuery("##{id}_details").trigger('reloadGrid');
|
117
|
+
}
|
118
|
+
}
|
119
|
+
else
|
120
|
+
{
|
121
|
+
jQuery("##{id}_details").jqGrid('setGridParam', {url:"#{options[:details_url]}?q=1&id="+ids,page:1})
|
122
|
+
jQuery("##{id}_details").jqGrid('setCaption', "#{options[:details_caption]} : "+ids)
|
123
|
+
jQuery("##{id}_details").trigger('reloadGrid');
|
124
|
+
}
|
125
|
+
},/
|
126
|
+
end
|
127
|
+
|
128
|
+
# Enable selection link, button
|
129
|
+
# The javascript function created by the user (options[:selection_handler]) will be called with the selected row id as a parameter
|
130
|
+
selection_link = ""
|
131
|
+
if options[:direct_selection].blank? && options[:selection_handler].present? && options[:multi_selection].blank?
|
132
|
+
selection_link = %Q/
|
133
|
+
jQuery("##{id}_select_button").click( function(){
|
134
|
+
var id = jQuery("##{id}").jqGrid('getGridParam', 'selrow');
|
135
|
+
if (id) {
|
136
|
+
#{options[:selection_handler]}(id);
|
137
|
+
} else {
|
138
|
+
alert("Please select a row");
|
139
|
+
}
|
140
|
+
return false;
|
141
|
+
});/
|
142
|
+
end
|
143
|
+
|
144
|
+
# Enable direct selection (when a row in the table is clicked)
|
145
|
+
# The javascript function created by the user (options[:selection_handler]) will be called with the selected row id as a parameter
|
146
|
+
direct_link = ""
|
147
|
+
if options[:direct_selection] && options[:selection_handler].present? && options[:multi_selection].blank?
|
148
|
+
direct_link = %Q/
|
149
|
+
onSelectRow: function(id){
|
150
|
+
if(id){
|
151
|
+
#{options[:selection_handler]}(id);
|
152
|
+
}
|
153
|
+
},/
|
154
|
+
end
|
155
|
+
|
156
|
+
# Enable grid_loaded callback
|
157
|
+
# When data are loaded into the grid, call the Javascript function options[:grid_loaded] (defined by the user)
|
158
|
+
grid_loaded = ""
|
159
|
+
if options[:grid_loaded].present?
|
160
|
+
grid_loaded = %Q/
|
161
|
+
loadComplete: function(){
|
162
|
+
#{options[:grid_loaded]}();
|
163
|
+
},
|
164
|
+
/
|
165
|
+
end
|
166
|
+
|
167
|
+
# Enable inline editing
|
168
|
+
# When a row is selected, all fields are transformed to input types
|
169
|
+
editable = ""
|
170
|
+
if options[:edit] && options[:inline_edit] == 'true'
|
171
|
+
editable = %Q/
|
172
|
+
onSelectRow: function(id){
|
173
|
+
if(id && id!==lastsel){
|
174
|
+
jQuery('##{id}').jqGrid('restoreRow', lastsel);
|
175
|
+
jQuery('##{id}').jqGrid('editRow', id, true, #{options[:inline_edit_handler]}, #{options[:error_handler]});
|
176
|
+
lastsel=id;
|
177
|
+
}
|
178
|
+
},/
|
179
|
+
end
|
180
|
+
|
181
|
+
# Enable subgrids
|
182
|
+
subgrid = ""
|
183
|
+
subgrid_enabled = "subGrid:false,"
|
184
|
+
|
185
|
+
if options[:subgrid].present?
|
186
|
+
|
187
|
+
subgrid_enabled = "subGrid:true,"
|
188
|
+
|
189
|
+
options[:subgrid] =
|
190
|
+
{
|
191
|
+
:rows_per_page => '10',
|
192
|
+
:sort_column => 'id',
|
193
|
+
:sort_order => 'asc',
|
194
|
+
:add => 'false',
|
195
|
+
:edit => 'false',
|
196
|
+
:delete => 'false',
|
197
|
+
:search => 'false'
|
198
|
+
}.merge(options[:subgrid])
|
199
|
+
|
200
|
+
# Stringify options values
|
201
|
+
options[:subgrid].inject({}) do |suboptions, (key, value)|
|
202
|
+
suboptions[key] = value.to_s
|
203
|
+
suboptions
|
204
|
+
end
|
205
|
+
|
206
|
+
subgrid_inline_edit = ""
|
207
|
+
if options[:subgrid][:inline_edit] == true
|
208
|
+
options[:subgrid][:edit] = 'false'
|
209
|
+
subgrid_inline_edit = %Q/
|
210
|
+
onSelectRow: function(id){
|
211
|
+
if(id && id!==lastsel){
|
212
|
+
jQuery('#'+subgrid_table_id).jqGrid('restoreRow', lastsel);
|
213
|
+
jQuery('#'+subgrid_table_id).jqGrid('editRow', id,true);
|
214
|
+
lastsel=id;
|
215
|
+
}
|
216
|
+
},
|
217
|
+
/
|
218
|
+
end
|
219
|
+
|
220
|
+
if options[:subgrid][:direct_selection] && options[:subgrid][:selection_handler].present?
|
221
|
+
subgrid_direct_link = %Q/
|
222
|
+
onSelectRow: function(id){
|
223
|
+
if(id){
|
224
|
+
#{options[:subgrid][:selection_handler]}(id);
|
225
|
+
}
|
226
|
+
},
|
227
|
+
/
|
228
|
+
end
|
229
|
+
|
230
|
+
sub_col_names, sub_col_model = gen_columns(options[:subgrid][:columns])
|
231
|
+
|
232
|
+
subgrid = %Q(
|
233
|
+
subGridRowExpanded: function(subgrid_id, row_id) {
|
234
|
+
var subgrid_table_id, pager_id;
|
235
|
+
subgrid_table_id = subgrid_id+"_t";
|
236
|
+
pager_id = "p_"+subgrid_table_id;
|
237
|
+
$("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
|
238
|
+
jQuery("#"+subgrid_table_id).jqGrid({
|
239
|
+
url:"#{options[:subgrid][:url]}?q=2&id="+row_id,
|
240
|
+
editurl:'#{options[:subgrid][:edit_url]}?parent_id='+row_id,
|
241
|
+
datatype: "json",
|
242
|
+
colNames: #{sub_col_names},
|
243
|
+
colModel: #{sub_col_model},
|
244
|
+
rowNum:#{options[:subgrid][:rows_per_page]},
|
245
|
+
pager: pager_id,
|
246
|
+
imgpath: '/images/jqgrid',
|
247
|
+
sortname: '#{options[:subgrid][:sort_column]}',
|
248
|
+
sortorder: '#{options[:subgrid][:sort_order]}',
|
249
|
+
viewrecords: true,
|
250
|
+
toolbar : [true,"top"],
|
251
|
+
#{subgrid_inline_edit}
|
252
|
+
#{subgrid_direct_link}
|
253
|
+
height: '100%'
|
254
|
+
});
|
255
|
+
jQuery("#"+subgrid_table_id).jqGrid('navGrid', "#"+pager_id,{edit:#{options[:subgrid][:edit]},add:#{options[:subgrid][:add]},del:#{options[:subgrid][:delete]},search:false})
|
256
|
+
jQuery("#"+subgrid_table_id).jqGrid('navButtonAdd', "#"+pager_id,{caption:"Search",title:"Toggle Search",buttonimg:'/images/jqgrid/search.png',
|
257
|
+
onClickButton:function(){
|
258
|
+
if(jQuery("#t_"+subgrid_table_id).css("display")=="none") {
|
259
|
+
jQuery("#t_"+subgrid_table_id).css("display","");
|
260
|
+
} else {
|
261
|
+
jQuery("#t_"+subgrid_table_id).css("display","none");
|
262
|
+
}
|
263
|
+
}
|
264
|
+
});
|
265
|
+
jQuery("#t_"+subgrid_table_id).height(25).hide().jqGrid('filterGrid', ""+subgrid_table_id,{gridModel:true,gridToolbar:true});
|
266
|
+
},
|
267
|
+
subGridRowColapsed: function(subgrid_id, row_id) {
|
268
|
+
},
|
269
|
+
)
|
270
|
+
end
|
271
|
+
|
272
|
+
# Generate required Javascript & html to create the jqgrid
|
273
|
+
%Q(
|
274
|
+
<script type="text/javascript">
|
275
|
+
var lastsel;
|
276
|
+
jQuery(document).ready(function(){
|
277
|
+
jQuery("##{id}").jqGrid({
|
278
|
+
url:'#{action}?q=1',
|
279
|
+
editurl:'#{options[:edit_url]}',
|
280
|
+
datatype: "json",
|
281
|
+
colNames:#{col_names},
|
282
|
+
colModel:#{col_model},
|
283
|
+
pager: '##{id}_pager',
|
284
|
+
rowNum:#{options[:rows_per_page]},
|
285
|
+
rowList:[10,25,50,100],
|
286
|
+
sortname: '#{options[:sort_column]}',
|
287
|
+
sortorder: '#{options[:sort_order]}',
|
288
|
+
viewrecords: true,
|
289
|
+
height: #{options[:height]},
|
290
|
+
toolbar : [true,"top"],
|
291
|
+
gridview: #{options[:gridview]},
|
292
|
+
hidegrid: #{options[:hidegrid]},
|
293
|
+
scrollrows: true,
|
294
|
+
rownumbers: #{options[:rownumbers]},
|
295
|
+
autowidth: #{options[:autowidth]},
|
296
|
+
#{multiselect}
|
297
|
+
#{masterdetails}
|
298
|
+
#{grid_loaded}
|
299
|
+
#{direct_link}
|
300
|
+
#{editable}
|
301
|
+
#{subgrid_enabled}
|
302
|
+
#{subgrid}
|
303
|
+
caption: "#{title}"
|
304
|
+
});
|
305
|
+
jQuery("##{id}").jqGrid('navGrid', '##{id}_pager',
|
306
|
+
{edit:#{edit_button},add:#{options[:add]},del:#{options[:delete]},search:#{options[:search]},refresh:true},
|
307
|
+
{afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'edit');}},
|
308
|
+
{afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'add');}},
|
309
|
+
{afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'delete');}},
|
310
|
+
{#{search_options}}
|
311
|
+
);
|
312
|
+
#{multihandler}
|
313
|
+
#{selection_link}
|
314
|
+
});
|
315
|
+
</script>
|
316
|
+
)
|
317
|
+
end
|
318
|
+
|
319
|
+
private
|
320
|
+
|
321
|
+
def gen_columns(columns)
|
322
|
+
# Generate columns data
|
323
|
+
col_names = "[" # Labels
|
324
|
+
col_model = "[" # Options
|
325
|
+
columns.each do |c|
|
326
|
+
col_names << "'#{c[:label]}',"
|
327
|
+
col_model << "{name:'#{c[:field]}', index:'#{c[:field]}'#{get_attributes(c)}},"
|
328
|
+
end
|
329
|
+
col_names.chop! << "]"
|
330
|
+
col_model.chop! << "]"
|
331
|
+
[col_names, col_model]
|
332
|
+
end
|
333
|
+
|
334
|
+
# Generate a list of attributes for related column (align:'right', sortable:true, resizable:false, ...)
|
335
|
+
def get_attributes(column)
|
336
|
+
options = ","
|
337
|
+
column.except(:field, :label).each do |couple|
|
338
|
+
if couple[0] == :editoptions
|
339
|
+
options << "editoptions:#{get_sub_options(couple[1])},"
|
340
|
+
elsif couple[0] == :formoptions
|
341
|
+
options << "formoptions:#{get_sub_options(couple[1])},"
|
342
|
+
elsif couple[0] == :searchoptions
|
343
|
+
options << "searchoptions:#{get_sub_options(couple[1])},"
|
344
|
+
elsif couple[0] == :editrules
|
345
|
+
options << "editrules:#{get_sub_options(couple[1])},"
|
346
|
+
else
|
347
|
+
if couple[1].class == String
|
348
|
+
options << "#{couple[0]}:'#{couple[1]}',"
|
349
|
+
else
|
350
|
+
options << "#{couple[0]}:#{couple[1]},"
|
351
|
+
end
|
352
|
+
end
|
353
|
+
end
|
354
|
+
options.chop!
|
355
|
+
end
|
356
|
+
|
357
|
+
# Generate options for editable fields (value, data, width, maxvalue, cols, rows, ...)
|
358
|
+
def get_sub_options(editoptions)
|
359
|
+
options = "{"
|
360
|
+
editoptions.each do |couple|
|
361
|
+
if couple[0] == :value # :value => [[1, "Rails"], [2, "Ruby"], [3, "jQuery"]]
|
362
|
+
options << %Q/value:"/
|
363
|
+
couple[1].each do |v|
|
364
|
+
options << "#{v[0]}:#{v[1]};"
|
365
|
+
end
|
366
|
+
options.chop! << %Q/",/
|
367
|
+
elsif couple[0] == :data # :data => [Category.all, :id, :title])
|
368
|
+
options << %Q/value:"/
|
369
|
+
couple[1].first.each do |obj|
|
370
|
+
options << "%s:%s;" % [obj.send(couple[1].second), obj.send(couple[1].third)]
|
371
|
+
end
|
372
|
+
options.chop! << %Q/",/
|
373
|
+
else # :size => 30, :rows => 5, :maxlength => 20, ...
|
374
|
+
if couple[1].instance_of?(Fixnum) || couple[1] == 'true' || couple[1] == 'false' || couple[1] == true || couple[1] == false
|
375
|
+
options << %Q/#{couple[0]}:#{couple[1]},/
|
376
|
+
else
|
377
|
+
options << %Q/#{couple[0]}:"#{couple[1]}",/
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
options.chop! << "}"
|
382
|
+
end
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
module JqgridJson
|
387
|
+
include ActionView::Helpers::JavaScriptHelper
|
388
|
+
|
389
|
+
def to_jqgrid_json(attributes, current_page, per_page, total)
|
390
|
+
json = %Q({"page":"#{current_page}","total":#{total/per_page.to_i+1},"records":"#{total}")
|
391
|
+
if total > 0
|
392
|
+
json << %Q(,"rows":[)
|
393
|
+
each do |elem|
|
394
|
+
elem.id ||= index(elem)
|
395
|
+
json << %Q({"id":"#{elem.id}","cell":[)
|
396
|
+
couples = elem.attributes.symbolize_keys
|
397
|
+
attributes.each do |atr|
|
398
|
+
value = get_atr_value(elem, atr, couples)
|
399
|
+
value = escape_javascript(value) if value and value.is_a? String
|
400
|
+
json << %Q("#{value}",)
|
401
|
+
end
|
402
|
+
json.chop! << "]},"
|
403
|
+
end
|
404
|
+
json.chop! << "]}"
|
405
|
+
else
|
406
|
+
json << "}"
|
407
|
+
end
|
408
|
+
end
|
409
|
+
|
410
|
+
private
|
411
|
+
|
412
|
+
def get_atr_value(elem, atr, couples)
|
413
|
+
if atr.to_s.include?('.')
|
414
|
+
value = get_nested_atr_value(elem, atr.to_s.split('.').reverse)
|
415
|
+
else
|
416
|
+
value = couples[atr]
|
417
|
+
value = elem.send(atr.to_sym) if value.blank? && elem.respond_to?(atr) # Required for virtual attributes
|
418
|
+
end
|
419
|
+
value
|
420
|
+
end
|
421
|
+
|
422
|
+
def get_nested_atr_value(elem, hierarchy)
|
423
|
+
return nil if hierarchy.size == 0
|
424
|
+
atr = hierarchy.pop
|
425
|
+
raise ArgumentError, "#{atr} doesn't exist on #{elem.inspect}" unless elem.respond_to?(atr)
|
426
|
+
nested_elem = elem.send(atr)
|
427
|
+
return "" if nested_elem.nil?
|
428
|
+
value = get_nested_atr_value(nested_elem, hierarchy)
|
429
|
+
value.nil? ? nested_elem : value
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
class Array
|
434
|
+
include JqgridJson
|
435
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
def jqgrid_from_xml(name, opts={})
|
5
|
+
@@grid_name = name.to_s
|
6
|
+
grid_data = YAML::load_file("#{RAILS_ROOT}/config/jquery/jqGrid/#{name}.yml")
|
7
|
+
|
8
|
+
before = grid_data[:before]
|
9
|
+
params = grid_data[:params]
|
10
|
+
|
11
|
+
def param_body(value)
|
12
|
+
value.scan(/^\((.+?)\)(.+)/).flatten
|
13
|
+
end
|
14
|
+
|
15
|
+
jquery_grid_code = <<-CODE
|
16
|
+
<script type="text/javascript">
|
17
|
+
<%= before %>_n_
|
18
|
+
jQuery(document).ready(function(){
|
19
|
+
jQuery("#<%= name %>").jqGrid({
|
20
|
+
<% params.each do |key,value| %>
|
21
|
+
<% if opts.keys.include?(key) %>
|
22
|
+
_ss_<%= key %>: <%= opts[key].to_json %>,
|
23
|
+
<% elsif (value.is_a? String) && (value[0..0] == "$") %>
|
24
|
+
_ss_<%= key %>: <%= "jQuery"+value[1,value.length] %>,
|
25
|
+
<% elsif (key.to_s[0..8] == 'function_') %>
|
26
|
+
<% js_params, body = param_body(value) %>
|
27
|
+
_ss_<%= key.to_s[9..-1] %>: function(<%= js_params %>) {
|
28
|
+
_ss__ss_<%= body %>
|
29
|
+
_ss_},
|
30
|
+
<% else %>
|
31
|
+
_ss_<%= key %>: <%= value.to_json %>,
|
32
|
+
<% end %>
|
33
|
+
<% end %>
|
34
|
+
<% opts.each do |key,value| %>
|
35
|
+
<% unless params.keys.include?(key) %>
|
36
|
+
_ss_<%= key %>: <%= value.to_json %>,
|
37
|
+
<% end %>
|
38
|
+
<% end %> });
|
39
|
+
});
|
40
|
+
</script>
|
41
|
+
CODE
|
42
|
+
::ERB.new(jquery_grid_code, nil, '>').result(binding).gsub("_n_", "\n").
|
43
|
+
gsub(/[ \t]{2,}/,' ').
|
44
|
+
gsub(/,\s+\}\);/, "\n });").
|
45
|
+
gsub(/\n\s*\n/m, "\n").
|
46
|
+
gsub("_ss_", " ").
|
47
|
+
gsub("<grid_name>", @@grid_name)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|