cryptpad/www/common/common-interface.js

549 lines
16 KiB
JavaScript
Raw Normal View History

define([
'jquery',
'/customize/messages.js',
'/common/common-util.js',
'/customize/application_config.js',
'/bower_components/alertifyjs/dist/js/alertify.js',
'/common/notify.js',
2017-07-19 15:14:10 +00:00
'/common/visible.js',
'/common/tippy.min.js',
2017-09-07 14:41:11 +00:00
'/customize/pages.js',
2017-09-07 10:45:07 +00:00
'/common/hyperscript.js',
'/bower_components/bootstrap-tokenfield/dist/bootstrap-tokenfield.js',
2017-07-19 15:14:10 +00:00
'css!/common/tippy.css',
2017-09-07 14:41:11 +00:00
], function ($, Messages, Util, AppConfig, Alertify, Notify, Visible, Tippy, Pages, h) {
var UI = {};
/*
* Alertifyjs
*/
UI.Alertify = Alertify;
// set notification timeout
Alertify._$$alertify.delay = AppConfig.notificationTimeout || 5000;
2017-09-07 10:45:07 +00:00
var findCancelButton = UI.findCancelButton = function (root) {
if (root) {
return $(root).find('button.cancel').last();
}
return $('button.cancel').last();
};
2017-09-07 10:45:07 +00:00
var findOKButton = UI.findOKButton = function (root) {
if (root) {
return $(root).find('button.ok').last();
}
return $('button.ok').last();
};
var listenForKeys = UI.listenForKeys = function (yes, no) {
var handler = function (e) {
switch (e.which) {
case 27: // cancel
if (typeof(no) === 'function') { no(e); }
break;
case 13: // enter
if (typeof(yes) === 'function') { yes(e); }
break;
}
};
$(window).keyup(handler);
return handler;
};
var stopListening = UI.stopListening = function (handler) {
$(window).off('keyup', handler);
};
2017-09-07 16:54:58 +00:00
var dialog = UI.dialog = {};
var merge = function (a, b) {
var c = {};
if (a) {
Object.keys(a).forEach(function (k) {
c[k] = a[k];
});
}
if (b) {
Object.keys(b).forEach(function (k) {
c[k] = b[k];
});
}
return c;
};
dialog.selectable = function (value, opt) {
var attrs = merge({
2017-09-07 16:54:58 +00:00
type: 'text',
readonly: 'readonly',
}, opt);
var input = h('input', attrs);
2017-09-07 16:54:58 +00:00
$(input).val(value).click(function () {
input.select();
});
return input;
};
2017-09-08 15:40:25 +00:00
dialog.okButton = function (content) {
return h('button.ok', { tabindex: '2', }, content || Messages.okButton);
2017-09-07 16:54:58 +00:00
};
2017-09-08 15:40:25 +00:00
dialog.cancelButton = function (content) {
return h('button.cancel', { tabindex: '1'}, content || Messages.cancelButton);
2017-09-07 16:54:58 +00:00
};
dialog.message = function (text) {
return h('p.msg', text);
2017-09-07 16:54:58 +00:00
};
dialog.textInput = function (opt) {
2017-09-08 15:40:25 +00:00
var attrs = merge({
2017-09-07 16:54:58 +00:00
type: 'text',
'class': 'cp-text-input',
2017-09-08 15:40:25 +00:00
}, opt);
return h('input', attrs);
2017-09-07 16:54:58 +00:00
};
dialog.nav = function (content) {
return h('nav', content || [
dialog.cancelButton(),
dialog.okButton(),
]);
};
dialog.frame = function (content) {
return h('div.alertify', [
h('div.dialog', [
h('div', content),
])
]);
};
UI.tokenField = function (target) {
var t = {
element: target || h('input'),
};
2017-09-07 16:54:58 +00:00
var $t = t.tokenfield = $(t.element).tokenfield();
t.getTokens = function () {
return $t.tokenfield('getTokens').map(function (token) {
return token.value;
2017-08-04 13:54:15 +00:00
});
2017-09-07 16:54:58 +00:00
};
t.preventDuplicates = function (cb) {
$t.on('tokenfield:createtoken', function (ev) {
var val;
if (t.getTokens().some(function (t) {
if (t === ev.attrs.value) { return ((val = t)); }
})) {
ev.preventDefault();
if (typeof(cb) === 'function') { cb(val); }
}
});
return t;
};
t.setTokens = function (tokens) {
$t.tokenfield('setTokens',
tokens.map(function (token) {
return {
value: token,
label: token,
};
}));
};
t.focus = function () {
var $temp = $t.closest('.tokenfield').find('.token-input');
$temp.css('width', '20%');
$t.tokenfield('focusInput', $temp[0]);
};
return t;
};
dialog.tagPrompt = function (tags, cb) {
var input = dialog.textInput();
var tagger = dialog.frame([
dialog.message('make some tags'), // TODO translate
input,
dialog.nav(),
]);
var field = UI.tokenField(input).preventDuplicates(function (val) {
UI.warn('Duplicate tag: ' + val); // TODO translate
});
var close = Util.once(function () {
var $t = $(tagger).fadeOut(150, function () { $t.remove(); });
});
var listener = listenForKeys(function () {}, function () {
close();
stopListening(listener);
});
var CB = Util.once(cb);
findOKButton(tagger).click(function () {
var tokens = field.getTokens();
close();
CB(tokens);
});
findCancelButton(tagger).click(function () {
close();
CB(null);
});
// :(
setTimeout(function () {
field.setTokens(tags);
field.focus();
});
return tagger;
};
UI.alert = function (msg, cb, force) {
cb = cb || function () {};
var message;
if (typeof(msg) === 'string') {
// sanitize
if (!force) { msg = Util.fixHTML(msg); }
message = dialog.message();
message.innerHTML = msg;
} else {
message = dialog.message(msg);
2017-09-07 16:54:58 +00:00
}
2017-09-07 16:54:58 +00:00
var ok = dialog.okButton();
var frame = dialog.frame([
message,
2017-09-07 16:54:58 +00:00
dialog.nav(ok),
]);
var listener;
var close = Util.once(function () {
$(frame).fadeOut(150, function () { $(this).remove(); });
stopListening(listener);
});
listener = listenForKeys(close, close);
var $ok = $(ok).click(close);
document.body.appendChild(frame);
setTimeout(function () {
$ok.focus();
2017-09-08 15:40:25 +00:00
UI.notify();
});
};
UI.prompt = function (msg, def, cb, opt, force) {
cb = cb || function () {};
2017-09-08 15:40:25 +00:00
opt = opt || {};
var input = dialog.textInput();
input.value = typeof(def) === 'string'? def: '';
var message;
if (typeof(msg) === 'string') {
if (!force) { msg = Util.fixHTML(msg); }
message = dialog.message();
message.innerHTML = msg;
} else {
message = dialog.message(msg);
}
2017-09-08 15:40:25 +00:00
var ok = dialog.okButton(opt.ok);
var cancel = dialog.cancelButton(opt.cancel);
var frame = dialog.frame([
message,
input,
dialog.nav([ cancel, ok, ]),
]);
var listener;
var close = Util.once(function () {
$(frame).fadeOut(150, function () { $(this).remove(); });
stopListening(listener);
});
var $ok = $(ok).click(function (ev) { cb(input.value, ev); });
var $cancel = $(cancel).click(function (ev) { cb(null, ev); });
listener = listenForKeys(function () { // yes
close(); $ok.click();
2017-05-04 14:16:09 +00:00
}, function () { // no
2017-09-08 15:40:25 +00:00
close(); $cancel.click();
});
2017-09-08 15:40:25 +00:00
document.body.appendChild(frame);
setTimeout(function () {
input.select().focus();
UI.notify();
2017-09-08 15:40:25 +00:00
});
};
UI.confirm = function (msg, cb, opt, force, styleCB) {
cb = cb || function () {};
2017-09-08 15:40:25 +00:00
opt = opt || {};
2017-09-08 15:40:25 +00:00
var message;
if (typeof(msg) === 'string') {
if (!force) { msg = Util.fixHTML(msg); }
message = dialog.message();
message.innerHTML = msg;
} else {
message = dialog.message(msg);
}
var ok = dialog.okButton(opt.ok);
var cancel = dialog.cancelButton(opt.cancel);
var frame = dialog.frame([
message,
dialog.nav(opt.reverseOrder?
[ok, cancel]: [cancel, ok]),
]);
var listener;
var close = Util.once(function () {
$(frame).fadeOut(150, function () { $(this).remove(); });
stopListening(listener);
});
2017-09-08 15:40:25 +00:00
var $ok = $(ok).click(function (ev) { close(); cb(true, ev); });
var $cancel = $(cancel).click(function (ev) { close(); cb(false, ev); });
2017-09-08 15:40:25 +00:00
if (opt.cancelClass) { $cancel.addClass(opt.cancelClass); }
if (opt.okClass) { $ok.addClass(opt.okClass); }
listener = listenForKeys(function () {
$ok.click();
}, function () {
$cancel.click();
});
document.body.appendChild(frame);
setTimeout(function () {
UI.notify();
if (typeof(styleCB) === 'function') {
styleCB($ok.closest('.dialog'));
}
2017-09-08 15:40:25 +00:00
});
};
UI.log = function (msg) {
Alertify.success(Util.fixHTML(msg));
};
UI.warn = function (msg) {
Alertify.error(Util.fixHTML(msg));
};
/*
* spinner
*/
UI.spinner = function (parent) {
var $target = $('<span>', {
2017-09-07 14:41:11 +00:00
'class': 'fa fa-circle-o-notch fa-spin fa-4x fa-fw',
}).hide();
$(parent).append($target);
return {
show: function () {
$target.css('display', 'inline');
return this;
},
hide: function () {
$target.hide();
return this;
},
get: function () {
return $target;
},
};
};
var LOADING = 'loading';
var getRandomTip = function () {
if (!Messages.tips || !Object.keys(Messages.tips).length) { return ''; }
var keys = Object.keys(Messages.tips);
var rdm = Math.floor(Math.random() * keys.length);
return Messages.tips[keys[rdm]];
};
UI.addLoadingScreen = function (config) {
config = config || {};
var loadingText = config.loadingText;
var hideTips = config.hideTips;
var hideLogo = config.hideLogo;
var $loading, $container;
if ($('#' + LOADING).length) {
$loading = $('#' + LOADING).show();
if (loadingText) {
$('#' + LOADING).find('p').text(loadingText);
}
$container = $loading.find('.loadingContainer');
} else {
2017-09-07 14:41:11 +00:00
$loading = $(Pages.loadingScreen());
$container = $loading.find('.loadingContainer');
if (hideLogo) {
$loading.find('img').hide();
} else {
$loading.find('img').show();
}
2017-09-07 14:41:11 +00:00
var $spinner = $loading.find('.spinnerContainer');
$spinner.show();
$('body').append($loading);
}
if (Messages.tips && !hideTips) {
var $loadingTip = $('<div>', {'id': 'loadingTip'});
2017-05-04 14:16:09 +00:00
$('<span>', {'class': 'tips'}).text(getRandomTip()).appendTo($loadingTip);
$loadingTip.css({
'bottom': $('body').height()/2 - $container.height()/2 + 20 + 'px'
});
$('body').append($loadingTip);
}
};
UI.removeLoadingScreen = function (cb) {
$('#' + LOADING).fadeOut(750, cb);
var $tip = $('#loadingTip').css('top', '')
2017-06-02 10:13:11 +00:00
// loading.less sets transition-delay: $wait-time
// and transition: opacity $fadeout-time
.css({
'opacity': 0,
'pointer-events': 'none',
});
setTimeout(function () {
$tip.remove();
}, 3750);
2017-06-02 10:13:11 +00:00
// jquery.fadeout can get stuck
};
UI.errorLoadingScreen = function (error, transparent) {
if (!$('#' + LOADING).is(':visible')) { UI.addLoadingScreen({hideTips: true}); }
$('.spinnerContainer').hide();
if (transparent) { $('#' + LOADING).css('opacity', 0.8); }
$('#' + LOADING).find('p').html(error || Messages.error);
};
// Notify
var notify = {};
UI.unnotify = function () {
if (notify.tabNotification &&
typeof(notify.tabNotification.cancel) === 'function') {
notify.tabNotification.cancel();
}
};
UI.notify = function () {
if (Visible.isSupported() && !Visible.currently()) {
UI.unnotify();
notify.tabNotification = Notify.tab(1000, 10);
}
};
if (Visible.isSupported()) {
Visible.onChange(function (yes) {
if (yes) { UI.unnotify(); }
});
}
2017-07-19 15:14:10 +00:00
UI.importContent = function (type, f, cfg) {
return function () {
2017-07-19 15:14:10 +00:00
var $files = $('<input>', {type:"file"});
if (cfg && cfg.accept) {
$files.attr('accept', cfg.accept);
}
$files.click();
$files.on('change', function (e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = function (e) { f(e.target.result, file); };
reader.readAsText(file, type);
});
};
};
2017-09-04 13:09:54 +00:00
var $defaultIcon = $('<span>', {"class": "fa fa-file-text-o"});
2017-06-29 13:15:40 +00:00
UI.getIcon = function (type) {
2017-09-04 13:09:54 +00:00
var $icon = $defaultIcon.clone();
if (AppConfig.applicationsIcon && AppConfig.applicationsIcon[type]) {
var appClass = ' cp-icon-color-'+type;
$icon = $('<span>', {'class': 'fa ' + AppConfig.applicationsIcon[type] + appClass});
2017-06-29 13:15:40 +00:00
}
return $icon;
};
2017-07-19 15:14:10 +00:00
// Tooltips
2017-07-31 10:29:41 +00:00
UI.clearTooltips = function () {
// If an element is removed from the UI while a tooltip is applied on that element, the tooltip will get hung
// forever, this is a solution which just searches for tooltips which have no corrisponding element and removes
// them.
var win;
$('.tippy-popper').each(function (i, el) {
win = win || $('#pad-iframe')[0].contentWindow;
if (win.$('[aria-describedby=' + el.getAttribute('id') + ']').length === 0) {
el.remove();
}
});
2017-07-31 10:29:41 +00:00
};
2017-07-19 15:14:10 +00:00
UI.addTooltips = function () {
var MutationObserver = window.MutationObserver;
var addTippy = function (el) {
if (el.nodeName === 'IFRAME') { return; }
var delay = typeof(AppConfig.tooltipDelay) === "number" ? AppConfig.tooltipDelay : 500;
2017-07-19 15:14:10 +00:00
Tippy(el, {
position: 'bottom',
distance: 0,
performance: true,
dynamicTitle: true,
delay: [delay, 0]
2017-07-19 15:14:10 +00:00
});
};
var $body = $('body');
var $padIframe = $('#pad-iframe').contents().find('body');
$('[title]').each(function (i, el) {
addTippy(el);
});
$('#pad-iframe').contents().find('[title]').each(function (i, el) {
addTippy(el);
});
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList' && mutation.addedNodes.length) {
$body.find('[title]').each(function (i, el) {
addTippy(el);
});
if (!$padIframe.length) { return; }
$padIframe.find('[title]').each(function (i, el) {
addTippy(el);
});
}
});
});
observer.observe($('body')[0], {
attributes: false,
childList: true,
characterData: false,
subtree: true
});
if ($('#pad-iframe').length) {
observer.observe($('#pad-iframe').contents().find('body')[0], {
attributes: false,
childList: true,
characterData: false,
subtree: true
});
}
};
return UI;
});