blob: 72438379bbf84d7292fd267609638a4b9ea78af1 [file] [log] [blame]
/**
* Create a view that can be added to a panel,
* like a tree view or the metadata view.
*/
define(['buttongroup', 'util'], function (buttonGroupClass) {
const loc = KorAP.Locale;
loc.CLOSE = loc.CLOSE || 'Close';
return {
create : function (classes) {
return Object.create(this)._init(classes);
},
// Override by inheriting object
_init : function (classes) {
this.panel = undefined;
this._classes = classes;
this._shown = false;
// The buttonclass is bind to the view
var c = ['action', 'button-view'];
if (classes)
c.push.apply(c,classes);
this.actions = buttonGroupClass.create(c).bind(this);
this.actions.add(loc.CLOSE, {'cls':['button-icon','close']}, function (e) {
this.close();
});
// Warning: This is circular
this.actions.view = this;
return this;
},
/**
* Element of the view
*/
element : function () {
if (this._element)
return this._element;
// Create panel element
var e = document.createElement('div');
var cl = e.classList;
cl.add('view');
if (this._classes)
cl.add.apply(cl, this._classes);
// TODO: The show may need to be wrapped in another DIV!
if (this.show !== undefined) {
let s = this.show();
if (s) {
e.appendChild(s);
} else {
return e
}
}
this._shown = true;
e.appendChild(this.actions.element());
this._element = e;
return e;
},
/**
* Is the object shown?
*/
shown : function () {
return this._shown;
},
// onClose : function () {},
/**
* Close the view.
*/
close : function () {
// Close embedded things before
if (this.onClose)
this.onClose();
var e = this.element();
if (e.parentNode) {
e.parentNode.removeChild(e);
};
this.panel.delView(this);
this._shown = false;
},
/**
* Upgrade this object to another object,
* while private data stays intact.
*
* @param {Object] An object with properties.
*/
upgradeTo : function (props) {
for (var prop in props) {
this[prop] = props[prop];
};
return this;
}
};
});