Adapt history to changes

This commit is contained in:
Ludovic Stephan 2017-04-24 13:25:07 -03:00
parent e283439ebc
commit 46ac82fd27

View file

@ -22,40 +22,42 @@ class KHistory {
constructor(options) { constructor(options) {
var all_options = $.extend({}, this.constructor.default_options, options); var all_options = $.extend({}, this.constructor.default_options, options);
this.templates = all_options.templates;
this.api_options = all_options.api_options; this.api_options = all_options.api_options;
this._$container = $('#history'); this._$container = $('#history');
this._$nb_opes = $('#nb_opes'); this._$nb_opes = $('#nb_opes');
this.list = new OperationList(); this.data = new OperationList();
if (!all_options.no_select) if (!all_options.no_select)
this.selection = new KHistorySelection(this); this.selection = new KHistorySelection(this);
var templates = all_options.templates
if (all_options.no_trigramme) if (all_options.no_trigramme)
this.templates['opegroup'] = '<div class="opegroup"><span class="time"></span><span class="amount"></span><span class="valid_by"></span><span class="comment"></span></div>' templates['opegroup'] =
'<div class="opegroup"><span class="time"></span><span class="amount"></span><span class="valid_by"></span><span class="comment"></span></div>';
this.display = new ForestDisplay(this._$container, templates);
this._init_events(); this._init_events();
} }
fetch(api_options) { fetch(api_options) {
this.list.clear(); this.data.clear();
if (api_options) if (api_options)
this.api_options = api_options; this.api_options = api_options;
this.list.fromAPI(this.api_options) this.data.fromAPI(this.api_options)
.done( () => this.display() ); .done( () => this.display_data() );
} }
display() { display_data() {
this._$container.html(''); this.display.clear();
this.list.display(this._$container, this.templates, {}); this.display.render(this.data);
var nb_opes = this._$container.find('.ope[canceled="false"]').length; var nb_opes = this._$container.find('.ope[canceled="false"]').length;
$('#nb_opes').text(nb_opes); this._$nb_opes.text(nb_opes);
} }
_init_events() { _init_events() {
@ -82,20 +84,16 @@ class KHistory {
} }
add_node(data) { add_node(data) {
var node = this.list.get_or_create(data.modelname, data.content, 0); var node = this.data.get_or_create(data.modelname, data.content, 0);
this.list.add_to_container(this._$container, node, this.templates); this.display.add(node);
} }
update_node(type, id, update_data) { update_node(modelname, id, update_data) {
var to_update = this.list.find(type, id); var updated = this.data.update(modelname, id, update_data)
if (!to_update) if (!updated)
return false; return false;
to_update.update(update_data); this.display.update(updated);
var $new_elt = to_update.display($(this.templates[type]), {});
var $to_replace = this._$container.find('#'+type+'-'+id+'>:first-child');
$to_replace.replaceWith($new_elt);
return true; return true;
} }