cryptpad/www/common/common-interface.js

1363 lines
42 KiB
JavaScript
Raw Normal View History

if (!document.querySelector("#alertifyCSS")) {
// Prevent alertify from injecting CSS, we create our own in alertify.less.
// see: https://github.com/alertifyjs/alertify.js/blob/v1.0.11/src/js/alertify.js#L414
var head = document.getElementsByTagName("head")[0];
var css = document.createElement("span");
css.id = "alertifyCSS";
css.setAttribute('data-but-why', 'see: common-interface.js');
head.insertBefore(css, head.firstChild);
}
define([
'jquery',
'/customize/messages.js',
'/common/common-util.js',
'/common/common-hash.js',
'/common/common-notifier.js',
'/customize/application_config.js',
'/bower_components/alertifyjs/dist/js/alertify.js',
2018-04-12 17:08:08 +00:00
'/common/tippy/tippy.min.js',
2017-09-07 10:45:07 +00:00
'/common/hyperscript.js',
'/customize/loading.js',
'/common/test.js',
'/common/jquery-ui/jquery-ui.min.js',
2017-09-07 10:45:07 +00:00
'/bower_components/bootstrap-tokenfield/dist/bootstrap-tokenfield.js',
2018-04-12 17:08:08 +00:00
'css!/common/tippy/tippy.css',
'css!/common/jquery-ui/jquery-ui.min.css'
], function ($, Messages, Util, Hash, Notifier, AppConfig,
2018-10-03 15:25:04 +00:00
Alertify, Tippy, h, Loading, Test) {
var UI = {};
/*
* Alertifyjs
*/
UI.Alertify = Alertify;
// set notification timeout
2018-04-16 14:07:40 +00:00
Alertify._$$alertify.delay = AppConfig.notificationTimeout || 5000;
2018-10-03 15:25:04 +00:00
var setHTML = UI.setHTML = function (e, html) {
e.innerHTML = html;
return e;
};
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();
};
2020-01-20 12:59:29 +00:00
UI.removeModals = function () {
$('div.alertify').remove();
};
var listenForKeys = UI.listenForKeys = function (yes, no, el) {
var handler = function (e) {
e.stopPropagation();
switch (e.which) {
case 27: // cancel
if (typeof(no) === 'function') { no(e); }
break;
case 13: // enter
if (typeof(yes) === 'function') { yes(e); }
break;
}
$(el || window).off('keydown', handler);
};
$(el || window).keydown(handler);
return handler;
};
2018-01-17 17:39:45 +00:00
var customListenForKeys = function (keys, cb, el) {
2018-01-18 09:53:36 +00:00
if (!keys || !keys.length || typeof cb !== "function") { return; }
2018-01-17 17:39:45 +00:00
var handler = function (e) {
e.stopPropagation();
2018-01-18 09:53:36 +00:00
keys.some(function (k) {
// k is number or array
// if it's an array, it should be [keyCode, "{ctrl|alt|shift|meta}"]
if (Array.isArray(k) && e.which === k[0] && e[k[1] + 'Key']) {
cb();
return true;
}
if (e.which === k && !e.shiftKey && !e.altKey && !e.metaKey && !e.ctrlKey) {
cb();
return true;
}
});
2018-01-17 17:39:45 +00:00
};
$(el || window).keydown(handler);
return handler;
};
var stopListening = UI.stopListening = function (handler) {
if (!handler) { return; } // we don't want to stop all the 'keyup' listeners
$(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;
};
2019-12-04 12:46:56 +00:00
dialog.selectableArea = function (value, opt) {
var attrs = merge({
readonly: 'readonly',
}, opt);
var input = h('textarea', attrs);
$(input).val(value).click(function () {
input.select();
});
return input;
};
2018-01-24 10:01:15 +00:00
dialog.okButton = function (content, classString) {
2020-06-19 15:06:25 +00:00
var sel = typeof(classString) === 'string'? 'button.ok.' + classString:'button.btn.ok.primary';
2018-01-24 10:01:15 +00:00
return h(sel, { tabindex: '2', }, content || Messages.okButton);
2017-09-07 16:54:58 +00:00
};
2018-01-24 10:01:15 +00:00
dialog.cancelButton = function (content, classString) {
2020-06-19 15:06:25 +00:00
var sel = typeof(classString) === 'string'? 'button.' + classString:'button.btn.cancel';
2018-01-24 10:01:15 +00:00
return h(sel, { 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('p.msg', h('input', attrs));
2017-09-07 16:54:58 +00:00
};
dialog.textTypeInput = function (dropdown) {
var attrs = {
type: 'text',
'class': 'cp-text-type-input',
};
return h('p.msg.cp-alertify-type-container', h('div.cp-alertify-type', [
h('input', attrs),
dropdown // must be a "span"
]));
};
2017-09-07 16:54:58 +00:00
dialog.nav = function (content) {
return h('nav', content || [
dialog.cancelButton(),
dialog.okButton(),
]);
};
dialog.frame = function (content, opt) {
opt = opt || {};
var cls = opt.wide ? '.wide' : '';
2019-09-12 16:51:03 +00:00
var frame = h('div.alertify', {
tabindex: 1,
}, [
2017-09-07 16:54:58 +00:00
h('div.dialog', [
h('div'+cls, content),
2017-09-07 16:54:58 +00:00
])
2019-09-12 16:51:03 +00:00
]);
var $frame = $(frame);
frame.closeModal = function (cb) {
frame.closeModal = function () {}; // Prevent further calls
2019-09-12 16:51:03 +00:00
$frame.fadeOut(150, function () {
$frame.detach();
2020-03-10 16:12:41 +00:00
if (typeof(cb) === "function") { cb(); }
2019-09-12 16:51:03 +00:00
});
};
return $frame.click(function (e) {
$frame.find('.cp-dropdown-content').hide();
e.stopPropagation();
})[0];
2017-09-07 16:54:58 +00:00
};
/**
* tabs is an array containing objects
* each object must have the following attributes:
* - title: String
* - content: DOMElement
*/
dialog.tabs = function (tabs) {
var contents = [];
var titles = [];
2019-11-29 15:47:18 +00:00
var active = 0;
tabs.forEach(function (tab, i) {
2020-02-18 09:28:36 +00:00
if (!(tab.content || tab.disabled) || !tab.title) { return; }
2018-01-17 17:39:45 +00:00
var content = h('div.alertify-tabs-content', tab.content);
2020-09-15 08:42:26 +00:00
var title = h('span.alertify-tabs-title'+ (tab.disabled ? '.disabled' : ''), h('span.tab-title-text',{id: 'cp-tab-' + tab.title.toLowerCase(), 'aria-hidden':"true"}, tab.title));
2019-11-21 10:26:30 +00:00
if (tab.icon) {
2020-09-15 08:42:26 +00:00
var icon = h('i', {class: tab.icon, 'aria-labelledby': 'cp-tab-' + tab.title.toLowerCase()});
2019-11-21 10:26:30 +00:00
$(title).prepend(' ').prepend(icon);
2019-11-22 16:03:17 +00:00
}
$(title).click(function () {
2020-02-18 09:28:36 +00:00
if (tab.disabled) { return; }
var old = tabs[active];
if (old.onHide) { old.onHide(); }
titles.forEach(function (t) { $(t).removeClass('alertify-tabs-active'); });
contents.forEach(function (c) { $(c).removeClass('alertify-tabs-content-active'); });
if (tab.onShow) {
tab.onShow();
}
$(title).addClass('alertify-tabs-active');
$(content).addClass('alertify-tabs-content-active');
active = i;
});
titles.push(title);
contents.push(content);
2020-02-18 09:28:36 +00:00
if (tab.active && !tab.disabled) { active = i; }
});
if (contents.length) {
2019-11-29 15:47:18 +00:00
$(contents[active]).addClass('alertify-tabs-content-active');
$(titles[active]).addClass('alertify-tabs-active');
}
return h('div.alertify-tabs', [
h('div.alertify-tabs-titles', titles),
h('div.alertify-tabs-contents', contents),
]);
};
UI.tokenField = function (target, autocomplete) {
2017-09-07 16:54:58 +00:00
var t = {
element: target || h('input'),
};
var $t = t.tokenfield = $(t.element).tokenfield({
autocomplete: {
source: autocomplete,
delay: 100
},
showAutocompleteOnFocus: false
});
t.getTokens = function (ignorePending) {
var tokens = $t.tokenfield('getTokens').map(function (token) {
2017-09-19 13:30:08 +00:00
return token.value.toLowerCase();
2017-08-04 13:54:15 +00:00
});
if (ignorePending) { return tokens; }
var $pendingEl = $($t.parent().find('.token-input')[0]);
var val = ($pendingEl.val() || "").trim();
if (val && tokens.indexOf(val) === -1) {
return tokens.concat(val);
}
return tokens;
2017-09-07 16:54:58 +00:00
};
2017-09-20 15:55:05 +00:00
var $root = $t.parent();
2017-09-21 09:19:23 +00:00
$t.on('tokenfield:removetoken', function () {
2017-09-20 15:55:05 +00:00
$root.find('.token-input').focus();
});
2017-09-07 16:54:58 +00:00
t.preventDuplicates = function (cb) {
$t.on('tokenfield:createtoken', function (ev) {
// Close the suggest list when a token is added because we're going to wipe the input
var $input = $t.closest('.tokenfield').find('.token-input');
$input.autocomplete('close');
2017-09-07 16:54:58 +00:00
var val;
2017-09-19 13:30:08 +00:00
ev.attrs.value = ev.attrs.value.toLowerCase();
if (t.getTokens(true).some(function (t) {
if (t === ev.attrs.value) {
ev.preventDefault();
return ((val = t));
}
2017-09-07 16:54:58 +00:00
})) {
ev.preventDefault();
if (typeof(cb) === 'function') { cb(val); }
}
});
return t;
};
t.setTokens = function (tokens) {
$t.tokenfield('setTokens',
tokens.map(function (token) {
return {
2017-09-19 13:30:08 +00:00
value: token.toLowerCase(),
label: token.toLowerCase(),
2017-09-07 16:54:58 +00:00
};
}));
};
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, existing, cb) {
2017-09-07 16:54:58 +00:00
var input = dialog.textInput();
var tagger = dialog.frame([
dialog.message([ Messages.tags_add ]),
2017-09-07 16:54:58 +00:00
input,
2017-10-09 14:51:57 +00:00
h('center', h('small', Messages.tags_notShared)),
2017-09-07 16:54:58 +00:00
dialog.nav(),
]);
var field = UI.tokenField(input, existing).preventDuplicates(function (val) {
2017-09-19 13:30:08 +00:00
UI.warn(Messages._getKey('tags_duplicate', [val]));
2017-09-07 16:54:58 +00:00
});
2017-09-20 15:55:05 +00:00
var listener;
var close = Util.once(function (result, ev) {
2017-10-05 13:08:34 +00:00
ev.stopPropagation();
ev.preventDefault();
2017-09-20 15:55:05 +00:00
var $frame = $(tagger).fadeOut(150, function () {
stopListening(listener);
$frame.remove();
cb(result, ev);
});
2017-09-07 16:54:58 +00:00
});
2017-10-05 13:08:34 +00:00
var $ok = findOKButton(tagger).click(function (e) {
2017-09-07 16:54:58 +00:00
var tokens = field.getTokens();
2017-10-05 13:08:34 +00:00
close(tokens, e);
2017-09-20 15:55:05 +00:00
});
2017-10-05 13:08:34 +00:00
var $cancel = findCancelButton(tagger).click(function (e) {
close(null, e);
2017-09-07 16:54:58 +00:00
});
2020-06-19 12:53:26 +00:00
$(tagger).on('keydown', function (e) {
if (e.which === 27) {
$cancel.click();
return;
}
if (e.which === 13) {
$ok.click();
}
});
2017-10-05 13:08:34 +00:00
$(tagger).on('click submit', function (e) {
e.stopPropagation();
2017-09-07 16:54:58 +00:00
});
2017-09-20 15:55:05 +00:00
document.body.appendChild(tagger);
2017-09-07 16:54:58 +00:00
// :(
setTimeout(function () {
field.setTokens(tags);
field.focus();
});
var $field = field.tokenfield.closest('.tokenfield').find('.token-input');
$field.on('keypress', function (e) {
if (!$field.val() && e.which === 13) { return void $ok.click(); }
});
$field.on('keydown', function (e) {
if (!$field.val() && e.which === 27) { return void $cancel.click(); }
});
2017-09-07 16:54:58 +00:00
return tagger;
};
2019-09-12 16:51:03 +00:00
dialog.getButtons = function (buttons, onClose) {
2020-03-18 14:03:44 +00:00
if (!buttons) { return; }
2019-09-12 16:51:03 +00:00
if (!Array.isArray(buttons)) { return void console.error('Not an array'); }
2020-01-20 12:59:29 +00:00
if (!buttons.length) { return; }
2019-09-12 16:51:03 +00:00
var navs = [];
buttons.forEach(function (b) {
if (!b.name || !b.onClick) { return; }
var button = h('button', { tabindex: '1', 'class': b.className || '' }, [
b.iconClass ? h('i' + b.iconClass) : undefined,
b.name
]);
2020-06-19 15:06:25 +00:00
button.classList.add('btn');
2020-03-13 14:05:07 +00:00
var todo = function () {
var noClose = b.onClick();
if (noClose) { return; }
2019-09-12 16:51:03 +00:00
var $modal = $(button).parents('.alertify').first();
if ($modal.length && $modal[0].closeModal) {
$modal[0].closeModal(function () {
if (onClose) {
onClose();
}
});
}
2020-03-13 14:05:07 +00:00
};
if (b.confirm) {
UI.confirmButton(button, {
classes: 'danger',
divClasses: 'left'
}, todo);
} else {
$(button).click(function () {
todo();
});
}
2019-09-12 16:51:03 +00:00
if (b.keys && b.keys.length) { $(button).attr('data-keys', JSON.stringify(b.keys)); }
navs.push(button);
});
return dialog.nav(navs);
};
2018-01-17 17:39:45 +00:00
dialog.customModal = function (msg, opt) {
var force = false;
if (typeof(opt) === 'object') {
force = opt.force || false;
} else if (typeof(opt) === 'boolean') {
force = opt;
}
if (typeof(opt) !== 'object') {
opt = {};
}
var message;
if (typeof(msg) === 'string') {
// sanitize
if (!force) { msg = Util.fixHTML(msg); }
message = dialog.message();
message.innerHTML = msg;
} else {
message = dialog.message(msg);
}
var frame = h('div', [
message,
2019-09-12 16:51:03 +00:00
dialog.getButtons(opt.buttons, opt.onClose)
2018-01-17 17:39:45 +00:00
]);
if (opt.forefront) { $(frame).addClass('forefront'); }
return frame;
};
UI.openCustomModal = function (content, opt) {
2018-01-17 17:39:45 +00:00
var frame = dialog.frame([
content
], opt);
2018-01-17 17:39:45 +00:00
$(frame).find('button[data-keys]').each(function (i, el) {
2018-01-18 09:53:36 +00:00
var keys = JSON.parse($(el).attr('data-keys'));
2018-01-17 17:39:45 +00:00
customListenForKeys(keys, function () {
if (!$(el).is(':visible')) { return; }
$(el).click();
}, frame);
});
document.body.appendChild(frame);
$(frame).focus();
setTimeout(function () {
Notifier.notify();
});
2020-03-18 13:47:33 +00:00
return frame;
2018-01-17 17:39:45 +00:00
};
UI.createModal = function (cfg) {
var $body = cfg.$body || $('body');
2020-04-07 12:27:44 +00:00
var $blockContainer = cfg.id && $body.find('#'+cfg.id);
if (!$blockContainer || !$blockContainer.length) {
var id = '';
if (cfg.id) { id = '#'+cfg.id; }
$blockContainer = $(h('div.cp-modal-container'+id, {
tabindex: 1
}));
}
2020-04-07 12:27:44 +00:00
var deleted = false;
var hide = function () {
2020-04-07 12:27:44 +00:00
if (deleted) { return; }
$blockContainer.hide();
2020-04-07 12:27:44 +00:00
if (!cfg.id) {
deleted = true;
$blockContainer.remove();
}
if (cfg.onClose) { cfg.onClose(); }
};
$blockContainer.html('').appendTo($body);
var $block = $(h('div.cp-modal')).appendTo($blockContainer);
$(h('span.cp-modal-close.fa.fa-times', {
title: Messages.filePicker_close
})).click(hide).appendTo($block);
$body.click(hide);
$block.click(function (e) {
e.stopPropagation();
});
$body.keydown(function (e) {
if (e.which === 27) {
hide();
}
});
2020-04-06 09:42:47 +00:00
return {
$modal: $blockContainer,
show: function () {
$blockContainer.css('display', 'flex');
},
hide: hide
};
};
2017-09-13 10:32:30 +00:00
UI.alert = function (msg, cb, opt) {
var force = false;
if (typeof(opt) === 'object') {
force = opt.force || false;
} else if (typeof(opt) === 'boolean') {
force = opt;
}
if (typeof(opt) !== 'object') {
2017-09-13 10:32:30 +00:00
opt = {};
}
2017-09-07 16:54:58 +00:00
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),
]);
2017-09-13 10:32:30 +00:00
if (opt.forefront) { $(frame).addClass('forefront'); }
2017-09-07 16:54:58 +00:00
var listener;
var close = Util.once(function () {
$(frame).fadeOut(150, function () { $(this).remove(); });
stopListening(listener);
cb();
2017-09-07 16:54:58 +00:00
});
listener = listenForKeys(close, close, frame);
2017-09-07 16:54:58 +00:00
var $ok = $(ok).click(close);
document.body.appendChild(frame);
setTimeout(function () {
$ok.focus();
Notifier.notify();
});
2020-02-05 15:18:09 +00:00
return {
element: frame,
delete: close
};
};
UI.prompt = function (msg, def, cb, opt, force) {
cb = cb || function () {};
2017-09-08 15:40:25 +00:00
opt = opt || {};
var inputBlock = opt.password ? UI.passwordInput() :
(opt.typeInput ? dialog.textTypeInput(opt.typeInput) : dialog.textInput());
2019-12-11 15:40:39 +00:00
var input = $(inputBlock).is('input') ? inputBlock : $(inputBlock).find('input')[0];
2017-09-08 15:40:25 +00:00
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,
2018-05-28 15:50:28 +00:00
inputBlock,
2017-09-08 15:40:25 +00:00
dialog.nav([ cancel, ok, ]),
]);
var listener;
var close = Util.once(function (result, ev) {
var $frame = $(frame).fadeOut(150, function () {
stopListening(listener);
$frame.remove();
cb(result, ev);
});
2017-09-08 15:40:25 +00:00
});
var $ok = $(ok).click(function (ev) { close(input.value, ev); });
var $cancel = $(cancel).click(function (ev) { close(null, ev); });
2017-09-08 15:40:25 +00:00
listener = listenForKeys(function () { // yes
$ok.click();
2017-05-04 14:16:09 +00:00
}, function () { // no
$cancel.click();
2017-09-21 15:56:24 +00:00
}, input);
2017-09-08 15:40:25 +00:00
document.body.appendChild(frame);
setTimeout(function () {
$(input).select().focus();
Notifier.notify();
2017-09-08 15:40:25 +00:00
});
};
2017-09-11 14:24:43 +00:00
UI.confirm = function (msg, cb, opt, force) {
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);
}
2018-01-24 10:01:15 +00:00
var ok = dialog.okButton(opt.ok, opt.okClass);
var cancel = dialog.cancelButton(opt.cancel, opt.cancelClass);
2017-09-08 15:40:25 +00:00
var frame = dialog.frame([
message,
dialog.nav(opt.reverseOrder?
[ok, cancel]: [cancel, ok]),
]);
var listener;
var close = Util.once(function (bool, ev) {
2017-09-08 15:40:25 +00:00
$(frame).fadeOut(150, function () { $(this).remove(); });
stopListening(listener);
cb(bool, ev);
});
var $ok = $(ok).click(function (ev) { close(true, ev); });
var $cancel = $(cancel).click(function (ev) { close(false, ev); });
2017-09-08 15:40:25 +00:00
listener = listenForKeys(function () {
$ok.click();
}, function () {
$cancel.click();
}, frame);
2017-09-08 15:40:25 +00:00
document.body.appendChild(frame);
setTimeout(function () {
Notifier.notify();
$(frame).find('.ok').focus();
2017-09-11 14:24:43 +00:00
if (typeof(opt.done) === 'function') {
opt.done($ok.closest('.dialog'));
}
2017-09-08 15:40:25 +00:00
});
};
2020-02-04 13:49:31 +00:00
UI.confirmButton = function (originalBtn, config, _cb) {
config = config || {};
var cb = Util.mkAsync(_cb);
if (!config.multiple) {
cb = Util.once(cb);
}
2020-02-04 13:49:31 +00:00
var classes = 'btn ' + (config.classes || 'btn-primary');
var button = h('button', {
"class": classes,
title: config.title || ''
2020-02-18 13:14:29 +00:00
}, Messages.areYouSure);
2020-02-04 13:49:31 +00:00
var $button = $(button);
var div = h('div', {
"class": config.classes || ''
});
var timer = h('div.cp-button-timer', div);
var content = h('div.cp-button-confirm', [
button,
timer
]);
2020-03-13 14:05:07 +00:00
if (config.divClasses) {
$(content).addClass(config.divClasses);
}
2020-02-04 13:49:31 +00:00
var to;
var done = function (res) {
2020-02-24 10:28:20 +00:00
if (res) { cb(res); }
2020-02-04 13:49:31 +00:00
clearTimeout(to);
2020-02-24 10:28:20 +00:00
$(content).detach();
2020-02-04 13:49:31 +00:00
$(originalBtn).show();
};
$button.click(function () {
done(true);
});
var TIMEOUT = 3000;
var INTERVAL = 10;
var i = 1;
var todo = function () {
var p = 100 * ((TIMEOUT - (i * INTERVAL)) / TIMEOUT);
if (i++ * INTERVAL >= TIMEOUT) {
done(false);
return;
}
$(div).css('width', p+'%');
to = setTimeout(todo, INTERVAL);
};
2020-02-24 10:28:20 +00:00
$(originalBtn).addClass('cp-button-confirm-placeholder').click(function () {
// If we have a validation function, continue only if it's true
if (config.validate && !config.validate()) { return; }
2020-02-24 10:28:20 +00:00
i = 1;
to = setTimeout(todo, INTERVAL);
$(originalBtn).hide().after(content);
});
2020-03-13 14:05:07 +00:00
return {
reset: function () {
done(false);
}
};
2020-02-04 13:49:31 +00:00
};
2019-09-20 13:26:58 +00:00
UI.proposal = function (content, cb) {
var buttons = [{
name: Messages.friendRequest_later,
onClick: function () {},
keys: [27]
}, {
className: 'primary',
name: Messages.friendRequest_accept,
onClick: function () {
cb(true);
},
keys: [13]
}, {
className: 'primary',
name: Messages.friendRequest_decline,
onClick: function () {
cb(false);
},
keys: [[13, 'ctrl']]
}];
var modal = dialog.customModal(content, {buttons: buttons});
UI.openCustomModal(modal);
return modal;
2019-09-20 13:26:58 +00:00
};
UI.log = function (msg) {
Alertify.success(Util.fixHTML(msg));
};
UI.warn = function (msg) {
Alertify.error(Util.fixHTML(msg));
};
UI.passwordInput = function (opts, displayEye) {
opts = opts || {};
var attributes = merge({
type: 'password'
}, opts);
var input = h('input.cp-password-input', attributes);
var eye = h('span.fa.fa-eye.cp-password-reveal');
var $eye = $(eye);
var $input = $(input);
if (displayEye) {
$eye.mousedown(function () {
$input.prop('type', 'text');
$input.focus();
2019-11-18 09:56:24 +00:00
}).mouseup(function(){
$input.prop('type', 'password');
$input.focus();
2019-11-18 09:56:24 +00:00
}).mouseout(function(){
$input.prop('type', 'password');
$input.focus();
2019-11-18 09:56:24 +00:00
});
} else {
$eye.click(function () {
if ($eye.hasClass('fa-eye')) {
$input.prop('type', 'text');
$input.focus();
$eye.removeClass('fa-eye').addClass('fa-eye-slash');
2019-11-18 09:56:24 +00:00
return;
}
$input.prop('type', 'password');
$input.focus();
$eye.removeClass('fa-eye-slash').addClass('fa-eye');
2019-11-18 09:56:24 +00:00
});
}
return h('span.cp-password-container', [
input,
eye
]);
};
UI.createHelper = function (href, text) {
var q = h('a.fa.fa-question-circle', {
2020-06-10 10:50:18 +00:00
'data-cptippy-html': true,
style: 'text-decoration: none !important;',
title: text,
href: href,
target: "_blank",
'data-tippy-placement': "right"
});
return q;
};
/*
* 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;
},
};
};
2017-09-13 14:19:26 +00:00
var LOADING = 'cp-loading';
UI.addLoadingScreen = function (config) {
config = config || {};
var loadingText = config.loadingText;
var todo = function () {
var $loading = $('#' + LOADING);
2020-10-28 15:10:34 +00:00
// Show the loading screen
$loading.css('display', '');
2017-12-21 17:21:10 +00:00
$loading.removeClass('cp-loading-hidden');
2020-10-28 15:10:34 +00:00
if (config.newProgress) {
// XXX re-add progress bar for step 6 after password prompt for PPP
// XXX also burn after reading
var progress = h('div.cp-loading-progress', [
2020-10-28 15:10:34 +00:00
h('p.cp-loading-progress-list'),
h('p.cp-loading-progress-container')
]);
2020-10-28 15:10:34 +00:00
$loading.find('.cp-loading-spinner-container').after(progress);
}
if (!$loading.find('.cp-loading-progress').length) {
// Add spinner
$('.cp-loading-spinner-container').show();
}
2020-10-28 15:10:34 +00:00
// Add loading text
if (loadingText) {
$('#' + LOADING).find('#cp-loading-message').show().text(loadingText);
} else {
$('#' + LOADING).find('#cp-loading-message').hide().text('');
}
};
if ($('#' + LOADING).length) {
todo();
} else {
Loading();
todo();
}
};
2020-10-14 13:21:26 +00:00
UI.updateLoadingProgress = function (data) {
2020-10-14 13:20:56 +00:00
if (window.CryptPad_updateLoadingProgress) {
window.CryptPad_updateLoadingProgress(data);
}
};
UI.removeLoadingScreen = function (cb) {
// Release the test blocker, hopefully every test has been registered.
// This test is created in sframe-boot2.js
cb = cb || function () {};
if (Test.__ASYNC_BLOCKER__) { Test.__ASYNC_BLOCKER__.pass(); }
2020-10-28 15:10:34 +00:00
var $loading = $('#' + LOADING);
$loading.addClass("cp-loading-hidden"); // Hide the loading screen
$loading.find('.cp-loading-progress').remove(); // Remove the progress list
2017-12-22 13:42:18 +00:00
setTimeout(cb, 750);
};
2018-02-13 17:20:13 +00:00
UI.errorLoadingScreen = function (error, transparent, exitable) {
var $loading = $('#' + LOADING);
if (!$loading.is(':visible') || $loading.hasClass('cp-loading-hidden')) {
2020-10-28 15:10:34 +00:00
UI.addLoadingScreen();
2018-02-13 17:20:13 +00:00
}
2020-10-28 15:10:34 +00:00
// Remove the progress list
$loading.find('.cp-loading-progress').remove();
2020-10-28 15:10:34 +00:00
// Hide the spinner
2017-09-13 14:19:26 +00:00
$('.cp-loading-spinner-container').hide();
if (transparent) { $loading.css('opacity', 0.9); }
2020-10-28 15:10:34 +00:00
// Add the error message
var $error = $loading.find('#cp-loading-message').show();
if (error instanceof Element) {
$error.html('').append(error);
} else {
$error.html(error || Messages.error);
}
2018-02-13 17:20:13 +00:00
if (exitable) {
$(window).focus();
$(window).keydown(function (e) {
if (e.which === 27) {
$loading.hide();
if (typeof(exitable) === "function") { exitable(); }
}
2018-02-13 17:20:13 +00:00
});
}
};
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]) {
2018-11-05 11:02:27 +00:00
var icon = AppConfig.applicationsIcon[type];
var font = icon.indexOf('cptools') === 0 ? 'cptools' : 'fa';
if (type === 'fileupload') { type = 'file'; }
2019-07-17 10:15:14 +00:00
if (type === 'folderupload') { type = 'file'; }
var appClass = ' cp-icon cp-icon-color-'+type;
2018-11-05 11:02:27 +00:00
$icon = $('<span>', {'class': font + ' ' + icon + appClass});
2017-06-29 13:15:40 +00:00
}
return $icon;
};
UI.getFileIcon = function (data) {
var $icon = UI.getIcon();
if (!data) { return $icon; }
2018-06-28 13:31:30 +00:00
var href = data.href || data.roHref;
2018-03-13 10:31:08 +00:00
var type = data.type;
if (!href && !type) { return $icon; }
2018-03-13 10:31:08 +00:00
if (!type) { type = Hash.parsePadUrl(href).type; }
$icon = UI.getIcon(type);
return $icon;
};
2017-06-29 13:15:40 +00:00
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.
$('.tippy-popper').each(function (i, el) {
2019-06-20 09:51:47 +00:00
if (el._tippy && el._tippy.reference && document.body.contains(el._tippy.reference)) {
2019-06-20 09:50:14 +00:00
el._tippy.destroy();
el.remove();
return;
}
if ($('[aria-describedby=' + el.getAttribute('id') + ']').length === 0) {
el.remove();
}
});
2017-07-31 10:29:41 +00:00
};
2018-04-12 17:08:08 +00:00
var delay = typeof(AppConfig.tooltipDelay) === "number" ? AppConfig.tooltipDelay : 500;
$.extend(true, Tippy.defaults, {
placement: 'bottom',
performance: true,
delay: [delay, 0],
//sticky: true,
theme: 'cryptpad',
arrow: true,
maxWidth: '200px',
2018-04-13 13:25:14 +00:00
flip: true,
2018-04-12 17:08:08 +00:00
popperOptions: {
modifiers: {
preventOverflow: { boundariesElement: 'window' }
}
},
//arrowType: 'round',
2020-04-29 15:54:54 +00:00
dynamicTitle: false,
2018-04-12 17:08:08 +00:00
arrowTransform: 'scale(2)',
zIndex: 100000001
2018-04-12 17:08:08 +00:00
});
2017-07-19 15:14:10 +00:00
UI.addTooltips = function () {
var MutationObserver = window.MutationObserver;
var addTippy = function (i, el) {
if (el._tippy) { return; }
2020-05-06 14:31:17 +00:00
if (!el.getAttribute('title')) { return; }
2017-07-19 15:14:10 +00:00
if (el.nodeName === 'IFRAME') { return; }
2018-04-12 17:08:08 +00:00
var opts = {
distance: 15
};
Array.prototype.slice.apply(el.attributes).filter(function (obj) {
return /^data-tippy-/.test(obj.name);
}).forEach(function (obj) {
opts[obj.name.slice(11)] = obj.value;
2017-07-19 15:14:10 +00:00
});
2020-04-29 10:30:59 +00:00
if (!el.getAttribute('data-cptippy-html') && !el.fixHTML) {
el.setAttribute('title', Util.fixHTML(el.getAttribute('title'))); // fixHTML
el.fixHTML = true; // Don't clean HTML twice on the same element
}
2018-04-12 17:08:08 +00:00
Tippy(el, opts);
2017-07-19 15:14:10 +00:00
};
// This is the robust solution to remove dangling tooltips
// The mutation observer does not always find removed nodes.
//setInterval(UI.clearTooltips, delay);
2019-06-12 09:36:08 +00:00
$('[title]').each(addTippy);
2017-07-19 15:14:10 +00:00
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
2017-11-15 15:31:26 +00:00
if (mutation.type === "childList") {
for (var i = 0; i < mutation.addedNodes.length; i++) {
2019-06-20 09:50:14 +00:00
if ($(mutation.addedNodes[i]).attr('title')) {
addTippy(0, mutation.addedNodes[i]);
}
2017-11-15 15:31:26 +00:00
$(mutation.addedNodes[i]).find('[title]').each(addTippy);
}
2019-06-12 09:36:08 +00:00
if (mutation.removedNodes.length !== 0) {
UI.clearTooltips();
2017-11-15 15:31:26 +00:00
}
}
2017-11-15 15:31:26 +00:00
if (mutation.type === "attributes" && mutation.attributeName === "title") {
2020-04-29 15:54:54 +00:00
mutation.target.fixHTML = false;
2017-11-15 15:31:26 +00:00
addTippy(0, mutation.target);
2017-07-19 15:14:10 +00:00
}
});
});
observer.observe($('body')[0], {
2017-11-15 15:31:26 +00:00
attributes: true,
2017-07-19 15:14:10 +00:00
childList: true,
characterData: false,
subtree: true
});
};
2018-10-03 15:25:04 +00:00
UI.createCheckbox = function (id, labelTxt, checked, opts) {
opts = opts|| {};
// Input properties
var inputOpts = {
type: 'checkbox',
id: id
};
if (checked) { inputOpts.checked = 'checked'; }
$.extend(inputOpts, opts.input || {});
// Label properties
var labelOpts = {};
$.extend(labelOpts, opts.label || {});
if (labelOpts.class) { labelOpts.class += ' cp-checkmark'; }
// Mark properties
var markOpts = { tabindex: 0 };
$.extend(markOpts, opts.mark || {});
var input = h('input', inputOpts);
var $input = $(input);
2018-10-03 15:25:04 +00:00
var mark = h('span.cp-checkmark-mark', markOpts);
var $mark = $(mark);
2018-10-03 15:25:04 +00:00
var label = h('span.cp-checkmark-label', labelTxt);
$mark.keydown(function (e) {
2018-10-03 15:25:04 +00:00
if (e.which === 32) {
e.stopPropagation();
e.preventDefault();
$input.prop('checked', !$input.is(':checked'));
$input.change();
2018-10-03 15:25:04 +00:00
}
});
$input.change(function () { $mark.focus(); });
2018-10-03 15:25:04 +00:00
return h('label.cp-checkmark', labelOpts, [
input,
mark,
label
]);
};
UI.createRadio = function (name, id, labelTxt, checked, opts) {
opts = opts|| {};
// Input properties
var inputOpts = {
type: 'radio',
id: id,
name: name
};
if (checked) { inputOpts.checked = 'checked'; }
$.extend(inputOpts, opts.input || {});
// Label properties
var labelOpts = {};
$.extend(labelOpts, opts.label || {});
if (labelOpts.class) { labelOpts.class += ' cp-checkmark'; }
// Mark properties
var markOpts = { tabindex: 0 };
$.extend(markOpts, opts.mark || {});
var input = h('input', inputOpts);
var mark = h('span.cp-radio-mark', markOpts);
var label = h('span.cp-checkmark-label', labelTxt);
$(mark).keydown(function (e) {
if (e.which === 32) {
e.stopPropagation();
e.preventDefault();
if ($(input).is(':checked')) { return; }
2018-10-03 15:25:04 +00:00
$(input).prop('checked', !$(input).is(':checked'));
$(input).change();
}
});
$(input).change(function () { $(mark).focus(); });
2018-04-16 17:07:54 +00:00
2018-10-03 15:25:04 +00:00
var radio = h('label', labelOpts, [
input,
mark,
label
]);
$(radio).addClass('cp-radio');
return radio;
};
2018-04-16 17:07:54 +00:00
2020-02-03 14:14:52 +00:00
var corner = {
queue: [],
state: false
};
UI.cornerPopup = function (text, actions, footer, opts) {
opts = opts || {};
2020-02-03 14:14:52 +00:00
var dontShowAgain = h('div.cp-corner-dontshow', [
h('span.fa.fa-times'),
Messages.dontShowAgain
2020-02-03 14:14:52 +00:00
]);
2018-08-27 12:58:09 +00:00
var popup = h('div.cp-corner-container', [
2018-10-03 15:25:04 +00:00
setHTML(h('div.cp-corner-text'), text),
2018-08-27 12:58:09 +00:00
h('div.cp-corner-actions', actions),
2020-02-03 14:14:52 +00:00
setHTML(h('div.cp-corner-footer'), footer),
opts.dontShowAgain ? dontShowAgain : undefined
2018-08-27 12:58:09 +00:00
]);
var $popup = $(popup);
if (opts.hidden) {
$popup.addClass('cp-minimized');
}
if (opts.big) {
$popup.addClass('cp-corner-big');
}
2020-02-03 14:14:52 +00:00
if (opts.alt) {
$popup.addClass('cp-corner-alt');
}
2018-08-27 12:58:09 +00:00
var hide = function () {
$popup.hide();
2018-08-27 12:58:09 +00:00
};
var show = function () {
$popup.show();
2018-08-27 12:58:09 +00:00
};
var deletePopup = function () {
$popup.remove();
2020-02-03 14:14:52 +00:00
if (!corner.queue.length) {
// Make sure no other popup is displayed in the next 5s
setTimeout(function () {
if (corner.queue.length) {
$('body').append(corner.queue.pop());
return;
}
corner.state = false;
}, 5000);
2020-02-03 14:14:52 +00:00
return;
}
setTimeout(function () {
$('body').append(corner.queue.pop());
}, 5000);
2018-08-27 12:58:09 +00:00
};
2020-02-03 14:14:52 +00:00
$(dontShowAgain).click(function () {
deletePopup();
if (typeof(opts.dontShowAgain) === "function") {
opts.dontShowAgain();
}
});
if (corner.state) {
corner.queue.push(popup);
} else {
corner.state = true;
$('body').append(popup);
}
2018-08-27 12:58:09 +00:00
return {
popup: popup,
2018-08-27 12:58:09 +00:00
hide: hide,
show: show,
delete: deletePopup
};
};
UI.makeSpinner = function ($container) {
var $ok = $('<span>', {'class': 'fa fa-check', title: Messages.saved}).hide();
var $spinner = $('<span>', {'class': 'fa fa-spinner fa-pulse'}).hide();
2020-02-18 15:10:39 +00:00
var state = false;
2020-03-06 12:52:46 +00:00
var to;
2020-02-18 15:10:39 +00:00
var spin = function () {
2020-03-06 12:52:46 +00:00
clearTimeout(to);
2020-02-18 15:10:39 +00:00
state = true;
$ok.hide();
$spinner.show();
};
var hide = function () {
2020-03-06 12:52:46 +00:00
clearTimeout(to);
2020-02-18 15:10:39 +00:00
state = false;
$ok.hide();
$spinner.hide();
};
var done = function () {
2020-03-06 12:52:46 +00:00
clearTimeout(to);
2020-02-18 15:10:39 +00:00
state = false;
$ok.show();
$spinner.hide();
2020-03-06 12:52:46 +00:00
to = setTimeout(function () {
$ok.hide();
}, 500);
};
if ($container && $container.append) {
$container.append($ok);
$container.append($spinner);
}
return {
2020-02-18 15:10:39 +00:00
getState: function () { return state; },
ok: $ok[0],
spinner: $spinner[0],
spin: spin,
hide: hide,
done: done
2020-01-29 17:40:18 +00:00
};
};
UI.createContextMenu = function (menu) {
var $menu = $(menu).appendTo($('body'));
var display = function (e) {
$menu.css({ display: "block" });
var h = $menu.outerHeight();
var w = $menu.outerWidth();
var wH = window.innerHeight;
var wW = window.innerWidth;
if (h > wH) {
$menu.css({
top: '0px',
bottom: ''
});
} else if (e.pageY + h <= wH) {
$menu.css({
top: e.pageY+'px',
bottom: ''
});
} else {
$menu.css({
bottom: '0px',
top: ''
});
}
if(w > wW) {
$menu.css({
left: '0px',
right: ''
});
} else if (e.pageX + w <= wW) {
$menu.css({
left: e.pageX+'px',
right: ''
});
} else {
$menu.css({
left: '',
right: '0px',
});
}
};
var hide = function () {
$menu.hide();
};
var remove = function () {
$menu.remove();
};
$('body').click(hide);
return {
menu: menu,
show: display,
hide: hide,
remove: remove
};
};
2020-05-26 16:51:37 +00:00
/* Given two jquery objects (a 'button' and a 'drawer')
add handlers to make it such that clicking the button
displays the drawer contents, and blurring the button
hides the drawer content. Used for toolbar buttons at the moment.
*/
UI.createDrawer = function ($button, $content) {
$button.click(function () {
$content.toggle();
$button.removeClass('cp-toolbar-button-active');
if ($content.is(':visible')) {
$button.addClass('cp-toolbar-button-active');
$content.focus();
var wh = $(window).height();
var topPos = $button[0].getBoundingClientRect().bottom;
$content.css('max-height', Math.floor(wh - topPos - 1)+'px');
}
});
var onBlur = function (e) {
if (e.relatedTarget) {
var $relatedTarget = $(e.relatedTarget);
if ($relatedTarget.is('.cp-toolbar-drawer-button')) { return; }
if ($relatedTarget.parents('.cp-toolbar-drawer-content').length) {
$relatedTarget.blur(onBlur);
return;
}
}
$button.removeClass('cp-toolbar-button-active');
$content.hide();
};
$content.blur(onBlur).appendTo($button);
2020-05-26 20:22:45 +00:00
$('body').keydown(function (e) {
if (e.which === 27) {
$content.blur();
}
});
2020-05-26 16:51:37 +00:00
};
return UI;
});