123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
'use strict';
modulum('MessageWidget', ['TextWidgetBase', 'WidgetFactory'],
function(context, cls) {
cls.MessageWidget = context.oo.Class(cls.TextWidgetBase, function($super) {
return {
__name: 'MessageWidget',
$static: {
defaultDisplayTime: 10
},
_text: '',
_htmlFormat: false,
_sanitize: null,
_kind: 'message',
_currentTimeout: null,
_messageDisplayTime: 0,
_closeButton: null,
_textElement: null,
_initElement: function() {
$super._initElement.call(this);
this._closeButton = this._element.querySelector('.close-button');
this._textElement = this._element.querySelector('.message-text');
var themeDisplayTime = context.ThemeService.getValue('theme-message-display-time');
this._messageDisplayTime = Object.isNumber(themeDisplayTime) || Object.isString(themeDisplayTime) ?
parseInt(themeDisplayTime, 10) : cls.MessageWidget.defaultDisplayTime;
this.setHidden(true);
},
destroy: function() {
this._closeButton = null;
this._textElement = null;
if (this._htmlFilter) {
this._htmlFilter.destroy();
this._htmlFilter = null;
}
$super.destroy.call(this);
},
manageMouseClick: function(domEvent) {
if (domEvent.target.isElementOrChildOf(this._closeButton)) {
this.setHidden(true);
}
return true;
},
setText: function(text) {
if (text !== this._text) {
this._text = text;
this._refreshText();
this.getLayoutEngine().invalidateMeasure();
}
},
_refreshText: function() {
this.domAttributesMutator(function() {
if (this._htmlFormat) {
if (this._sanitize) {
if (!this._htmlFilter) {
this._htmlFilter = cls.WidgetFactory.createWidget('HtmlFilterWidget', this.getBuildParameters());
}
this._textElement.innerHTML = this._htmlFilter.sanitize(this._text);
} else {
this._textElement.innerHTML = this._text;
}
} else {
this._textElement.textContent = this._text;
}
}.bind(this));
if (this._text.trim().length <= 0) {
this.setHidden(true);
} else {
this.setHidden(false);
}
if (this.isReversed()) {
var ui = this.getUserInterfaceWidget();
if (ui) {
var left = ui.getElement().getBoundingClientRect().left;
this.setStyle({
'left': (left + 12) + 'px'
});
}
}
},
getText: function() {
return this._textElement.textContent;
},
setHidden: function(hidden) {
if (this._text.trim().length <= 0 || this._messageDisplayTime < 0) {
hidden = true;
}
if (!hidden) {
this.removeClass("out-of-view");
}
var disp = this._messageDisplayTime * 1000;
if (this._messageDisplayTime > 0) {
if (this._hidden !== hidden) {
$super.setHidden.call(this, hidden);
if (this._currentTimeout !== null) {
this._clearTimeout(this._currentTimeout);
this._currentTimeout = null;
}
if (!hidden) {
this._currentTimeout = this._registerTimeout(this._hide.bind(this), disp);
}
this.emit("hide.MessageWidget", hidden);
}
} else {
$super.setHidden.call(this, hidden);
}
},
_hide: function() {
this.setHidden(true);
this._currentTimeout = null;
},
setHtmlFormat: function(html) {
if (this._htmlFormat !== html) {
this._htmlFormat = html;
this._refreshText();
}
},
setSanitize: function(sanitize) {
this._sanitize = sanitize;
},
setMessageKind: function(kind) {
this._kind = kind;
if (kind) {
var themeDisplayTime = context.ThemeService.getValue('theme-' + kind + '-display-time');
this._messageDisplayTime = Object.isNumber(themeDisplayTime) || Object.isString(themeDisplayTime) ?
parseInt(themeDisplayTime, 10) : cls.MessageWidget.defaultDisplayTime;
if (context.ThemeService.getValue("theme-" + kind + "-display-position")) {
this.addClass(context.ThemeService.getValue("theme-" + kind + "-display-position"));
}
}
},
getMessageKind: function() {
return this._kind;
},
getPosition: function() {
var pos = "bottom-right";
var kind = this.getMessageKind();
var themePos = context.ThemeService.getValue("theme-" + kind + "-display-position");
return themePos ? themePos : pos;
},
setDummyMessage: function() {
this.setText("...");
this.addClass("out-of-view");
},
};
});
cls.WidgetFactory.registerBuilder('Message', cls.MessageWidget);
});