1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

* Fixed Datatables tools width

* Refactoring of API (again...)
This commit is contained in:
Adolfo Gómez 2013-11-22 11:33:54 +00:00
parent ceecb0823d
commit b7bfecf87c
11 changed files with 375 additions and 326 deletions

View File

@ -91,12 +91,23 @@ class ModelHandlerMixin(object):
def get(self): def get(self):
logger.debug('method GET for {0}, {1}'.format(self.__class__.__name__, self._args)) logger.debug('method GET for {0}, {1}'.format(self.__class__.__name__, self._args))
if len(self._args) == 0: if len(self._args) == 0:
result = []
for val in self.model.objects.all():
res = self.item_as_dict(val)
if hasattr(val, 'getInstance'):
for key, value in val.getInstance().valuesDict().iteritems():
res[key] = value
result.append(res)
return result
if self._args[0] == 'overview':
return list(self.getItems()) return list(self.getItems())
# If has detail and is requesting detail # If has detail and is requesting detail
if self.detail is not None and len(self._args) > 1: if self.detail is not None and len(self._args) > 1:
return self.processDetail() return self.processDetail()
try: try:
val = self.model.objects.get(pk=self._args[0]) val = self.model.objects.get(pk=self._args[0])
res = self.item_as_dict(val) res = self.item_as_dict(val)

View File

@ -128,28 +128,50 @@ BasicModelRest.prototype = {
}); });
} }
}, },
get : function(options) { get : function(success_fnc, options) {
"use strict"; "use strict";
options = options || {}; options = options || {};
var path = this.getPath; var path = this.getPath;
if (options.id !== undefined) if ( options.id )
path += '/' + options.id; path += '/' + options.id;
return this._requestPath(path, { return this._requestPath(path, {
cacheKey: '.', // Right now, do not cache this cacheKey: '.', // Right now, do not cache any "get" method
success: options.success, success: success_fnc,
}); });
}, },
types : function(options) { list: function(success_fnc, options) { // This is "almost" an alias for get
"use strict";
options = options || {};
return this.get(success_fnc, {
id: '',
});
},
overview: function(success_fnc, options) {
"use strict";
options = options || {};
return this.get(success_fnc, {
id: 'overview',
});
},
item: function(itemId, success_fnc, options) {
"use strict";
options = options || {};
return this.get(success_fnc, {
id: itemId,
});
},
types : function(success_fnc, options) {
"use strict"; "use strict";
options = options || {}; options = options || {};
return this._requestPath(this.typesPath, { return this._requestPath(this.typesPath, {
cacheKey: 'type', cacheKey: 'type',
success: options.success, success: success_fnc,
}); });
}, },
gui: function(typeName, options) { gui: function(typeName, success_fnc, options) {
// GUI returns a dict, that contains: // GUI returns a dict, that contains:
// name: Name of the field // name: Name of the field
// value: value of the field (selected element in choice, text for inputs, etc....) // value: value of the field (selected element in choice, text for inputs, etc....)
@ -159,31 +181,18 @@ BasicModelRest.prototype = {
var path = [this.typesPath, typeName, 'gui'].join('/'); var path = [this.typesPath, typeName, 'gui'].join('/');
return this._requestPath(path, { return this._requestPath(path, {
cacheKey: typeName + '-gui', cacheKey: typeName + '-gui',
success: options.success, success: success_fnc,
}); });
}, },
tableInfo : function(options) { tableInfo : function(success_fnc, options) {
"use strict"; "use strict";
options = options || {}; options = options || {};
var success_fnc = options.success || function(){api.doLog('success not provided for tableInfo');}; success_fnc = success_fnc || function(){api.doLog('success not provided for tableInfo');};
var path = this.tableInfoPath; var path = this.tableInfoPath;
// Cache types locally, will not change unless new broker version this._requestPath(path, {
if( this.cache.get(path) ) { success: success_fnc,
if (success_fnc) {
success_fnc(this.cache.get(path));
}
return;
}
var $this = this;
api.getJson(path, {
success: function(data) {
$this.cache.put(path, data);
success_fnc(data);
},
}); });
}, },
detail: function(id, child) { detail: function(id, child) {
@ -209,14 +218,27 @@ DetailModelRestApi.prototype = {
"use strict"; "use strict";
return this.base.get(options); return this.base.get(options);
}, },
types: function(options) {
"use strict";
return this.base.types(options);
},
tableInfo: function(options) { tableInfo: function(options) {
"use strict"; "use strict";
return this.base.tableInfo(options); return this.base.tableInfo(options);
}, },
list: function(success_fnc, options) { // This is "almost" an alias for get
"use strict";
return this.base.list(success_fnc, options);
},
overview: function(success_fnc, options) {
"use strict";
return this.base.overview(success_fnc, options);
},
item: function(itemId, success_fnc, options) {
"use strict";
return this.base.item(success_fnc, options);
},
types: function(options) {
"use strict";
return this.base.types(options);
},
}; };
// Populate api // Populate api

