Files
mattermost/web/react/utils/utils.jsx

976 lines
36 KiB
React
Raw Normal View History

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
var AppDispatcher = require('../dispatcher/app_dispatcher.jsx');
2015-08-18 09:43:22 -04:00
var ChannelStore = require('../stores/channel_store.jsx');
2015-06-14 23:53:32 -08:00
var UserStore = require('../stores/user_store.jsx');
var PreferenceStore = require('../stores/preference_store.jsx');
2015-09-25 09:16:26 -07:00
var TeamStore = require('../stores/team_store.jsx');
2015-06-14 23:53:32 -08:00
var Constants = require('../utils/constants.jsx');
var ActionTypes = Constants.ActionTypes;
var AsyncClient = require('./async_client.jsx');
var client = require('./client.jsx');
var Autolinker = require('autolinker');
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function isEmail(email) {
//var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var regex = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
2015-08-18 09:43:22 -04:00
return regex.test(email);
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function cleanUpUrlable(input) {
var cleaned = input.trim().replace(/-/g, ' ').replace(/[^\w\s]/gi, '').toLowerCase().replace(/\s/g, '-');
cleaned = cleaned.replace(/^\-+/, '');
cleaned = cleaned.replace(/\-+$/, '');
return cleaned;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function isTestDomain() {
2015-08-18 09:43:22 -04:00
if ((/^localhost/).test(window.location.hostname)) {
2015-06-14 23:53:32 -08:00
return true;
2015-08-18 09:43:22 -04:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if ((/^dockerhost/).test(window.location.hostname)) {
2015-06-18 10:55:51 -08:00
return true;
2015-08-18 09:43:22 -04:00
}
2015-06-18 10:55:51 -08:00
2015-08-18 09:43:22 -04:00
if ((/^test/).test(window.location.hostname)) {
2015-06-14 23:53:32 -08:00
return true;
2015-08-18 09:43:22 -04:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if ((/^127.0./).test(window.location.hostname)) {
2015-06-14 23:53:32 -08:00
return true;
2015-08-18 09:43:22 -04:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if ((/^192.168./).test(window.location.hostname)) {
2015-06-14 23:53:32 -08:00
return true;
2015-08-18 09:43:22 -04:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if ((/^10./).test(window.location.hostname)) {
2015-06-14 23:53:32 -08:00
return true;
2015-08-18 09:43:22 -04:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if ((/^176./).test(window.location.hostname)) {
2015-06-14 23:53:32 -08:00
return true;
2015-08-18 09:43:22 -04:00
}
2015-06-14 23:53:32 -08:00
return false;
}
2015-09-17 22:00:33 -07:00
export function isInRole(roles, inRole) {
var parts = roles.split(' ');
for (var i = 0; i < parts.length; i++) {
if (parts[i] === inRole) {
return true;
}
}
return false;
}
export function isAdmin(roles) {
if (isInRole(roles, 'admin')) {
return true;
}
if (isInRole(roles, 'system_admin')) {
return true;
}
return false;
}
export function isSystemAdmin(roles) {
if (isInRole(roles, 'system_admin')) {
return true;
}
return false;
}
2015-09-02 10:42:26 -04:00
export function getDomainWithOutSub() {
2015-08-18 09:43:22 -04:00
var parts = window.location.host.split('.');
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (parts.length === 1) {
if (parts[0].indexOf('dockerhost') > -1) {
return 'dockerhost:8065';
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
return 'localhost:8065';
2015-06-18 10:55:51 -08:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
return parts[1] + '.' + parts[2];
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getCookie(name) {
2015-08-18 09:43:22 -04:00
var value = '; ' + document.cookie;
var parts = value.split('; ' + name + '=');
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
2015-09-02 10:42:26 -04:00
}
2015-09-02 10:42:26 -04:00
export function notifyMe(title, body, channel) {
2015-08-18 09:43:22 -04:00
if ('Notification' in window && Notification.permission !== 'denied') {
Notification.requestPermission(function onRequestPermission(permission) {
if (Notification.permission !== permission) {
Notification.permission = permission;
}
if (permission === 'granted') {
var notification = new Notification(title, {body: body, tag: body, icon: '/static/images/icon50x50.gif'});
notification.onclick = function onClick() {
window.focus();
if (channel) {
2015-09-02 10:42:26 -04:00
switchChannel(channel);
2015-08-18 09:43:22 -04:00
} else {
2015-09-25 09:16:26 -07:00
window.location.href = TeamStore.getCurrentTeamUrl() + '/channels/town-square';
2015-08-18 09:43:22 -04:00
}
};
setTimeout(function closeNotificationOnTimeout() {
notification.close();
}, 5000);
}
});
}
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function ding() {
if (!isBrowserFirefox()) {
var audio = new Audio('/static/images/ding.mp3');
audio.play();
}
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getUrlParameter(sParam) {
2015-06-14 23:53:32 -08:00
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
2015-08-18 09:43:22 -04:00
for (var i = 0; i < sURLVariables.length; i++) {
2015-06-14 23:53:32 -08:00
var sParameterName = sURLVariables[i].split('=');
2015-08-18 09:43:22 -04:00
if (sParameterName[0] === sParam) {
2015-06-14 23:53:32 -08:00
return sParameterName[1];
}
}
return null;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getDateForUnixTicks(ticks) {
2015-08-18 09:43:22 -04:00
return new Date(ticks);
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function displayDate(ticks) {
2015-08-18 09:43:22 -04:00
var d = new Date(ticks);
var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
return monthNames[d.getMonth()] + ' ' + d.getDate() + ', ' + d.getFullYear();
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function displayTime(ticks) {
const d = new Date(ticks);
let hours = d.getHours();
let minutes = d.getMinutes();
let ampm = '';
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (minutes <= 9) {
minutes = '0' + minutes;
2015-08-18 09:43:22 -04:00
}
const useMilitaryTime = PreferenceStore.getPreference(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'use_military_time', {value: 'false'}).value;
2015-10-15 00:05:39 +03:00
if (useMilitaryTime === 'false') {
ampm = ' AM';
if (hours >= 12) {
ampm = ' PM';
}
hours = hours % 12;
if (!hours) {
hours = '12';
}
}
return hours + ':' + minutes + ampm;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function displayDateTime(ticks) {
2015-08-18 09:43:22 -04:00
var seconds = Math.floor((Date.now() - ticks) / 1000);
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
var interval = Math.floor(seconds / 3600);
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (interval > 24) {
return this.displayTime(ticks);
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (interval > 1) {
return interval + ' hours ago';
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (interval === 1) {
return interval + ' hour ago';
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + ' minutes ago';
}
return '1 minute ago';
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function displayCommentDateTime(ticks) {
return displayDate(ticks) + ' ' + displayTime(ticks);
}
2015-06-14 23:53:32 -08:00
// returns Unix timestamp in milliseconds
2015-09-02 10:42:26 -04:00
export function getTimestamp() {
2015-06-14 23:53:32 -08:00
return Date.now();
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
function testUrlMatch(text) {
2015-07-16 14:22:15 -07:00
var urlMatcher = new Autolinker.matchParser.MatchParser({
2015-08-18 09:43:22 -04:00
urls: true,
emails: false,
twitter: false,
phone: false,
hashtag: false
2015-07-16 14:22:15 -07:00
});
var result = [];
2015-08-18 09:43:22 -04:00
function replaceFn(match) {
var linkData = {};
var matchText = match.getMatchedText();
2015-08-18 09:43:22 -04:00
linkData.text = matchText;
if (matchText.trim().indexOf('http') === 0) {
2015-08-18 09:43:22 -04:00
linkData.link = matchText;
} else {
linkData.link = 'http://' + matchText;
2015-08-18 09:43:22 -04:00
}
2015-08-18 09:43:22 -04:00
result.push(linkData);
}
2015-08-18 09:43:22 -04:00
urlMatcher.replace(text, replaceFn, this);
return result;
}
2015-09-02 10:42:26 -04:00
export function extractLinks(text) {
2015-08-18 09:43:22 -04:00
var repRegex = new RegExp('<br>', 'g');
var matches = testUrlMatch(text.replace(repRegex, '\n'));
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (!matches.length) {
return {links: null, text: text};
}
2015-06-14 23:53:32 -08:00
2015-07-08 10:32:33 -07:00
var links = [];
2015-06-14 23:53:32 -08:00
for (var i = 0; i < matches.length; i++) {
2015-07-08 10:32:33 -07:00
links.push(matches[i].link);
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
return {links: links, text: text};
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function escapeRegExp(string) {
2015-08-18 09:43:22 -04:00
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
2015-09-02 10:42:26 -04:00
}
2015-08-18 09:43:22 -04:00
2015-09-02 10:42:26 -04:00
export function areStatesEqual(state1, state2) {
2015-06-14 23:53:32 -08:00
return JSON.stringify(state1) === JSON.stringify(state2);
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function replaceHtmlEntities(text) {
2015-06-14 23:53:32 -08:00
var tagsToReplace = {
'&amp;': '&',
'&lt;': '<',
'&gt;': '>'
};
2015-08-18 09:43:22 -04:00
var newtext = text;
2015-06-14 23:53:32 -08:00
for (var tag in tagsToReplace) {
2015-08-18 09:43:22 -04:00
if ({}.hasOwnProperty.call(tagsToReplace, tag)) {
var regex = new RegExp(tag, 'g');
newtext = newtext.replace(regex, tagsToReplace[tag]);
}
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
return newtext;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function insertHtmlEntities(text) {
2015-06-14 23:53:32 -08:00
var tagsToReplace = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
};
2015-08-18 09:43:22 -04:00
var newtext = text;
2015-06-14 23:53:32 -08:00
for (var tag in tagsToReplace) {
2015-08-18 09:43:22 -04:00
if ({}.hasOwnProperty.call(tagsToReplace, tag)) {
var regex = new RegExp(tag, 'g');
newtext = newtext.replace(regex, tagsToReplace[tag]);
}
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
return newtext;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function searchForTerm(term) {
2015-06-14 23:53:32 -08:00
AppDispatcher.handleServerAction({
type: ActionTypes.RECIEVED_SEARCH_TERM,
term: term,
do_search: true
});
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getFileType(extin) {
2015-08-18 09:43:22 -04:00
var ext = extin.toLowerCase();
2015-06-14 23:53:32 -08:00
if (Constants.IMAGE_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'image';
2015-06-14 23:53:32 -08:00
}
if (Constants.AUDIO_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'audio';
2015-06-14 23:53:32 -08:00
}
if (Constants.VIDEO_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'video';
2015-06-14 23:53:32 -08:00
}
if (Constants.SPREADSHEET_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'spreadsheet';
2015-06-14 23:53:32 -08:00
}
if (Constants.CODE_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'code';
2015-06-14 23:53:32 -08:00
}
if (Constants.WORD_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'word';
2015-06-14 23:53:32 -08:00
}
2015-09-02 10:42:26 -04:00
if (Constants.PRESENTATION_TYPES.indexOf(ext) > -1) {
return 'presentation';
2015-06-14 23:53:32 -08:00
}
if (Constants.PDF_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'pdf';
2015-06-14 23:53:32 -08:00
}
if (Constants.PATCH_TYPES.indexOf(ext) > -1) {
2015-08-18 09:43:22 -04:00
return 'patch';
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
return 'other';
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getPreviewImagePathForFileType(fileTypeIn) {
2015-08-18 09:43:22 -04:00
var fileType = fileTypeIn.toLowerCase();
var icon;
if (fileType in Constants.ICON_FROM_TYPE) {
icon = Constants.ICON_FROM_TYPE[fileType];
} else {
2015-08-18 09:43:22 -04:00
icon = Constants.ICON_FROM_TYPE.other;
}
2015-08-18 09:43:22 -04:00
return '/static/images/icons/' + icon + '.png';
2015-09-02 10:42:26 -04:00
}
2015-09-02 10:42:26 -04:00
export function getIconClassName(fileTypeIn) {
2015-08-18 09:43:22 -04:00
var fileType = fileTypeIn.toLowerCase();
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
if (fileType in Constants.ICON_FROM_TYPE) {
2015-06-14 23:53:32 -08:00
return Constants.ICON_FROM_TYPE[fileType];
2015-08-18 09:43:22 -04:00
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
return 'glyphicon-file';
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function splitFileLocation(fileLocation) {
2015-06-14 23:53:32 -08:00
var fileSplit = fileLocation.split('.');
2015-08-18 09:43:22 -04:00
var ext = '';
if (fileSplit.length > 1) {
ext = fileSplit[fileSplit.length - 1];
fileSplit.splice(fileSplit.length - 1, 1);
}
2015-06-14 23:53:32 -08:00
var filePath = fileSplit.join('.');
2015-08-18 09:43:22 -04:00
var filename = filePath.split('/')[filePath.split('/').length - 1];
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
return {ext: ext, name: filename, path: filePath};
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function toTitleCase(str) {
2015-08-18 09:43:22 -04:00
function doTitleCase(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
return str.replace(/\w\S*/g, doTitleCase);
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-23 10:12:40 -04:00
export function applyTheme(theme) {
if (theme.sidebarBg) {
changeCss('.sidebar--left, .settings-modal .settings-table .settings-links, .sidebar--menu', 'background:' + theme.sidebarBg, 1);
2015-09-23 10:12:40 -04:00
}
if (theme.sidebarText) {
2015-10-06 22:27:05 +05:00
changeCss('.sidebar--left .nav-pills__container li>a, .sidebar--right, .settings-modal .nav-pills>li a, .sidebar--menu', 'color:' + changeOpacity(theme.sidebarText, 0.6), 1);
changeCss('@media(max-width: 768px){.settings-modal .settings-table .nav>li>a', 'color:' + theme.sidebarText, 1);
2015-10-06 22:27:05 +05:00
changeCss('.sidebar--left .nav-pills__container li>h4, .sidebar--left .add-channel-btn', 'color:' + changeOpacity(theme.sidebarText, 0.6), 1);
2015-09-23 10:12:40 -04:00
changeCss('.sidebar--left .add-channel-btn:hover, .sidebar--left .add-channel-btn:focus', 'color:' + theme.sidebarText, 1);
changeCss('.sidebar--left, .sidebar--right .sidebar--right__header', 'border-color:' + changeOpacity(theme.sidebarText, 0.2), 1);
changeCss('.sidebar--left .status path', 'fill:' + changeOpacity(theme.sidebarText, 0.5), 1);
changeCss('@media(max-width: 768px){.settings-modal .settings-table .nav>li>a', 'border-color:' + changeOpacity(theme.sidebarText, 0.2), 2);
2015-09-23 10:12:40 -04:00
}
if (theme.sidebarUnreadText) {
changeCss('.sidebar--left .nav-pills__container li>a.unread-title', 'color:' + theme.sidebarUnreadText + '!important;', 2);
2015-09-23 10:12:40 -04:00
}
if (theme.sidebarTextHoverBg) {
changeCss('.sidebar--left .nav-pills__container li>a:hover, .sidebar--left .nav-pills__container li>a:focus, .settings-modal .nav-pills>li:hover a, .settings-modal .nav-pills>li:focus a', 'background:' + theme.sidebarTextHoverBg, 1);
changeCss('@media(max-width: 768px){.settings-modal .settings-table .nav>li:hover a', 'background:' + theme.sidebarTextHoverBg, 1);
2015-09-23 10:12:40 -04:00
}
if (theme.sidebarTextActiveBorder) {
changeCss('.sidebar--left .nav li.active a:before, .settings-modal .nav-pills>li.active a:before', 'background:' + theme.sidebarTextActiveBorder, 1);
2015-09-23 10:12:40 -04:00
}
if (theme.sidebarTextActiveColor) {
changeCss('.sidebar--left .nav-pills__container li.active a, .sidebar--left .nav-pills__container li.active a:hover, .sidebar--left .nav-pills__container li.active a:focus, .settings-modal .nav-pills>li.active a, .settings-modal .nav-pills>li.active a:hover, .settings-modal .nav-pills>li.active a:active', 'color:' + theme.sidebarTextActiveColor, 2);
2015-10-12 19:22:05 +05:00
}
2015-09-23 10:12:40 -04:00
if (theme.sidebarHeaderBg) {
changeCss('.sidebar--left .team__header, .sidebar--menu .team__header', 'background:' + theme.sidebarHeaderBg, 1);
changeCss('.modal .modal-header', 'background:' + theme.sidebarHeaderBg, 1);
changeCss('#navbar .navbar-default', 'background:' + theme.sidebarHeaderBg, 1);
changeCss('@media(max-width: 768px){.search-bar__container', 'background:' + theme.sidebarHeaderBg, 1);
2015-09-23 10:12:40 -04:00
}
if (theme.sidebarHeaderTextColor) {
changeCss('.sidebar--left .team__header .header__info, .sidebar--menu .team__header .header__info', 'color:' + theme.sidebarHeaderTextColor, 1);
changeCss('.sidebar--left .team__header .user__name, .sidebar--menu .team__header .user__name', 'color:' + changeOpacity(theme.sidebarHeaderTextColor, 0.8), 1);
changeCss('.sidebar--left .team__header:hover .user__name, .sidebar--menu .team__header:hover .user__name', 'color:' + theme.sidebarHeaderTextColor, 1);
changeCss('.modal .modal-header .modal-title, .modal .modal-header .modal-title .name, .modal .modal-header button.close', 'color:' + theme.sidebarHeaderTextColor, 1);
changeCss('#navbar .navbar-default .navbar-brand .heading', 'color:' + theme.sidebarHeaderTextColor, 1);
2015-09-23 10:12:40 -04:00
changeCss('#navbar .navbar-default .navbar-toggle .icon-bar, ', 'background:' + theme.sidebarHeaderTextColor, 1);
changeCss('@media(max-width: 768px){.search-bar__container', 'color:' + theme.sidebarHeaderTextColor, 2);
2015-09-23 10:12:40 -04:00
}
if (theme.onlineIndicator) {
changeCss('.sidebar--left .status .online--icon', 'fill:' + theme.onlineIndicator, 1);
}
if (theme.mentionBj) {
changeCss('.sidebar--left .nav-pills__unread-indicator', 'background:' + theme.mentionBj, 1);
changeCss('.sidebar--left .badge', 'background:' + theme.mentionBj, 1);
}
if (theme.mentionColor) {
changeCss('.sidebar--left .nav-pills__unread-indicator', 'color:' + theme.mentionColor, 2);
changeCss('.sidebar--left .badge', 'color:' + theme.mentionColor, 2);
}
if (theme.centerChannelBg) {
changeCss('.app__content, .markdown__table, .markdown__table tbody tr, .command-box, .modal .modal-content, .mentions-name, .mentions--top .mentions-box', 'background:' + theme.centerChannelBg, 1);
2015-09-23 10:12:40 -04:00
changeCss('#post-list .post-list-holder-by-time', 'background:' + theme.centerChannelBg, 1);
changeCss('#post-create', 'background:' + theme.centerChannelBg, 1);
changeCss('.date-separator .separator__text, .new-separator .separator__text', 'background:' + theme.centerChannelBg, 1);
2015-09-24 18:55:53 +05:00
changeCss('.post-image__column .post-image__details', 'background:' + theme.centerChannelBg, 1);
changeCss('.sidebar--right, .dropdown-menu, .popover', 'background:' + theme.centerChannelBg, 1);
2015-10-15 22:54:15 +05:00
changeCss('.popover.bottom>.arrow:after', 'border-bottom-color:' + theme.centerChannelBg, 1);
changeCss('.popover.right>.arrow:after', 'border-right-color:' + theme.centerChannelBg, 1);
changeCss('.popover.left>.arrow:after', 'border-left-color:' + theme.centerChannelBg, 1);
changeCss('.popover.top>.arrow:after', 'border-top-color:' + theme.centerChannelBg, 1);
2015-10-06 17:29:50 +05:00
changeCss('.search-bar__container .search__form .search-bar, .form-control', 'background:' + theme.centerChannelBg, 1);
2015-09-23 10:12:40 -04:00
}
if (theme.centerChannelColor) {
2015-10-05 22:32:26 +05:00
changeCss('.app__content, .post-create__container .post-create-body .btn-file, .post-create__container .post-create-footer .msg-typing, .command-name, .modal .modal-content, .dropdown-menu, .popover, .mentions-name', 'color:' + theme.centerChannelColor, 1);
2015-09-23 10:12:40 -04:00
changeCss('#post-create', 'color:' + theme.centerChannelColor, 2);
2015-10-14 17:04:13 +05:00
changeCss('.channel-header__links a', 'fill:' + changeOpacity(theme.centerChannelColor, 0.7), 1);
changeCss('.channel-header__links a:hover, .channel-header__links a:active', 'fill:' + theme.centerChannelColor, 2);
changeCss('.mentions--top, .command-box', 'box-shadow:' + changeOpacity(theme.centerChannelColor, 0.2) + ' 1px -3px 12px', 3);
changeCss('.mentions--top, .command-box', '-webkit-box-shadow:' + changeOpacity(theme.centerChannelColor, 0.2) + ' 1px -3px 12px', 2);
changeCss('.mentions--top, .command-box', '-moz-box-shadow:' + changeOpacity(theme.centerChannelColor, 0.2) + ' 1px -3px 12px', 1);
changeCss('.dropdown-menu, .popover ', 'box-shadow:' + changeOpacity(theme.centerChannelColor, 0.1) + ' 0px 6px 12px', 3);
changeCss('.dropdown-menu, .popover ', '-webkit-box-shadow:' + changeOpacity(theme.centerChannelColor, 0.1) + ' 0px 6px 12px', 2);
changeCss('.dropdown-menu, .popover ', '-moz-box-shadow:' + changeOpacity(theme.centerChannelColor, 0.1) + ' 0px 6px 12px', 1);
2015-10-05 22:32:26 +05:00
changeCss('.post-body hr, .loading-screen .loading__content .round', 'background:' + theme.centerChannelColor, 1);
2015-09-23 10:12:40 -04:00
changeCss('.channel-header .heading', 'color:' + theme.centerChannelColor, 1);
2015-09-24 18:55:53 +05:00
changeCss('.markdown__table tbody tr:nth-child(2n)', 'background:' + changeOpacity(theme.centerChannelColor, 0.07), 1);
2015-09-23 10:12:40 -04:00
changeCss('.channel-header__info>div.dropdown .header-dropdown__icon', 'color:' + changeOpacity(theme.centerChannelColor, 0.8), 1);
changeCss('.channel-header #member_popover', 'color:' + changeOpacity(theme.centerChannelColor, 0.8), 1);
changeCss('.custom-textarea, .custom-textarea:focus, .preview-container .preview-div, .post-image__column .post-image__details, .sidebar--right .sidebar-right__body, .markdown__table th, .markdown__table td, .command-box, .modal .modal-content, .settings-modal .settings-table .settings-content .divider-light, .dropdown-menu, .modal .modal-header, .popover, .mentions--top .mentions-box', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.2), 1);
2015-10-14 17:04:13 +05:00
changeCss('.popover.bottom>.arrow', 'border-bottom-color:' + changeOpacity(theme.centerChannelColor, 0.25), 1);
changeCss('.popover.right>.arrow', 'border-right-color:' + changeOpacity(theme.centerChannelColor, 0.25), 1);
changeCss('.popover.left>.arrow', 'border-left-color:' + changeOpacity(theme.centerChannelColor, 0.25), 1);
changeCss('.popover.top>.arrow', 'border-top-color:' + changeOpacity(theme.centerChannelColor, 0.25), 1);
changeCss('.command-name, .popover .popover-title', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.2), 1);
changeCss('.dropdown-menu .divider', 'background:' + theme.centerChannelColor, 1);
2015-09-24 18:55:53 +05:00
changeCss('.custom-textarea', 'color:' + theme.centerChannelColor, 1);
changeCss('.post-image__column', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.2), 2);
changeCss('.post-image__column .post-image__details', 'color:' + theme.centerChannelColor, 2);
changeCss('.post-image__column a, .post-image__column a:hover, .post-image__column a:focus', 'color:' + theme.centerChannelColor, 1);
2015-10-06 17:29:50 +05:00
changeCss('.search-bar__container .search__form .search-bar, .form-control', 'color:' + theme.centerChannelColor, 2);
2015-10-05 06:43:08 +05:00
changeCss('@media(max-width: 768px){.search-bar__container .search__form .search-bar', 'background:' + changeOpacity(theme.centerChannelColor, 0.2) + '; color: inherit;', 1);
2015-10-06 22:33:30 +05:00
changeCss('.input-group-addon, .search-bar__container .search__form, .form-control', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.2), 1);
2015-10-06 17:29:50 +05:00
changeCss('.form-control:focus', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.3), 1);
2015-09-23 10:12:40 -04:00
changeCss('.channel-intro .channel-intro__content', 'background:' + changeOpacity(theme.centerChannelColor, 0.05), 1);
2015-09-25 22:44:36 +05:00
changeCss('.date-separator .separator__text', 'color:' + theme.centerChannelColor, 2);
changeCss('.date-separator .separator__hr, .modal-footer, .modal .custom-textarea, .post-right__container .post.post--root hr, .search-item-container', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.2), 1);
changeCss('.modal .custom-textarea:focus', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.3), 1);
changeCss('.channel-intro, .settings-modal .settings-table .settings-content .divider-dark, hr, .settings-modal .settings-table .settings-links', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.2), 1);
2015-10-12 19:22:05 +05:00
changeCss('.post.current--user .post-body, .post.post--comment.other--root.current--user .post-comment, pre', 'background:' + changeOpacity(theme.centerChannelColor, 0.07), 1);
2015-10-19 22:58:47 +05:00
changeCss('.post.current--user .post-body, .post.post--comment.other--root.current--user .post-comment, .post.post--comment.other--root .post-comment, .post.same--root .post-body, .modal .more-table tbody>tr td, .member-div:first-child, .member-div, .access-history__table .access__report, .activity-log__table', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.1), 2);
2015-09-24 18:55:53 +05:00
changeCss('@media(max-width: 1440px){.post.same--root', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.07), 2);
2015-09-23 10:12:40 -04:00
changeCss('@media(max-width: 1440px){.post.same--root', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.07), 2);
changeCss('@media(max-width: 1800px){.inner__wrap.move--left .post.post--comment.same--root', 'border-color:' + changeOpacity(theme.centerChannelColor, 0.07), 2);
2015-10-19 22:58:47 +05:00
changeCss('.post:hover, .modal .more-table tbody>tr:hover td, .sidebar--right .sidebar--right__header, .settings-modal .settings-table .settings-content .section-min:hover', 'background:' + changeOpacity(theme.centerChannelColor, 0.07), 1);
changeCss('.date-separator.hovered--before:after, .date-separator.hovered--after:before, .new-separator.hovered--after:before, .new-separator.hovered--before:after', 'background:' + changeOpacity(theme.centerChannelColor, 0.07), 1);
changeCss('.command-name:hover, .mentions-name:hover, .mentions-focus, .dropdown-menu>li>a:focus, .dropdown-menu>li>a:hover', 'background:' + changeOpacity(theme.centerChannelColor, 0.15), 1);
2015-10-13 14:33:37 +05:00
changeCss('code', 'background:' + changeOpacity(theme.centerChannelColor, 0.1), 1);
2015-09-23 10:12:40 -04:00
changeCss('.post.current--user:hover .post-body ', 'background: none;', 1);
changeCss('.sidebar--right', 'color:' + theme.centerChannelColor, 2);
}
2015-09-25 22:44:36 +05:00
if (theme.newMessageSeparator) {
changeCss('.new-separator .separator__text', 'color:' + theme.newMessageSeparator, 1);
changeCss('.new-separator .separator__hr', 'border-color:' + changeOpacity(theme.newMessageSeparator, 0.5), 1);
}
2015-09-23 10:12:40 -04:00
if (theme.linkColor) {
changeCss('a, a:focus, a:hover, .btn, .btn:focus, .btn:hover', 'color:' + theme.linkColor, 1);
2015-09-23 10:12:40 -04:00
changeCss('.post .comment-icon__container', 'fill:' + theme.linkColor, 1);
}
if (theme.buttonBg) {
changeCss('.btn.btn-primary', 'background:' + theme.buttonBg, 1);
changeCss('.btn.btn-primary:hover, .btn.btn-primary:active, .btn.btn-primary:focus', 'background:' + changeColor(theme.buttonBg, -0.25), 1);
2015-10-19 22:30:00 +02:00
changeCss('.file-playback-controls', 'color:' + changeColor(theme.buttonBg, -0.25), 1);
2015-09-23 10:12:40 -04:00
}
if (theme.buttonColor) {
changeCss('.btn.btn-primary', 'color:' + theme.buttonColor, 2);
}
if (theme.mentionHighlightBg) {
changeCss('.mention-highlight, .search-highlight', 'background:' + theme.mentionHighlightBg, 1);
}
if (theme.mentionHighlightLink) {
changeCss('.mention-highlight .mention-link', 'color:' + theme.mentionHighlightLink, 1);
}
2015-09-23 10:12:40 -04:00
}
export function changeCss(className, classValue, classRepeat) {
2015-06-14 23:53:32 -08:00
// we need invisible container to store additional css definitions
var cssMainContainer = $('#css-modifier-container');
2015-08-18 09:43:22 -04:00
if (cssMainContainer.length === 0) {
2015-08-20 08:41:45 -04:00
cssMainContainer = $('<div id="css-modifier-container"></div>');
cssMainContainer.hide();
cssMainContainer.appendTo($('body'));
2015-06-14 23:53:32 -08:00
}
// and we need one div for each class
2015-09-23 10:12:40 -04:00
var classContainer = cssMainContainer.find('div[data-class="' + className + classRepeat + '"]');
2015-08-18 09:43:22 -04:00
if (classContainer.length === 0) {
2015-09-23 10:12:40 -04:00
classContainer = $('<div data-class="' + className + classRepeat + '"></div>');
2015-06-14 23:53:32 -08:00
classContainer.appendTo(cssMainContainer);
}
// append additional style
classContainer.html('<style>' + className + ' {' + classValue + '}</style>');
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function rgb2hex(rgbIn) {
2015-08-18 09:43:22 -04:00
if (/^#[0-9A-F]{6}$/i.test(rgbIn)) {
return rgbIn;
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
var rgb = rgbIn.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
2015-06-14 23:53:32 -08:00
function hex(x) {
2015-08-18 09:43:22 -04:00
return ('0' + parseInt(x, 10).toString(16)).slice(-2);
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
return '#' + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function placeCaretAtEnd(el) {
2015-06-14 23:53:32 -08:00
el.focus();
2015-08-18 09:43:22 -04:00
if (typeof window.getSelection != 'undefined' && typeof document.createRange != 'undefined') {
2015-06-14 23:53:32 -08:00
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
2015-08-18 09:43:22 -04:00
} else if (typeof document.body.createTextRange != 'undefined') {
2015-06-14 23:53:32 -08:00
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.collapse(false);
textRange.select();
}
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getCaretPosition(el) {
2015-06-14 23:53:32 -08:00
if (el.selectionStart) {
2015-08-18 09:43:22 -04:00
return el.selectionStart;
2015-06-14 23:53:32 -08:00
} else if (document.selection) {
2015-08-18 09:43:22 -04:00
el.focus();
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
var r = document.selection.createRange();
if (r == null) {
return 0;
}
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
var re = el.createTextRange();
var rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
return rc.text.length;
2015-06-14 23:53:32 -08:00
}
return 0;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function setSelectionRange(input, selectionStart, selectionEnd) {
2015-08-18 09:43:22 -04:00
if (input.setSelectionRange) {
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
} else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function setCaretPosition(input, pos) {
setSelectionRange(input, pos, pos);
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getSelectedText(input) {
2015-08-18 09:43:22 -04:00
var selectedText;
if (typeof document.selection !== 'undefined') {
input.focus();
var sel = document.selection.createRange();
selectedText = sel.text;
} else if (typeof input.selectionStart !== 'undefined') {
var startPos = input.selectionStart;
var endPos = input.selectionEnd;
selectedText = input.value.substring(startPos, endPos);
2015-06-14 23:53:32 -08:00
}
2015-08-18 09:43:22 -04:00
return selectedText;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function isValidUsername(name) {
2015-08-18 09:43:22 -04:00
var error = '';
if (!name) {
error = 'This field is required';
} else if (name.length < 3 || name.length > 15) {
error = 'Must be between 3 and 15 characters';
} else if (!(/^[a-z0-9\.\-\_]+$/).test(name)) {
error = "Must contain only letters, numbers, and the symbols '.', '-', and '_'.";
} else if (!(/[a-z]/).test(name.charAt(0))) { //eslint-disable-line no-negated-condition
2015-08-18 09:43:22 -04:00
error = 'First character must be a letter.';
} else {
for (var i = 0; i < Constants.RESERVED_USERNAMES.length; i++) {
2015-09-14 16:10:31 +01:00
if (name === Constants.RESERVED_USERNAMES[i]) {
2015-08-18 09:43:22 -04:00
error = 'Cannot use a reserved word as a username.';
2015-06-14 23:53:32 -08:00
break;
}
}
}
return error;
2015-09-02 10:42:26 -04:00
}
2015-08-18 09:43:22 -04:00
2015-09-02 10:42:26 -04:00
export function updateAddressBar(channelName) {
2015-08-18 09:43:22 -04:00
var teamURL = window.location.href.split('/channels')[0];
history.replaceState('data', '', teamURL + '/channels/' + channelName);
2015-06-14 23:53:32 -08:00
}
2015-09-28 16:31:18 -07:00
export function switchChannel(channel) {
2015-06-14 23:53:32 -08:00
AppDispatcher.handleViewAction({
2015-08-18 09:43:22 -04:00
type: ActionTypes.CLICK_CHANNEL,
name: channel.name,
id: channel.id
2015-06-14 23:53:32 -08:00
});
updateAddressBar(channel.name);
2015-06-14 23:53:32 -08:00
AsyncClient.getChannels(true, true, true);
AsyncClient.getChannelExtraInfo(true);
AsyncClient.getPosts(channel.id);
2015-06-14 23:53:32 -08:00
$('.inner__wrap').removeClass('move--right');
$('.sidebar--left').removeClass('move--right');
client.trackPage();
return false;
}
2015-09-02 10:42:26 -04:00
export function isMobile() {
2015-08-18 09:43:22 -04:00
return screen.width <= 768;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function isComment(post) {
2015-06-14 23:53:32 -08:00
if ('root_id' in post) {
2015-08-18 09:43:22 -04:00
return post.root_id !== '';
2015-06-14 23:53:32 -08:00
}
return false;
2015-09-02 10:42:26 -04:00
}
2015-06-14 23:53:32 -08:00
2015-09-02 10:42:26 -04:00
export function getDirectTeammate(channelId) {
2015-08-18 09:43:22 -04:00
var userIds = ChannelStore.get(channelId).name.split('__');
var curUserId = UserStore.getCurrentId();
var teammate = {};
2015-07-01 12:41:36 -07:00
2015-08-18 09:43:22 -04:00
if (userIds.length !== 2 || userIds.indexOf(curUserId) === -1) {
return teammate;
}
2015-07-01 12:41:36 -07:00
2015-07-02 09:34:57 -04:00
for (var idx in userIds) {
2015-08-18 09:43:22 -04:00
if (userIds[idx] !== curUserId) {
teammate = UserStore.getProfile(userIds[idx]);
break;
}
}
2015-07-01 12:41:36 -07:00
return teammate;
2015-09-02 10:42:26 -04:00
}
2015-07-01 12:41:36 -07:00
2015-09-02 10:42:26 -04:00
Image.prototype.load = function imageLoad(url, progressCallback) {
2015-08-18 09:43:22 -04:00
var self = this;
2015-06-14 23:53:32 -08:00
var xmlHTTP = new XMLHttpRequest();
xmlHTTP.open('GET', url, true);
xmlHTTP.responseType = 'arraybuffer';
2015-08-18 09:43:22 -04:00
xmlHTTP.onload = function onLoad() {
var h = xmlHTTP.getAllResponseHeaders();
var m = h.match(/^Content-Type\:\s*(.*?)$/mi);
var mimeType = m[1] || 'image/png';
2015-06-14 23:53:32 -08:00
2015-08-18 09:43:22 -04:00
var blob = new Blob([this.response], {type: mimeType});
self.src = window.URL.createObjectURL(blob);
2015-06-14 23:53:32 -08:00
};
2015-08-18 09:43:22 -04:00
xmlHTTP.onprogress = function onprogress(e) {
parseInt(self.completedPercentage = (e.loaded / e.total) * 100, 10);
if (progressCallback) {
progressCallback();
}
2015-06-14 23:53:32 -08:00
};
2015-08-18 09:43:22 -04:00
xmlHTTP.onloadstart = function onloadstart() {
self.completedPercentage = 0;
2015-06-14 23:53:32 -08:00
};
xmlHTTP.send();
};
Image.prototype.completedPercentage = 0;
2015-09-02 10:42:26 -04:00
export function changeColor(colourIn, amt) {
2015-09-23 10:12:40 -04:00
var hex = colourIn;
var lum = amt;
2015-07-11 08:32:02 -07:00
2015-09-23 10:12:40 -04:00
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
2015-07-11 08:32:02 -07:00
}
2015-09-23 10:12:40 -04:00
lum = lum || 0;
2015-07-11 08:32:02 -07:00
2015-09-23 10:12:40 -04:00
// convert to decimal and change luminosity
var rgb = '#';
var c;
var i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i * 2, 2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ('00' + c).substr(c.length);
2015-08-18 09:43:22 -04:00
}
2015-07-11 08:32:02 -07:00
2015-09-23 10:12:40 -04:00
return rgb;
}
2015-08-18 09:43:22 -04:00
2015-09-23 10:12:40 -04:00
export function changeOpacity(oldColor, opacity) {
var color = oldColor;
if (color[0] === '#') {
color = color.slice(1);
2015-08-18 09:43:22 -04:00
}
2015-07-11 08:32:02 -07:00
2015-09-23 10:12:40 -04:00
if (color.length === 3) {
const tempColor = color;
color = '';
2015-08-18 11:58:15 +05:00
2015-09-23 10:12:40 -04:00
color += tempColor[0] + tempColor[0];
color += tempColor[1] + tempColor[1];
color += tempColor[2] + tempColor[2];
2015-08-18 11:58:15 +05:00
}
2015-09-23 10:12:40 -04:00
var r = parseInt(color.substring(0, 2), 16);
var g = parseInt(color.substring(2, 4), 16);
var b = parseInt(color.substring(4, 6), 16);
2015-08-18 11:58:15 +05:00
return 'rgba(' + r + ',' + g + ',' + b + ',' + opacity + ')';
2015-09-02 10:42:26 -04:00
}
2015-08-18 11:58:15 +05:00
2015-09-02 10:42:26 -04:00
export function getFullName(user) {
if (user.first_name && user.last_name) {
2015-08-18 09:43:22 -04:00
return user.first_name + ' ' + user.last_name;
} else if (user.first_name) {
return user.first_name;
} else if (user.last_name) {
return user.last_name;
}
2015-08-18 09:43:22 -04:00
return '';
2015-09-02 10:42:26 -04:00
}
2015-09-02 10:42:26 -04:00
export function getDisplayName(user) {
if (user.nickname && user.nickname.trim().length > 0) {
return user.nickname;
2015-08-18 09:43:22 -04:00
}
2015-09-02 10:42:26 -04:00
var fullName = getFullName(user);
2015-08-18 09:43:22 -04:00
if (fullName) {
return fullName;
}
2015-08-18 09:43:22 -04:00
return user.username;
2015-09-02 10:42:26 -04:00
}
//IE10 does not set window.location.origin automatically so this must be called instead when using it
2015-09-02 10:42:26 -04:00
export function getWindowLocationOrigin() {
var windowLocationOrigin = window.location.origin;
if (!windowLocationOrigin) {
2015-08-18 09:43:22 -04:00
windowLocationOrigin = window.location.protocol + '//' + window.location.hostname;
if (window.location.port) {
windowLocationOrigin += ':' + window.location.port;
}
}
return windowLocationOrigin;
2015-09-02 10:42:26 -04:00
}
2015-08-18 09:43:22 -04:00
// Converts a file size in bytes into a human-readable string of the form '123MB'.
2015-09-02 10:42:26 -04:00
export function fileSizeToString(bytes) {
// it's unlikely that we'll have files bigger than this
if (bytes > 1024 * 1024 * 1024 * 1024) {
2015-08-18 09:43:22 -04:00
return Math.floor(bytes / (1024 * 1024 * 1024 * 1024)) + 'TB';
} else if (bytes > 1024 * 1024 * 1024) {
2015-08-18 09:43:22 -04:00
return Math.floor(bytes / (1024 * 1024 * 1024)) + 'GB';
} else if (bytes > 1024 * 1024) {
2015-08-18 09:43:22 -04:00
return Math.floor(bytes / (1024 * 1024)) + 'MB';
} else if (bytes > 1024) {
2015-08-18 09:43:22 -04:00
return Math.floor(bytes / 1024) + 'KB';
}
2015-08-18 09:43:22 -04:00
return bytes + 'B';
2015-09-02 10:42:26 -04:00
}
// Converts a filename (like those attached to Post objects) to a url that can be used to retrieve attachments from the server.
2015-09-02 10:42:26 -04:00
export function getFileUrl(filename) {
var url = filename;
// This is a temporary patch to fix issue with old files using absolute paths
2015-08-18 09:43:22 -04:00
if (url.indexOf('/api/v1/files/get') !== -1) {
url = filename.split('/api/v1/files/get')[1];
}
2015-09-02 10:42:26 -04:00
url = getWindowLocationOrigin() + '/api/v1/files/get' + url;
return url;
2015-09-02 10:42:26 -04:00
}
// Gets the name of a file (including extension) from a given url or file path.
2015-09-02 10:42:26 -04:00
export function getFileName(path) {
var split = path.split('/');
return split[split.length - 1];
2015-09-02 10:42:26 -04:00
}
// Generates a RFC-4122 version 4 compliant globally unique identifier.
2015-09-02 10:42:26 -04:00
export function generateId() {
// implementation taken from http://stackoverflow.com/a/2117523
var id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
2015-08-18 09:43:22 -04:00
id = id.replace(/[xy]/g, function replaceRandom(c) {
var r = Math.floor(Math.random() * 16);
var v;
if (c === 'x') {
v = r;
} else {
v = r & 0x3 | 0x8;
}
return v.toString(16);
});
return id;
2015-09-02 10:42:26 -04:00
}
2015-09-02 10:42:26 -04:00
export function isBrowserFirefox() {
2015-08-17 08:10:49 -07:00
return navigator && navigator.userAgent && navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
2015-09-02 10:42:26 -04:00
}
// Checks if browser is IE10 or IE11
2015-09-02 10:42:26 -04:00
export function isBrowserIE() {
if (window.navigator && window.navigator.userAgent) {
var ua = window.navigator.userAgent;
return ua.indexOf('Trident/7.0') > 0 || ua.indexOf('Trident/6.0') > 0;
}
return false;
2015-09-02 10:42:26 -04:00
}
2015-09-02 10:42:26 -04:00
export function isBrowserEdge() {
return window.naviagtor && navigator.userAgent && navigator.userAgent.toLowerCase().indexOf('edge') > -1;
2015-09-02 10:42:26 -04:00
}
export function getDirectChannelName(id, otherId) {
let handle;
if (otherId > id) {
handle = id + '__' + otherId;
} else {
handle = otherId + '__' + id;
}
return handle;
}
// Used to get the id of the other user from a DM channel
2015-09-02 10:42:26 -04:00
export function getUserIdFromChannelName(channel) {
var ids = channel.name.split('__');
var otherUserId = '';
if (ids[0] === UserStore.getCurrentId()) {
otherUserId = ids[1];
} else {
otherUserId = ids[0];
}
return otherUserId;
2015-09-02 10:42:26 -04:00
}
2015-07-07 09:16:13 -04:00
2015-09-02 10:42:26 -04:00
export function importSlack(file, success, error) {
2015-08-18 09:43:22 -04:00
var formData = new FormData();
formData.append('file', file, file.name);
formData.append('filesize', file.size);
2015-07-07 09:16:13 -04:00
formData.append('importFrom', 'slack');
2015-08-18 09:43:22 -04:00
client.importSlack(formData, success, error);
2015-09-02 10:42:26 -04:00
}
export function getTeamURLFromAddressBar() {
return window.location.href.split('/channels')[0];
}
export function getShortenedTeamURL() {
const teamURL = getTeamURLFromAddressBar();
2015-09-22 11:21:28 +05:00
if (teamURL.length > 35) {
return teamURL.substring(0, 10) + '...' + teamURL.substring(teamURL.length - 12, teamURL.length) + '/';
}
2015-09-22 11:21:28 +05:00
return teamURL + '/';
}
export function windowWidth() {
return $(window).width();
}
export function windowHeight() {
return $(window).height();
}