var IndexTable = function(params) { this.init(params); }
IndexTable.prototype = {
//============================================================================
// Required parameters
//============================================================================
form_authenticity_token: false,
// Base URL used for default refresh/update/delete/bulk URLs
base_url: false,
// Where to get json data for all the models
refresh_url: false,
// Where to send bulk updates
bulk_update_url: false,
// Where to send bulk deletes
bulk_delete_url: false,
// When to send bulk import CSV data
bulk_import_url: false,
// Where to post new models
add_url: false,
after_add: 'refresh', // or 'redirect'
// Array of fields you want to show in the table (given as model binder attributes with additional text and value functions)
// [{
// show: true,
// bulk_edit: true,
// name: 'title',
// nice_name: 'Title',
// type: 'text',
// text: function(obj) { return obj.title; },
// value: function(obj) { return obj.id; },
// options_url: '/admin/dogs/collar-options'
// fixed_placeholder: false,
// width: 200,
// }]
fields: [],
//============================================================================
// Additional parameters
//============================================================================
// Container for the table
container: 'models',
// Where to get json data for a single model
refresh_single_url: function(model_id, it) { return it.base_url + '/' + model_id + '/json'; },
// Where to send normal updates
update_url: function(model_id, it) { return it.base_url + '/' + model_id; },
// Where to send duplicate calls
duplicate_url: function(model_id, it) { return it.base_url + '/' + model_id + '/duplicate'; },
// What to do when the edit button is clicked for a model
edit_click_handler: function(model_id, it) { window.location = it.base_url + '/' + model_id; },
// What to do when a row is click. Default is quick edit mode.
row_click_handler: function(model_id, it, e) {
if (it.quick_edit_model_id == model_id)
{
if ($(e.target).prop('tagName') == 'TD')
it.quick_edit_model_id = false;
else
return;
}
else
it.quick_edit_model_id = model_id;
it.print();
},
allow_add: true,
allow_bulk_edit: true,
allow_bulk_delete: true,
allow_bulk_import: true,
allow_duplicate: true,
allow_advanced_edit: true,
bulk_import_fields: false,
no_models_text: "There are no models right now.",
new_model_text: 'New',
new_model_fields: [{ name: 'name', nice_name: 'Name', type: 'text', width: 400 }],
search_fields: false,
custom_row_controls: false,
after_print: false,
table_class: 'data',
//============================================================================
// End of parameters
//============================================================================
models: [], // The models we get from the server
model_ids: [], // IDs of currently selected models
quick_edit_model_id: false, // The id of the model currently being edited
pager: {
options: { page: 1 },
params: {}
},
add_to_url: function(url, str)
{
if (url.indexOf('?') > -1)
{
bu = url.split('?');
return bu.shift() + str + '?' + bu.join('?');
}
return url + str;
},
// Constructor
init: function(params) {
for (var thing in params)
this[thing] = params[thing];
var that = this;
if (!this.refresh_url ) this.refresh_url = this.add_to_url(this.base_url, '/json');
if (!this.bulk_update_url ) this.bulk_update_url = this.add_to_url(this.base_url, '/bulk');
if (!this.bulk_delete_url ) this.bulk_delete_url = this.add_to_url(this.base_url, '/bulk');
if (!this.add_url ) this.add_url = this.base_url;
$.each(this.fields, function(i, f) {
if (f.editable == null) f.editable = true;
});
this.init_local_storage();
this.get_visible_columns();
$(window).on('hashchange', function() { that.refresh(); });
this.refresh();
},
parse_querystring: function()
{
var b = {};
// Get the querystring values
a = window.location.search;
if (a.length > 0)
{
a = a.substr(1).split('&');
for (var i=0; i Refreshing...
Please select at least one row.
", 'bulk_edit_select_row'); return; } var div = $('').addClass('note') .append($('').html('Bulk Edit Jobs')) .append($('').html('Any change you make the fields below will apply to all the selected rows.')); $.each(this.fields, function(i, field) { if (field.bulk_edit == true) div.append($('').append($('').attr('id', 'bulkmodel_1_' + field.name))); }); div.append($('').append($('').attr('type','button').val('Finished').click(function() { that.hide_message(); }))); that.show_message(div, 'bulk_edit_form'); var params = this.model_ids.map(function(model_id) { return 'model_ids[]=' + model_id; }).join('&'); var attribs = []; $.each(this.fields, function(i, field) { if (field.bulk_edit == true) { var attrib = $.extend({}, field); attrib['value'] = ''; attrib['after_update'] = function() { that.refresh(); }; attrib['width'] = 600; attribs.push(attrib); } }); m = new ModelBinder({ name: 'BulkModel', id: 1, update_url: that.bulk_update_url + '?' + params, authenticity_token: this.form_authenticity_token, attributes: attribs }); }, bulk_delete: function(confirm) { var that = this; if (this.model_ids.length == 0) { that.show_message("Please select at least one row.
", 'bulk_delete_select_row'); return; } if (!confirm) { var p = $('').addClass('note').addClass('warning') .append('Are you sure you want to delete the selected rows? ') .append($('').attr('type','button').val('Yes').click(function() { that.bulk_delete(true); })).append(' ') .append($('').attr('type','button').val('No').click(function() { that.hide_message(); })); that.show_message(p, 'bulk_delete_confirm'); return; } var params = this.model_ids.map(function(model_id) { return 'model_ids[]=' + model_id; }).join('&'); $('#' + this.container).html("Refreshing...
"); $.ajax({ url: that.bulk_delete_url, type: 'delete', data: { model_ids: that.model_ids }, success: function(resp) { that.hide_message(); that.refresh(); } }); }, bulk_import: function(data, data_url) { var that = this; if (!data && !data_url) { var div = $('').addClass('note') .append($('').html('Bulk Import')) //.append($('').html("Enter either the URL where CSV data can be downloaded or the CSV data.")) //.append($('') // .append('CSV Data URL').append('Adding...
", 'bulk_import_loading'); $.ajax({ url: this.bulk_import_url, type: 'post', data: { csv_data: data, csv_data_url: data_url }, success: function(resp) { if (resp.error) that.show_message("" + resp.error + "
", 'bulk_import_error'); else { that.show_message("Added successfully.
", 'bulk_import_success'); setTimeout(function() { that.hide_message(); }, 3000); that.refresh(); } } }); }, model_for_id: function(model_id) { for (var i=0; iDuplicating...
", 'duplicate_loading'); $.ajax({ url: that.duplicate_url(that.model_ids[0], that), type: 'post', data: { count: count }, success: function(resp) { if (resp.error) that.show_message("" + resp.error + "
", 'duplicate_error'); if (resp.success) { that.hide_message(); that.refresh(); } } }); }, pager_div: function(summary) { var that = this; var p = this.pager; // Set default parameter values if not present if (!p.options.items_per_page) p.options.items_per_page = 10 if (!p.options.page) p.options.page = 1 var page = parseInt(p.options.page); // Max links to show (must be odd) var total_links = 5; var prev_page = page - 1; var next_page = page + 1; var total_pages = Math.ceil(parseFloat(p.options.item_count)/parseFloat(p.options.items_per_page)); var start = 1; var stop = 1; if (total_pages < total_links) { start = 1; stop = total_pages; } else { start = page - Math.floor(total_links/2); if (start < 1) start = 1; stop = start + total_links - 1 if (stop > total_pages) { stop = total_pages; start = stop - total_links; if (start < 1) start = 1; } } var div = $('').addClass('pager'); if (summary) div.append($('').html("Results: showing page " + page + " of " + total_pages)); if (total_pages > 1) { var div2 = $('').addClass('page_links'); if (page > 1) div2.append($('').attr('href', this.pager_hash({ page: prev_page })).html('Previous').click(function(e) { that.hash_click(e, this); })); for (i=start; i<=stop; i++) { if (page != i) div2.append($('').attr('href', this.pager_hash({ page: i })).html(i).click(function(e) { that.hash_click(e, this); })); else div2.append($('').addClass('current_page').html(i)); } if (page < total_pages) div2.append($('').attr('href', this.pager_hash({ page: next_page })).html('Next').click(function(e) { that.hash_click(e, this); })); div.append(div2); } return div; }, hash_click: function(e, el) { e.preventDefault(); e.stopPropagation(); window.location.hash = $(el).attr('href').substr(1); this.refresh(); }, pager_params: function(h) { var that = this; var skip = this.pager.options && this.pager.options.skip ? this.pager.options.skip : []; var p = {}; for (var i in this.pager.params) if (skip.indexOf(i) == -1) p[i] = this.pager.params[i]; if (this.pager.options) { if (this.pager.options.sort) p.sort = this.pager.options.sort; if (this.pager.options.desc) p.desc = this.pager.options.desc ? 1 : 0; if (this.pager.options.page) p.page = this.pager.options.page; } if (h) { for (var i in h) { if (typeof(h[i]) == 'boolean') p[i] = h[i] ? 1 : 0; else p[i] = h[i]; } } return p; }, pager_hash: function(h) { var p = this.pager_params(h); var qs = []; $.each(p, function(k,v) { if (k != '[object Object]') qs.push('' + k + '=' + encodeURIComponent(v)); }); return '#' + qs.join('&'); }, /****************************************************************************/ new_model_link: function() { var that = this; return $('').attr('href', '#').html(that.new_model_text).click(function(e) { e.preventDefault(); that.new_form(); }); }, hide_message: function() { var that = this; $('#' + that.container + '_message').slideUp(function() { $('#' + that.container + '_message').empty().css('margin-bottom', 0); }); that.current_message = false }, current_message: false, show_message: function(el, name, after) { var that = this; if (that.current_message == name) { $('#' + that.container + '_message').slideUp(function() { $('#' + that.container + '_message').empty().css('margin-bottom', 0); }); that.current_message = false; if (after) after(); return; } if (!$('#' + that.container + '_message').is(':empty')) { $('#' + that.container + '_message').slideUp(function() { $('#' + that.container + '_message').empty().append(el).css('margin-bottom', '10px').slideDown(); if (after) after(); }); that.current_message = name; return; } else { $('#' + that.container + '_message').hide().empty().append(el).css('margin-bottom', '10px').slideDown(); that.current_message = name; if (after) after(); } }, new_form: function() { var that = this; $.each(this.new_model_fields, function(i, f) { if (f.options_url && !f.options) { $.ajax({ url: f.options_url, type: 'get', success: function(resp) { f.options = resp }, async: false }); } }); var form = $('').attr('id', 'new_form') .append($('').attr('type', 'hidden').attr('name', 'authenticity_token').val(that.form_authenticity_token)); var focus_field = false; $.each(this.new_model_fields, function(i, f) { if (f.type == 'hidden') form.append($('').attr('type', 'hidden').attr('name', f.name).val(f.value)); else if (f.type == 'select') { var select = $('').attr('name', f.name); $.each(f.options, function(i, option) { var opt = $('').val(option.value).html(option.text); if (f.value && f.value == option.value) opt.attr('selected', 'true'); select.append(opt); }); form.append(select); } else // text { form.append($('').append($('').attr('type', 'text').attr('id', 'new_form_' + f.name).attr('name', f.name).attr('placeholder', f.nice_name).css('width', '' + f.width + 'px'))); if (!focus_field) focus_field = f.name; } }); form.append($('').attr('id', that.container + '_new_message')); form.append($('') .append($('').attr('type', 'button').val('Cancel').click(function(e) { that.hide_message(); })).append(' ') .append($('').attr('type', 'submit').val('Add').click(function(e) { that.add_model(); return false; })) ); var div = $('
').addClass('note') .append($('').css('margin-top', 0).css('padding-top', 0).html(that.new_model_text)) .append(form); that.show_message(div, null, function() { $('#new_form input[name="' + focus_field + '"]').focus(); $.each(that.new_model_fields, function(i, f) { if (f.type == 'date') { $('#new_form_' + f.name).datetimepicker({ format: f.date_format ? f.date_format : 'm/d/Y', timepicker: false }); } }); }); }, add_model: function() { var that = this; $('#' + that.container + '_new_message').html("Adding...
"); $.ajax({ url: this.add_url, type: 'post', data: $('#new_form').serialize(), success: function(resp) { if (resp.error) $('#' + that.container + '_new_message').html("" + resp.error + "
"); if (resp.redirect && that.after_add == 'redirect') window.location = resp.redirect; else if (resp.redirect || resp.refresh || resp.success) { that.hide_message(); that.refresh(); } } }); }, //============================================================================ // Seach Form //============================================================================ toggle_search_form: function() { var that = this; var form = that.search_form(); that.show_message(form, 'toggle_search_form'); }, search_form: function() { var that = this; var pp = that.pager_params(); var tbody = $(''); $.each(that.search_fields, function(i, f) { var tr = $('