View File

@ -139,6 +139,6 @@ if ($.fn.DataTable.TableTools) {
"button" : "li", "button" : "li",
"liner" : "a" "liner" : "a"
}, },
"button" : "button", // "button" : "button",
}); });
} }

View File

@ -35,16 +35,13 @@ gui.providers.link = function(event) {
var tableId = gui.providers.table({ var tableId = gui.providers.table({
rowSelect : 'single', rowSelect : 'single',
onEdit: function(value, event, table) { onEdit: function(value, event, table) {
gui.providers.rest.gui(value.type, { gui.providers.rest.gui(value.type, function(data) {
success: function(data){
var form = gui.fields(data); var form = gui.fields(data);
gui.appendToWorkspace(gui.modal('edit_modal', gettext('Edit service provider'), form)); gui.appendToWorkspace(gui.modal('edit_modal', gettext('Edit service provider'), form));
$('#edit_modal').modal() $('#edit_modal').modal()
.on('hidden.bs.modal', function () { .on('hidden.bs.modal', function () {
$('#edit_modal').remove(); $('#edit_modal').remove();
}) });
;
},
}); });
}, },
buttons : [ 'edit', 'delete', 'xls' ], buttons : [ 'edit', 'delete', 'xls' ],
@ -90,7 +87,7 @@ gui.authenticators.link = function(event) {
user.table({ user.table({
container : 'users-placeholder', container : 'users-placeholder',
rowSelect : 'multi', rowSelect : 'multi',
buttons : [ 'edit', 'delete', 'xls' ], buttons : [ 'new', 'edit', 'delete', 'xls' ],
scrollToTable : true, scrollToTable : true,
onLoad: function(k) { onLoad: function(k) {
api.tools.unblockUI(); api.tools.unblockUI();
@ -102,11 +99,9 @@ gui.authenticators.link = function(event) {
$('#users-placeholder').empty(); // Remove detail on parent refresh $('#users-placeholder').empty(); // Remove detail on parent refresh
}, },
onEdit: function(value, event, table) { onEdit: function(value, event, table) {
gui.authenticators.rest.gui(value.type, { gui.authenticators.rest.gui(value.type, function(data){
success: function(data){
var form = gui.fields(data); var form = gui.fields(data);
gui.launchModal(gettext('Edit authenticator')+' '+value.name, form); gui.launchModal(gettext('Edit authenticator')+' '+value.name, form);
},
}); });
}, },
}); });
@ -146,29 +141,26 @@ gui.connectivity.link = function(event) {
gui.connectivity.transports.table({ gui.connectivity.transports.table({
rowSelect : 'multi', rowSelect : 'multi',
container : 'transports-placeholder', container : 'transports-placeholder',
buttons : [ 'edit', 'delete', 'xls' ], buttons : [ 'new', 'edit', 'delete', 'xls' ],
onEdit: function(value, event, table) { onEdit: function(value, event, table) {
gui.connectivity.transports.rest.gui(value.type, { gui.connectivity.transports.rest.gui(value.type, function(itemGui){
success: function(itemGui){ gui.connectivity.transports.rest.item(value.id, function(item) {
gui.connectivity.transports.rest.get({
id:value.id,
success: function(item) {
var form = gui.fields(itemGui, item); var form = gui.fields(itemGui, item);
gui.launchModal(gettext('Edit transport')+' '+value.name,form, function(form_selector) { gui.launchModal(gettext('Edit transport')+' '+value.name,form, function(form_selector) {
var fields = gui.fields.read(form_selector); var fields = gui.fields.read(form_selector);
return false; return false;
}); });
}, });
}); });
}, },
}); onNew: function(type) {
gui.doLog(type);
}, },
}); });
gui.connectivity.networks.table({ gui.connectivity.networks.table({
rowSelect : 'multi', rowSelect : 'multi',
container : 'networks-placeholder', container : 'networks-placeholder',
buttons : [ 'edit', 'delete', 'xls' ], buttons : [ 'new', 'edit', 'delete', 'xls' ],
}); });
}); });

View File

@ -2,10 +2,10 @@
(function(gui, $, undefined) { (function(gui, $, undefined) {
"use strict"; "use strict";
// "public" methods // "public" methods
gui.doLog = function(data) { gui.doLog = function() {
if (gui.debug) { if (gui.debug) {
try { try {
console.log(data); console.log(arguments);
} catch (e) { } catch (e) {
// nothing can be logged // nothing can be logged
} }
@ -212,8 +212,7 @@ GuiElement.prototype = {
"use strict"; "use strict";
gui.doLog('Initializing ' + this.name); gui.doLog('Initializing ' + this.name);
var $this = this; var $this = this;
this.rest.types({ this.rest.types(function(data) {
success: function(data) {
var styles = ''; var styles = '';
$.each(data, function(index, value) { $.each(data, function(index, value) {
var className = $this.name + '-' + value.type; var className = $this.name + '-' + value.type;
@ -231,7 +230,6 @@ GuiElement.prototype = {
styles = '<style media="screen">' + styles + '</style>'; styles = '<style media="screen">' + styles + '</style>';
$(styles).appendTo('head'); $(styles).appendTo('head');
} }
},
}); });
}, },
// Options: dictionary // Options: dictionary
@ -268,6 +266,7 @@ GuiElement.prototype = {
// 4.- the DataTable that raised the event // 4.- the DataTable that raised the event
table : function(options) { table : function(options) {
"use strict"; "use strict";
console.log('Types: ', this.types);
options = options || {}; options = options || {};
gui.doLog('Composing table for ' + this.name); gui.doLog('Composing table for ' + this.name);
var tableId = this.name + '-table'; var tableId = this.name + '-table';
@ -313,8 +312,7 @@ GuiElement.prototype = {
return dict[data] || renderEmptyCell(''); return dict[data] || renderEmptyCell('');
}; };
}; };
this.rest.tableInfo({ this.rest.tableInfo(function(data) {
success: function(data) {
var title = data.title; var title = data.title;
var columns = []; var columns = [];
$.each(data.fields, function(index, value) { $.each(data.fields, function(index, value) {
@ -333,7 +331,7 @@ GuiElement.prototype = {
if (opts.searchable !== undefined) if (opts.searchable !== undefined)
column.bSearchable = opts.searchable; column.bSearchable = opts.searchable;
if (opts.type !== undefined && column.bVisible ) { if (opts.type && column.bVisible ) {
switch(opts.type) { switch(opts.type) {
case 'date': case 'date':
column.sType = 'date'; column.sType = 'date';
@ -367,7 +365,7 @@ GuiElement.prototype = {
columns.push(column); columns.push(column);
} }
}); });
// Generate styles for responsible table, just the name of fields // Generate styles for responsible table, just the name of fields (append header to table)
var respStyles = []; var respStyles = [];
var counter = 0; var counter = 0;
$.each(columns, function(col, value) { $.each(columns, function(col, value) {
@ -376,15 +374,12 @@ GuiElement.prototype = {
counter += 1; counter += 1;
respStyles.push('#' + tableId + ' td:nth-of-type(' + counter + '):before { content: "' + respStyles.push('#' + tableId + ' td:nth-of-type(' + counter + '):before { content: "' +
(value.sTitle || '') + '";}\n'); (value.sTitle || '') + '";}\n');
respStyles.push('#' + tableId + ' td:nth-of-type(' + counter + '):empty { background-color: red ;}\n');
}); });
// If styles already exists, remove them before adding new ones // If styles already exists, remove them before adding new ones
$('style-' + tableId).remove(); $('style-' + tableId).remove();
$('<style id="style-' + tableId + '" media="screen">@media (max-width: 979px) { ' + respStyles.join('') + '};</style>').appendTo('head'); $('<style id="style-' + tableId + '" media="screen">@media (max-width: 768px) { ' + respStyles.join('') + '};</style>').appendTo('head');
$this.rest.get({ $this.rest.overview(function(data) {
success : function(data) {
var refreshFnc;
var table = gui.table(title, tableId); var table = gui.table(title, tableId);
if (options.container === undefined) { if (options.container === undefined) {
gui.appendToWorkspace('<div class="row"><div class="col-lg-12">' + table.text + '</div></div>'); gui.appendToWorkspace('<div class="row"><div class="col-lg-12">' + table.text + '</div></div>');
@ -393,16 +388,45 @@ GuiElement.prototype = {
$('#' + options.container).append(table.text); $('#' + options.container).append(table.text);
} }
// What execute on refresh button push
var onRefresh = options.onRefresh || function(){};
var refreshFnc = function() {
// Refreshes table content
var tbl = $('#' + tableId).dataTable();
// Clears selection first
TableTools.fnGetInstance(tableId).fnSelectNone();
if( data.length > 1000 )
api.tools.blockUI();
$this.rest.overview(function(data) {
/*$(btn).removeClass('disabled').width('').html(saved);*/
setTimeout( function() {
tbl.fnClearTable();
tbl.fnAddData(data);
onRefresh($this);
api.tools.unblockUI();
}, 0);
});
return false; // This may be used on button or href, better disable execution of it
};
var btns = []; var btns = [];
if (options.buttons) { if (options.buttons) {
var clickHandlerFor = function(handler, action) { var clickHandlerFor = function(handler, action, newHandler) {
var handleFnc = handler || function(val, action, tbl) {gui.doLog('Default handler called for ' + action + ': ' + JSON.stringify(val));}; var handleFnc = handler || function(val, action, tbl) {gui.doLog('Default handler called for ', action);};
return function(btn) { return function(btn) {
var tbl = $('#' + tableId).dataTable(); var tbl = $('#' + tableId).dataTable();
var val = this.fnGetSelectedData()[0]; var val = this.fnGetSelectedData()[0];
setTimeout(function() { setTimeout(function() {
handleFnc(val, action, tbl); if( newHandler ) {
if( handleFnc(action, tbl) === true ) // Reload table?
refreshFnc();
} else {
if( handleFnc(val, action, tbl) === true ) // Reload table?
refreshFnc();
}
}, 0); }, 0);
}; };
}; };
@ -425,42 +449,34 @@ GuiElement.prototype = {
} }
}; };
// What execute on refresh button push
var onRefresh = options.onRefresh || function(){};
refreshFnc = function() {
// Refreshes table content
var tbl = $('#' + tableId).dataTable();
// Clears selection first
TableTools.fnGetInstance(tableId).fnSelectNone();
if( data.length > 1000 )
api.tools.blockUI();
$this.rest.get({
success : function(data) {
/*$(btn).removeClass('disabled').width('').html(saved);*/
setTimeout( function() {
tbl.fnClearTable();
tbl.fnAddData(data);
onRefresh($this);
api.tools.unblockUI();
}, 0);
}
});
return false; // This may be used on button or href, better disable execution of it
};
$.each(options.buttons, function(index, value) { $.each(options.buttons, function(index, value) {
var btn; var btn;
switch (value) { switch (value) {
case 'new': case 'new':
if(Object.keys($this.types).length === 0) {
btn = { btn = {
"sExtends" : "text", "sExtends" : "text",
"sButtonText" : gui.config.dataTableButtons['new'].text, "sButtonText" : gui.config.dataTableButtons['new'].text,
"fnSelect" : deleteSelected, "fnClick" : clickHandlerFor(options.onNew, 'new'),
"fnClick" : clickHandlerFor(options.onDelete, 'delete'),
"sButtonClass" : gui.config.dataTableButtons['new'].css, "sButtonClass" : gui.config.dataTableButtons['new'].css,
}; };
} else {
// This table has "types, so we create a dropdown with Types
var newButtons = [];
$.each($this.types, function(i, val){
newButtons.push({
"sExtends" : "text",
"sButtonText" : '<span class="' + val.css + '"></span> ' + val.name,
"fnClick" : clickHandlerFor(options.onNew, i, true),
});
});
btn = {
"sExtends" : "collection",
"aButtons": newButtons,
"sButtonText" : gui.config.dataTableButtons['new'].text,
"sButtonClass" : gui.config.dataTableButtons['new'].css,
};
}
break; break;
case 'edit': case 'edit':
btn = { btn = {
@ -589,10 +605,7 @@ GuiElement.prototype = {
if( options.onLoad ) { if( options.onLoad ) {
options.onLoad($this); options.onLoad($this);
} }
}
}); });
},
}); });
return '#' + tableId; return '#' + tableId;
} }

View File

@ -1,3 +1,3 @@
{% verbatim %} {% verbatim %}
<input type="hidden" class="{{ css }}" name="{{ name }}}" value="{{ value }}"> <input type="hidden" class="{{ css }}" name="{{ name }}" value="{{ value }}">
{% endverbatim %} {% endverbatim %}

View File

@ -9,6 +9,8 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
{{{ content }}} {{{ content }}}
</div>
<div class="modal-footer">
{{# if button1 }} {{# if button1 }}
{{{ button1 }}} {{{ button1 }}}
{{ else }} {{ else }}

View File

@ -0,0 +1,10 @@
{% verbatim %}
<style id="style-{{ tableId }}" media="screen">
@media (max-width: 768px) {
{{#each columns }}
#{{ tableId }} td:nth-of-type({{@index}}):before { content: "{{ this.sTitle }}"}\n');
respStyles.push('#' + tableId + ' td:nth-of-type(' + counter + '):empty { background-color: red ;}\n');
{{/each }}
};
</style>
{% endverbatim %}

View File

@ -1,5 +1,4 @@
{% verbatim %} {% verbatim %}<?xml version="1.0"?>
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?> <?mso-application progid="Excel.Sheet"?>
<Workbook <Workbook
xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns="urn:schemas-microsoft-com:office:spreadsheet"

View File

@ -10,7 +10,7 @@
</h3> </h3>
</div> </div>
<div class="panel-body collapse in"> <div class="panel-body collapse in">
<table class="table table-striped table-bordered table-hover" id="{{ table_id }}"> <table class="table table-striped table-bordered table-hover" id="{{ table_id }}" width="100%">
</table> </table>
</div> </div>
</div> </div>