Module: JqGridRails::Helpers
- Includes:
- RailsJavaScriptHelpers
- Defined in:
- lib/jqgrid_rails/jqgrid_rails_helpers.rb
Instance Method Summary collapse
-
#build_default_callback(hash) ⇒ Object
- hash
-
Argument hash Builds a default callback based on argument hash.
-
#build_selection_callback(hash, table_id = nil) ⇒ Object
- hash
-
Argument has Builds callback function for full selection NOTE: In general you will want the URL to be auto generated within jqgrid_rails.
-
#build_single_callback(hash) ⇒ Object
- hash
-
Argument hash Builds callback function for single selection.
-
#build_toolbar_button(url_hash) ⇒ Object
- url_hash
-
hash for url building Creates a toolbar button for the grid TODO: Put confirm in here.
-
#confirm_if_required(confirm, contents) ⇒ Object
- confirm
- Confirmation string contents
-
JS contents for if block Wraps contents within if block using confirm() as conditional.
-
#convert_dom_id(id) ⇒ Object
- id
-
String or RawJS object Ensures a DOM ID is returned from id.
- #csrf_token_discovery(method) ⇒ Object
-
#extract_callback_variables(hash) ⇒ Object
- hash
-
Argument hash Extracts and formats argument hash for callbacks.
-
#hash_to_callback(hash) ⇒ Object
- hash
-
Argument hash for callback generation Generates a javascript callback from a Ruby hash.
-
#map_click(key, options) ⇒ Object
- key
-
ondbl_click_row/on_select_row Sets up click event functions based on hash values.
-
#scrub_options_hash(options_hash) ⇒ Object
- options_hash
-
Hash of options Inserts callbacks in any applicable values.
-
#selection_array(error_when_empty = true, table_id = nil) ⇒ Object
Returns callable function to get current selection in array form.
Instance Method Details
#build_default_callback(hash) ⇒ Object
- hash
-
Argument hash
Builds a default callback based on argument hash. No interaction with grid is provided via this method
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 164 def build_default_callback(hash) args = extract_callback_variables(hash) confirm = args.delete(:confirm) if(hash[:remote]) "function(){ #{confirm_if_required(confirm, "jQuery.ajax(#{format_type_to_js(args[:url])}#{args[:args_replacements]}, #{format_type_to_js(args[:ajax_args])});")} }" else randomizer = rand(99999) output = " function(){ #{csrf_token_discovery(args[:method])} jQuery('body').append('<form id=\"jqgrid_redirector_#{randomizer}\" action=\"#{args[:url]}#{args[:args_replacements]}\" method=\"#{args[:method]}\">' + csrf_token + '</form>');" if(hash[:ajax_args] && hash[:ajax_args][:data]) output << "var args = #{format_type_to_js(hash[:ajax_args][:data])}; jQuery(Object.keys(args)).each(function(idx,key){ jQuery('#{format_id("jqgrid_redirector_#{randomizer}")}').append(jQuery('<input/>') .attr('type', 'hidden') .attr('name', key) .val(args[key]) ); });" end output << "#{confirm_if_required(confirm, "jQuery(#{format_type_to_js(format_id("jqgrid_redirector_#{randomizer}"))}).submit();")} }" end end |
#build_selection_callback(hash, table_id = nil) ⇒ Object
- hash
-
Argument has
Builds callback function for full selection NOTE: In general you will want the URL to be auto generated within jqgrid_rails. The route should accept an ID which will be the first ID of the current selection. An extra parameter named ‘ids’ will be provided which will be an array of all selected values, included the ID given to the route
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 128 def build_selection_callback(hash, table_id=nil) dom_id = table_id || @table_id hash[:id_replacement] ||= '000' hash[:args] = Array(hash[:args]) unless hash[:args].is_a?(Array) hash[:args].push hash[:id_replacement] args = extract_callback_variables(hash) confirm = args.delete(:confirm) item_id = args[:item_id].present? ? args[:item_id] : RawJS.new('id') function = "function(){ rows_func = #{selection_array(true, table_id)} ary = rows_func(); if(!ary.length){ return false; } " if(hash[:remote]) args[:ajax_args][:data] = {item_id.to_s.pluralize.to_sym => RawJS.new('ary')} if(hash[:with_row]) args[:ajax_args][:data][:row_data] = RawJS.new("jQuery(#{convert_dom_id(dom_id)}).jqGrid('getRowData')") end function << confirm_if_required(confirm, "jQuery.ajax(#{format_type_to_js(args[:url])}.replace(#{format_type_to_js(args[:id_replacement])}, ary[0])#{args[:args_replacements]}, #{format_type_to_js(args[:ajax_args])}); }") else randomizer = rand(99999) function << "parts = ary.map( function(item){ return '<input type=\"hidden\" name=\"#{item_id.to_s.pluralize}[]\" value=\"'+item+'\"/>'; }); #{csrf_token_discovery(args[:method])} var target_url = #{format_type_to_js(args[:url])}.replace(#{format_type_to_js(args[:id_replacement])}, ary[0])#{args[:args_replacements]}; jQuery('body').append('<form id=\"jqgrid_redirector_#{randomizer}\" action=\"'+ target_url +'\" method=\"#{args[:method]}\">' + parts + csrf_token + '</form>'); #{confirm_if_required(confirm, "jQuery(#{format_type_to_js(format_id("jqgrid_redirector_#{randomizer}"))}).submit();")} }" end end |
#build_single_callback(hash) ⇒ Object
- hash
-
Argument hash
Builds callback function for single selection
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 73 def build_single_callback(hash) hash[:id_replacement] ||= '000' hash[:args] = Array(hash[:args]) unless hash[:args].is_a?(Array) hash[:args].push hash[:id_replacement] args = extract_callback_variables(hash) confirm = args.delete(:confirm) item_id = args[:item_id].present? ? args[:item_id] : RawJS.new('id') if(hash[:remote]) if(hash[:with_row]) args[:ajax_args] ||= {} args[:ajax_args][:data] = {:row_data => RawJS.new("jQuery(#{convert_dom_id(@table_id)}).jqGrid('getRowData', id)")} end " function(id){ #{confirm_if_required( confirm, "jQuery.ajax(#{format_type_to_js(args[:url])}.replace(#{format_type_to_js(args[:id_replacement])}, #{format_type_to_js(item_id)})#{args[:args_replacements]}, #{format_type_to_js(args[:ajax_args])});" )} } " else form_rand = rand(999) " function(id){ #{csrf_token_discovery(args[:method])} #{confirm_if_required( confirm, "jQuery('body').append('<form id=\"redirector_#{form_rand}\" action=\"#{args[:url]}\" method=\"#{args[:method]}\">'.replace(#{format_type_to_js(args[:id_replacement])}, #{format_type_to_js(item_id)})#{args[:args_replacements]} + csrf_token +'</form>'); jQuery('#redirector_#{form_rand}').submit();" )} } " end end |
#build_toolbar_button(url_hash) ⇒ Object
- url_hash
-
hash for url building
Creates a toolbar button for the grid TODO: Put confirm in here
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 201 def (url_hash) url_hash[:empty_selection] ||= url_hash[:single] url_hash[:build_callback] ||= :selection unless url_hash[:empty_selection] classes = ['grid_toolbar_item', 'button', 'ui-state-default', 'ui-corner-all'] s = <<-EOS jQuery('<div class="#{(classes + Array(url_hash[:class])).compact.join(' ')}" />') .text('#{escape_javascript(url_hash[:name])}') .button() .click( #{hash_to_callback(url_hash)} ).appendTo('#t_' + #{format_type_to_js(@table_id)}); EOS end |
#confirm_if_required(confirm, contents) ⇒ Object
- confirm
-
Confirmation string
- contents
-
JS contents for if block
Wraps contents within if block using confirm() as conditional
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 227 def confirm_if_required(confirm, contents) string = '' if(confirm) string << "if(confirm(#{format_type_to_js(confirm)})){" string << contents string << "}" else string << contents end string end |
#convert_dom_id(id) ⇒ Object
- id
-
String or RawJS object
Ensures a DOM ID is returned from id. Does simple string replacement client side to force a # prefix. Client side conversion allows the id to be a RawJS instance for dynamic grid IDs
15 16 17 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 15 def convert_dom_id(id) RawJS.new("#{format_type_to_js(id)}.replace(/^#*/, '#')") end |
#csrf_token_discovery(method) ⇒ Object
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 239 def csrf_token_discovery(method) output = "var csrf_token = '';" if(method.to_s.downcase == 'get') output else output << "if(jQuery('meta[name=\"csrf-param\"]').size() > 0){ csrf_token = '<input type=\"hidden\" name=\"'+jQuery('meta[name=\"csrf-param\"]').attr('content')+'\" value=\"'+jQuery('meta[name=\"csrf-token\"]').attr('content')+'\" />'; }" end end |
#extract_callback_variables(hash) ⇒ Object
- hash
-
Argument hash
Extracts and formats argument hash for callbacks
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 46 def extract_callback_variables(hash) @url_gen ||= JqGridRails::UrlGenerator.new args = hash.dup args[:ajax_args] = hash.delete(:ajax) || {} args[:method] = args[:ajax_args][:type] || args[:ajax_args].delete(:method) || hash.delete(:method) || 'get' if(hash[:url].is_a?(Symbol)) url_args = hash[:args].is_a?(Array) ? hash[:args] : [hash[:args]] args[:url] = @url_gen.send(hash[:url], *(url_args.sort_by{|x,y| if(x.is_a?(Hash) && y.is_a?(Hash)) then 0 elsif(x.is_a?(Hash)) then 1 else -1 end})) else args[:url] = hash[:url] end if(hash[:args_replacements].present?) if(hash[:args_replacements].is_a?(Hash)) args[:args_replacements] = hash[:args_replacements].map{|fake_id, js_id| "replace(#{format_type_to_js(fake_id)}, #{format_type_to_js(js_id)})" }.join('.') unless(args[:args_replacements].blank?) args[:args_replacements] = ".#{args[:args_replacements]}" end else args[:args_replacements] = hash[:args_replacements] end end args[:ajax_args][:type] = args[:method] if hash[:remote] args end |
#hash_to_callback(hash) ⇒ Object
- hash
-
Argument hash for callback generation
Generates a javascript callback from a Ruby hash. Important hash keys: :build_callback -> [false|:item|:selection] (false will stop callback from being created and simply return original hash) :url -> Symbol of route method name or string for actual url :args -> Arguments to be used when generating URL from :url value :method -> Request method (defaults to ‘get’) :ajax_args -> Arguments for jQuery.ajax options hash :remote -> [true|false] Request should be made via ajax :id_replacement -> Value used for dynamic ID replacement (generally not to be altered) :item_id -> Use for :item type callbacks to set URL generated ID if not the generic ‘id’ variable
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 29 def hash_to_callback(hash) if(hash.is_a?(Hash) && hash[:build_callback] != false && hash[:url]) case hash[:build_callback] when :item build_single_callback(hash) when :selection build_selection_callback(hash) else build_default_callback(hash) end else hash end end |
#map_click(key, options) ⇒ Object
- key
-
ondbl_click_row/on_select_row
Sets up click event functions based on hash values
191 192 193 194 195 196 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 191 def map_click(key, ) if([key].is_a?(Hash)) [key][:build_callback] = :item [key] = hash_to_callback([key]) end end |
#scrub_options_hash(options_hash) ⇒ Object
- options_hash
-
Hash of options
Inserts callbacks in any applicable values
217 218 219 220 221 222 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 217 def () .each do |key,value| [key] = hash_to_callback(value) end end |
#selection_array(error_when_empty = true, table_id = nil) ⇒ Object
Returns callable function to get current selection in array form
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/jqgrid_rails/jqgrid_rails_helpers.rb', line 107 def selection_array(error_when_empty=true, table_id=nil) dom_id = convert_dom_id(table_id || @table_id) " function(){ ary = jQuery(#{dom_id}).jqGrid('getGridParam', 'selarrrow'); if(!ary.length){ ary = []; } ary.push(jQuery(#{dom_id}).jqGrid('getGridParam', 'selrow')); ary = jQuery.grep(ary, function(value,key){ return value != null && value.length && jQuery.inArray(value, ary) === key; }); if(!ary.length && #{format_type_to_js(error_when_empty)}){ alert('Please select items from the table first.'); } return ary; } " end |