Skip to content

Commit

Permalink
Merge branch 'jquery_and_prototype'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri Leikind committed Sep 8, 2010
2 parents 4748af0 + 0eec430 commit 7f6b393
Show file tree
Hide file tree
Showing 36 changed files with 668 additions and 169 deletions.
30 changes: 25 additions & 5 deletions README.rdoc
Expand Up @@ -2,6 +2,7 @@

Version:: 0.5
Author:: Yuri Leikind
Contributor:: Xavier Defrang
Homepage:: http://leikind.org/pages/wicegrid
Examples online:: http://grid.leikind.org
News:: http://leikind.org/wicegrid
Expand Down Expand Up @@ -33,6 +34,8 @@ than 2.1 can still be functional depending on the functionalities used.

WiceGrid is known to work with MySQL and Postgres.

WiceGrid works with Prototype and jQuery (experimental).

WiceGrid is compliant with Ruby 1.9.1.

=== Examples
Expand All @@ -46,7 +49,7 @@ Rails version 2.1.0 or newer.

<tt>will_paginate[http://github.com/mislav/will_paginate/wikis]</tt> version 2.3.2 or newer.

<tt>Prototype[http://www.prototypejs.org]</tt> version 1.5.1 or newer.
<tt>Prototype[http://www.prototypejs.org]</tt> version 1.5.1 or newer, or <tt>jQuery[http://jquery.com/]</tt>.

== How-To

Expand All @@ -56,13 +59,30 @@ Run the following command to install the plugin:

script/plugin install git://github.com/leikind/wice_grid.git

To copy the plugin initializer, icons, javascripts, and stylesheets to correct location, run the following command:
If your application is Prototype-based (default in Rails 2.x), run the following generator to copy all plugin assets:

./script/generate wice_grid_assets
./script/generate wice_grid_assets_prototype

For jQuery:

./script/generate wice_grid_assets_jquery

This will copy images, stylesheets, configuration file <tt>config/initializers/wice_grid_config.rb</tt>, and the correct version
of javascript files.

=== Support for Prototype and jQuery

WiceGrid started as a plugin using the Prototype javascript framework. Support for jQuery was added for version 0.6.
The plugin contains two versions of the main javascript file <tt>wice_grid.js</tt>, and depending on the generator run to
install the assets (+wice_grid_assets_prototype+ or +wice_grid_assets_jquery+) the correct file is copied into <tt>public/javascripts</tt>.

Using ./script/generate to copy assets gives the advantage of collision control - if a file with the same name already
exists at the given location, the script will pause and ask the user about what action to take.
Some javascript code is also generated and injected into the HTML page. The value of constant <tt>Wice::Defaults::JS_FRAMEWORK</tt> in
configuration file <tt>wice_grid_config.rb</tt> defines for which JS framework code should be generated.
Generators +wice_grid_assets_prototype+ and +wice_grid_assets_jquery+ create a version of <tt>wice_grid_config.rb</tt>
with the corresponding value of <tt>Wice::Defaults::JS_FRAMEWORK</tt>.

THERE IS TEMPORARILY NO JAVASCRIPT CALENDAR IN THE JQUERY VERSION AND ALL DATE/DATETIME FILTERS FALL BACK TO THE DEFAULT
RAILS DATE/DATETIME HELPERS.

=== Basics

Expand Down
@@ -1,5 +1,8 @@
if defined?(Wice::Defaults)

Wice::Defaults::JS_FRAMEWORK = :<%= active_js_framework %>
# Wice::Defaults::JS_FRAMEWORK = :<%= inactive_js_framework %>

# Style of the view helper.
# +false+ is a usual view helper.
# +true+ will allow to embed erb content in column (cell) definitions.
Expand Down
@@ -1,4 +1,9 @@
en:
date:
order:
- :year
- :month
- :day
wice_grid:
show_filter_tooltip: Show filter
hide_filter_tooltip: Hide filter
Expand Down Expand Up @@ -41,6 +46,11 @@ en:


is:
date:
order:
- :year
- :month
- :day
wice_grid:
show_filter_tooltip: Sýna síumöguleika
hide_filter_tooltip: Fela síumoguleika
Expand Down Expand Up @@ -80,6 +90,11 @@ is:
query_saved_message: leitaskipun hefur verið vistuð

nl:
date:
order:
- :year
- :month
- :day
wice_grid:
show_filter_tooltip: Filter tonen
hide_filter_tooltip: Filter verbergen
Expand Down Expand Up @@ -119,6 +134,11 @@ nl:
query_saved_message: Query opgeslagen

fr:
date:
order:
- :year
- :month
- :day
wice_grid:
show_filter_tooltip: Afficher le filtre
hide_filter_tooltip: Cacher le filtrer
Expand Down Expand Up @@ -158,6 +178,11 @@ fr:
query_saved_message: La requête a été sauvée.

ru:
date:
order:
- :year
- :month
- :day
wice_grid:
show_filter_tooltip: Показать фильтр
hide_filter_tooltip: Спрятать фильтр
Expand Down Expand Up @@ -200,6 +225,11 @@ ru:


pt:
date:
order:
- :year
- :month
- :day
wice_grid:
show_filter_tooltip: Mostrar o filtro
hide_filter_tooltip: Esconder o filtro
Expand Down
6 changes: 6 additions & 0 deletions generators/wice_grid_assets_jquery/templates/USAGE
@@ -0,0 +1,6 @@
Description:
Copies
configuration file wice_grid_config.rb to config/initializers/
wice_grid_jquery.js to public/javascripts/wice_grid.js
wice_grid.css to public/stylesheets/
wice_grid images to public/images/icons/grid
@@ -0,0 +1,155 @@
function WiceGridProcessor(name, base_request_for_filter, base_link_for_show_all_records,
link_for_export, parameter_name_for_query_loading, parameter_name_for_focus, environment){

this.checkIfJsFrameworkIsLoaded = function(){
if (! jQuery){
alert("jQuery not loaded, WiceGrid cannot proceed!")
}
}

this.checkIfJsFrameworkIsLoaded();
this.name = name;
this.parameter_name_for_query_loading = parameter_name_for_query_loading;
this.parameter_name_for_focus = parameter_name_for_focus;
this.base_request_for_filter = base_request_for_filter;
this.base_link_for_show_all_records = base_link_for_show_all_records;
this.link_for_export = link_for_export;
this.filter_declarations = new Array();
this.environment = environment;

this.toString = function(){
return "<WiceGridProcessor instance for grid '" + this.name + "'>";
}


this.process = function(dom_id_to_focus){
window.location = this.build_url_with_params(dom_id_to_focus);
}

this.reload_page_for_given_grid_state = function(grid_state){
var request_path = this.grid_state_to_request(grid_state);
window.location = this.append_to_url(this.base_link_for_show_all_records, request_path);
}

this.load_query = function(query_id){
var request = this.append_to_url(this.build_url_with_params(),
(this.parameter_name_for_query_loading + encodeURIComponent(query_id)));
window.location = request;
}

this.save_query = function(query_name, base_path_to_query_controller, grid_state, input_ids){
if (input_ids instanceof Array) {
input_ids.each(function(dom_id){
grid_state.push(['extra[' + dom_id + ']', $('#'+ dom_id)[0].value])
});
}

var request_path = this.grid_state_to_request(grid_state);

jQuery.ajax({
url: base_path_to_query_controller,
async: true,
data: request_path + '&query_name=' + encodeURIComponent(query_name),
dataType: 'script',
type: 'POST'
});
}

this.grid_state_to_request = function(grid_state){
return jQuery.map(grid_state, function(pair){
return encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1]);
}).join('&');
}


this.append_to_url = function(url, str){
var sep;
if (url.indexOf('?') != -1){
if (/[&\?]$/.exec(url)){
sep = '';
}else{
sep = '&';
}
}else{
sep = '?';
}
return url + sep + str;
}


this.build_url_with_params = function(dom_id_to_focus){
var results = new Array();
var _this = this;
jQuery.each(this.filter_declarations, function(i, filter_declaration){
param = _this.read_values_and_form_query_string(
filter_declaration.filter_name, filter_declaration.detached,
filter_declaration.templates, filter_declaration.ids);
if (param && param != ''){
results.push(param);
}
});

var res = this.base_request_for_filter;
if ( results.length != 0){
all_filter_params = results.join('&');
res = this.append_to_url(res, all_filter_params);
}
if (dom_id_to_focus){
res = this.append_to_url(res, this.parameter_name_for_focus + dom_id_to_focus);
}
return res;
}

this.reset = function(){
window.location = this.base_request_for_filter;
}

this.export_to_csv = function(){
window.location = this.link_for_export;
}

this.register = function(func){
this.filter_declarations.push(func);
}

this.read_values_and_form_query_string = function(filter_name, detached, templates, ids){
var res = new Array();
for(i = 0; i < templates.length; i++){
if($(ids[i]) == null){
if (this.environment == "development"){
message = 'WiceGrid: Error reading state of filter "' + filter_name + '". No DOM element with id "' + ids[i] + '" found.'
if (detached){
message += 'You have declared "' + filter_name +
'" as a detached filter but have not output it anywhere in the template. Read documentation about detached filters.'
}
alert(message);
}
return '';
}
var val = $('#' + ids[i]).val();
if (val instanceof Array) {
for(j = 0; j < val.length; j++){
if (val[j] && val[j] != "")
res.push(templates[i] + encodeURIComponent(val[j]));
}
} else if (val && val != ''){
res.push(templates[i] + encodeURIComponent(val));
}
}
return res.join('&');
}

};

function toggle_multi_select(select_id, link_obj, expand_label, collapse_label) {
var select = $('#' + select_id)[0];
if (select.multiple == true) {
select.multiple = false;
link_obj.title = expand_label;
} else {
select.multiple = true;
link_obj.title = collapse_label;
}
}

WiceGridProcessor._version = '0.4.2';
@@ -0,0 +1,35 @@
class WiceGridAssetsJqueryGenerator < Rails::Generator::Base
def active_js_framework
'jquery'
end
def inactive_js_framework
'prototype'
end

def manifest
record do |m|
# wice_grid config
m.directory "config/initializers"
@active_js_framework = 'jquery'
@inactive_js_framework = 'prototype'
m.template "../../common_templates/initializers/wice_grid_config.rb", "config/initializers/wice_grid_config.rb"

# wice_grid locales
m.directory "config/locales"
m.file "../../common_templates/locales/wice_grid.yml", "config/locales/wice_grid.yml"

# wice_grid js & css
m.file "javascripts/wice_grid_jquery.js", "public/javascripts/wice_grid.js"
m.file "../../common_templates/stylesheets/wice_grid.css", "public/stylesheets/wice_grid.css"

# images
m.directory "public/images/icons/grid"

%w(arrow_down.gif calendar_view_month.png expand.png page_white_find.png table_refresh.png
arrow_up.gif delete.png page_white_excel.png table.png tick_all.png untick_all.png ).each do |f|
m.file "../../common_templates/icons/#{f}", "public/images/icons/grid/#{f}"
end

end
end
end
@@ -1,7 +1,7 @@
Description:
Copies
configuration file wice_grid_config.rb to config/initializers/
wice_grid.js to public/javascripts/
wice_grid_prototype.js to public/javascripts/wice_grid.js
wice_grid.css to public/stylesheets/
calendarview.js to public/javascripts/
calendarview.css to public/stylesheets/
Expand Down

0 comments on commit 7f6b393

Please sign in to comment.