Skip to content

Instantly share code, notes, and snippets.

@leikind
Created July 6, 2011 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leikind/1067162 to your computer and use it in GitHub Desktop.
Save leikind/1067162 to your computer and use it in GitHub Desktop.
wice_grid.coffee.js
var WiceGridProcessor, toggle_multi_select;
WiceGridProcessor = function(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) {
return 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) {
return window.location = this.build_url_with_params(dom_id_to_focus);
};
this.reload_page_for_given_grid_state = function(grid_state) {
var request_path;
request_path = this.grid_state_to_request(grid_state);
return window.location = this.append_to_url(this.base_link_for_show_all_records, request_path);
};
this.load_query = function(query_id) {
var request;
request = this.append_to_url(this.build_url_with_params(), this.parameter_name_for_query_loading + encodeURIComponent(query_id));
return window.location = request;
};
this.save_query = function(field_id, query_name, base_path_to_query_controller, grid_state, input_ids) {
var request_path;
if (input_ids instanceof Array) {
input_ids.each(dom_id)(function() {
return grid_state.push(['extra[' + dom_id + ']', $('#' + dom_id)[0].value]);
});
}
request_path = this.grid_state_to_request(grid_state);
return jQuery.ajax({
url: base_path_to_query_controller,
async: true,
data: request_path + '&query_name=' + encodeURIComponent(query_name),
dataType: 'script',
type: 'POST',
success: function() {
return $('#' + field_id).val('');
}
});
};
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 all_filter_params, res, results, _this;
results = new Array();
_this = this;
jQuery.each(this.filter_declarations, function(i, filter_declaration) {
var param;
param = _this.read_values_and_form_query_string(filter_declaration.filter_name, filter_declaration.detached, filter_declaration.templates, filter_declaration.ids);
if (param && param !== '') {
return results.push(param);
}
});
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() {
return window.location = this.base_request_for_filter;
};
this.export_to_csv = function() {
return window.location = this.link_for_export;
};
this.register = function(func) {
return this.filter_declarations.push(func);
};
this.read_values_and_form_query_string = function(filter_name, detached, templates, ids) {
var el, i, j, message, res, val, _ref, _ref2;
res = new Array();
for (i = 0, _ref = templates.length - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : 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 '';
}
el = $('#' + ids[i]);
if (el[0] && el[0].type === 'checkbox') {
if (el[0].checked) {
val = 1;
}
} else {
val = el.val();
}
if (val instanceof Array) {
for (j = 0, _ref2 = val.length - 1; 0 <= _ref2 ? j <= _ref2 : j >= _ref2; 0 <= _ref2 ? j++ : 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('&');
};
return this;
};
toggle_multi_select = function(select_id, link_obj, expand_label, collapse_label) {
var select;
select = $('#' + select_id)[0];
if (select.multiple === true) {
select.multiple = false;
return link_obj.title = expand_label;
} else {
select.multiple = true;
return link_obj.title = collapse_label;
}
};
WiceGridProcessor._version = '0.4.3';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment