2012-11-16 16:45:39 +01:00
|
|
|
var ui = (function () {
|
|
|
|
|
2019-03-01 01:40:05 +01:00
|
|
|
var SimpleBar = require("simplebar").default;
|
|
|
|
|
2012-11-16 16:45:39 +01:00
|
|
|
var exports = {};
|
|
|
|
|
2012-12-03 20:54:29 +01:00
|
|
|
// What, if anything, obscures the home tab?
|
2012-11-30 18:28:23 +01:00
|
|
|
|
2013-07-23 00:25:25 +02:00
|
|
|
exports.replace_emoji_with_text = function (element) {
|
|
|
|
element.find(".emoji").replaceWith(function () {
|
2017-10-14 23:14:45 +02:00
|
|
|
if ($(this).is("img")) {
|
|
|
|
return $(this).attr("alt");
|
|
|
|
}
|
2017-09-27 19:39:42 +02:00
|
|
|
return $(this).text();
|
2013-07-23 00:25:25 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-03-01 01:40:05 +01:00
|
|
|
exports.set_up_scrollbar = function (element_selector) {
|
|
|
|
new SimpleBar(element_selector.expectOne()[0]);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.get_content_element = function (element_selector) {
|
|
|
|
var element = element_selector.expectOne()[0];
|
|
|
|
if (element.SimpleBar) {
|
|
|
|
return $(element.SimpleBar.getContentElement());
|
|
|
|
}
|
|
|
|
return element_selector;
|
2017-08-02 16:06:38 +02:00
|
|
|
};
|
|
|
|
|
2019-03-01 01:40:05 +01:00
|
|
|
exports.get_scroll_element = function (element_selector) {
|
|
|
|
var element = element_selector.expectOne()[0];
|
|
|
|
if (element.SimpleBar) {
|
|
|
|
return $(element.SimpleBar.getScrollElement());
|
2019-01-09 14:30:35 +01:00
|
|
|
}
|
2019-03-01 01:40:05 +01:00
|
|
|
return element_selector;
|
2019-01-09 14:30:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.reset_scrollbar = function (element_selector) {
|
2019-03-01 01:40:05 +01:00
|
|
|
var element = element_selector.expectOne()[0];
|
|
|
|
if (element.SimpleBar) {
|
|
|
|
element.SimpleBar.getScrollElement().scrollTop = 0;
|
|
|
|
} else {
|
|
|
|
element.scrollTop = 0;
|
2018-03-20 04:10:20 +01:00
|
|
|
}
|
2017-08-02 16:06:38 +02:00
|
|
|
};
|
|
|
|
|
2019-03-01 01:40:05 +01:00
|
|
|
exports.destroy_scrollbar = function (element_selector) {
|
|
|
|
element_selector.expectOne()[0].SimpleBar.unMount();
|
2018-03-12 03:01:11 +01:00
|
|
|
};
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
function update_message_in_all_views(message_id, callback) {
|
2016-04-25 23:45:25 +02:00
|
|
|
_.each([message_list.all, home_msg_list, message_list.narrowed], function (list) {
|
2013-12-19 17:03:08 +01:00
|
|
|
if (list === undefined) {
|
2013-03-27 18:30:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
var row = list.get_row(message_id);
|
2013-03-27 18:30:02 +01:00
|
|
|
if (row === undefined) {
|
2013-12-19 17:03:08 +01:00
|
|
|
// The row may not exist, e.g. if you do an action on a message in
|
|
|
|
// a narrowed view
|
2013-03-27 18:30:02 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-12-19 17:03:08 +01:00
|
|
|
callback(row);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-16 01:16:42 +02:00
|
|
|
exports.show_error_for_unsupported_platform = function () {
|
2017-09-20 14:14:37 +02:00
|
|
|
// Check if the user is using old desktop app
|
2018-06-05 08:12:06 +02:00
|
|
|
if (typeof bridge !== 'undefined') {
|
2017-09-16 01:16:42 +02:00
|
|
|
// We don't internationalize this string because it is long,
|
|
|
|
// and few users will have both the old desktop app and an
|
|
|
|
// internationalized version of Zulip anyway.
|
|
|
|
var error = "Hello! You're using the unsupported old Zulip desktop app," +
|
|
|
|
" which is no longer developed. We recommend switching to the new, " +
|
|
|
|
"modern desktop app, which you can download at " +
|
|
|
|
"<a href='https://zulipchat.com/apps'>zulipchat.com/apps</a>.";
|
|
|
|
|
|
|
|
ui_report.generic_embed_error(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-14 15:30:29 +01:00
|
|
|
exports.find_message = function (message_id) {
|
2014-01-28 22:44:34 +01:00
|
|
|
// Try to find the message object. It might be in the narrow list
|
2016-04-21 22:49:23 +02:00
|
|
|
// (if it was loaded when narrowed), or only in the message_list.all
|
2014-01-28 22:44:34 +01:00
|
|
|
// (if received from the server while in a different narrow)
|
|
|
|
var message;
|
2016-04-25 23:45:25 +02:00
|
|
|
_.each([message_list.all, home_msg_list, message_list.narrowed], function (msg_list) {
|
2014-01-28 22:44:34 +01:00
|
|
|
if (msg_list !== undefined && message === undefined) {
|
|
|
|
message = msg_list.get(message_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return message;
|
2014-03-14 15:30:29 +01:00
|
|
|
};
|
2014-01-28 22:44:34 +01:00
|
|
|
|
2018-08-01 19:02:38 +02:00
|
|
|
exports.update_starred_view = function (message_id, new_value) {
|
2018-07-31 21:12:01 +02:00
|
|
|
var starred = new_value;
|
2013-12-19 17:03:08 +01:00
|
|
|
|
|
|
|
// Avoid a full re-render, but update the star in each message
|
|
|
|
// table in which it is visible.
|
2018-07-31 21:12:01 +02:00
|
|
|
update_message_in_all_views(message_id, function update_row(row) {
|
2017-02-23 23:33:57 +01:00
|
|
|
var elt = row.find(".star");
|
2014-01-23 17:52:28 +01:00
|
|
|
if (starred) {
|
2018-07-04 01:42:24 +02:00
|
|
|
elt.addClass("fa-star").removeClass("fa-star-o").removeClass("empty-star");
|
2014-01-23 17:52:28 +01:00
|
|
|
} else {
|
2018-07-04 01:42:24 +02:00
|
|
|
elt.removeClass("fa-star").addClass("fa-star-o").addClass("empty-star");
|
2014-01-23 17:52:28 +01:00
|
|
|
}
|
2017-06-29 08:35:33 +02:00
|
|
|
var title_state = starred ? i18n.t("Unstar") : i18n.t("Star");
|
|
|
|
elt.attr("title", i18n.t("__starred_status__ this message", {starred_status: title_state}));
|
2013-03-27 18:30:02 +01:00
|
|
|
});
|
2014-01-23 17:52:28 +01:00
|
|
|
};
|
2013-03-27 18:30:02 +01:00
|
|
|
|
2014-01-02 17:34:16 +01:00
|
|
|
exports.show_message_failed = function (message_id, failed_msg) {
|
2013-12-19 17:03:08 +01:00
|
|
|
// Failed to send message, so display inline retry/cancel
|
|
|
|
update_message_in_all_views(message_id, function update_row(row) {
|
2014-01-02 17:34:16 +01:00
|
|
|
var failed_div = row.find('.message_failed');
|
|
|
|
failed_div.toggleClass('notvisible', false);
|
|
|
|
failed_div.find('.failed_text').attr('title', failed_msg);
|
2013-12-19 17:03:08 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-05-14 21:14:26 +02:00
|
|
|
exports.remove_message = function (message_id) {
|
|
|
|
_.each([message_list.all, home_msg_list, message_list.narrowed], function (list) {
|
|
|
|
if (list === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var row = list.get_row(message_id);
|
|
|
|
if (row !== undefined) {
|
|
|
|
list.remove_and_rerender([{id: message_id}]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-19 17:03:08 +01:00
|
|
|
exports.show_failed_message_success = function (message_id) {
|
|
|
|
// Previously failed message succeeded
|
|
|
|
update_message_in_all_views(message_id, function update_row(row) {
|
|
|
|
row.find('.message_failed').toggleClass('notvisible', true);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-07-21 02:19:14 +02:00
|
|
|
exports.get_hotkey_deprecation_notice = function (originalHotkey, replacementHotkey) {
|
|
|
|
return i18n.t(
|
|
|
|
'We\'ve replaced the "__originalHotkey__" hotkey with "__replacementHotkey__" '
|
|
|
|
+ 'to make this common shortcut easier to trigger.',
|
|
|
|
{ originalHotkey: originalHotkey, replacementHotkey: replacementHotkey }
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-04-02 01:19:47 +02:00
|
|
|
var shown_deprecation_notices = [];
|
2018-02-08 15:17:26 +01:00
|
|
|
exports.maybe_show_deprecation_notice = function (key) {
|
|
|
|
var message;
|
2018-09-20 10:03:13 +02:00
|
|
|
var isCmdOrCtrl = /Mac/i.test(navigator.userAgent) ? "Cmd" : "Ctrl";
|
2018-02-08 15:17:26 +01:00
|
|
|
if (key === 'C') {
|
2018-07-21 02:19:14 +02:00
|
|
|
message = exports.get_hotkey_deprecation_notice('C', 'x');
|
2018-06-02 15:48:15 +02:00
|
|
|
} else if (key === '*') {
|
2018-09-20 10:03:13 +02:00
|
|
|
message = exports.get_hotkey_deprecation_notice("*", isCmdOrCtrl + " + s");
|
2018-02-08 15:17:26 +01:00
|
|
|
} else {
|
2018-04-02 01:19:47 +02:00
|
|
|
blueslip.error("Unexpected deprecation notice for hotkey:", key);
|
2018-02-08 15:17:26 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-02 01:19:47 +02:00
|
|
|
// Here we handle the tracking for showing deprecation notices,
|
|
|
|
// whether or not local storage is available.
|
|
|
|
if (localstorage.supported()) {
|
|
|
|
var notices_from_storage = JSON.parse(localStorage.getItem('shown_deprecation_notices'));
|
|
|
|
if (notices_from_storage !== null) {
|
|
|
|
shown_deprecation_notices = notices_from_storage;
|
|
|
|
} else {
|
|
|
|
shown_deprecation_notices = [];
|
|
|
|
}
|
2018-02-08 15:17:26 +01:00
|
|
|
}
|
2018-04-02 01:19:47 +02:00
|
|
|
|
2018-02-08 15:17:26 +01:00
|
|
|
if (shown_deprecation_notices.indexOf(key) === -1) {
|
|
|
|
$('#deprecation-notice-modal').modal('show');
|
|
|
|
$('#deprecation-notice-message').text(message);
|
2018-06-02 22:28:08 +02:00
|
|
|
$('#close-deprecation-notice').focus();
|
2018-02-08 15:17:26 +01:00
|
|
|
shown_deprecation_notices.push(key);
|
2018-04-02 01:19:47 +02:00
|
|
|
if (localstorage.supported()) {
|
|
|
|
localStorage.setItem('shown_deprecation_notices', JSON.stringify(shown_deprecation_notices));
|
|
|
|
}
|
2018-02-08 15:17:26 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-18 08:16:57 +01:00
|
|
|
// Save the compose content cursor position and restore when we
|
|
|
|
// shift-tab back in (see hotkey.js).
|
|
|
|
var saved_compose_cursor = 0;
|
|
|
|
|
2018-05-15 20:43:14 +02:00
|
|
|
exports.set_compose_textarea_handlers = function () {
|
2017-11-26 20:37:44 +01:00
|
|
|
$('#compose-textarea').blur(function () {
|
2016-06-14 22:49:41 +02:00
|
|
|
saved_compose_cursor = $(this).caret();
|
2013-02-18 08:16:57 +01:00
|
|
|
});
|
2017-04-13 22:33:26 +02:00
|
|
|
|
|
|
|
// on the end of the modified-message fade in, remove the fade-in-message class.
|
|
|
|
var animationEnd = "webkitAnimationEnd oanimationend msAnimationEnd animationend";
|
|
|
|
$("body").on(animationEnd, ".fade-in-message", function () {
|
|
|
|
$(this).removeClass("fade-in-message");
|
|
|
|
});
|
2018-05-15 20:43:14 +02:00
|
|
|
};
|
2013-02-18 08:16:57 +01:00
|
|
|
|
|
|
|
exports.restore_compose_cursor = function () {
|
2017-11-26 20:37:44 +01:00
|
|
|
$('#compose-textarea')
|
2013-02-18 08:16:57 +01:00
|
|
|
.focus()
|
2016-06-14 22:49:41 +02:00
|
|
|
.caret(saved_compose_cursor);
|
2013-02-18 08:16:57 +01:00
|
|
|
};
|
2013-07-24 19:43:45 +02:00
|
|
|
|
2017-08-08 02:16:35 +02:00
|
|
|
exports.initialize = function () {
|
2018-05-15 20:43:14 +02:00
|
|
|
exports.set_compose_textarea_handlers();
|
2017-09-16 01:16:42 +02:00
|
|
|
exports.show_error_for_unsupported_platform();
|
2017-08-08 02:16:35 +02:00
|
|
|
};
|
|
|
|
|
2012-11-16 16:45:39 +01:00
|
|
|
return exports;
|
|
|
|
}());
|
2016-12-29 12:23:07 +01:00
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = ui;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
window.ui = ui;
|