fixes #9688 and harmonizes actions from channelSettings and the channel popover

Introduces a re-use-library for the channel related actions.
pull/9697/head
Oliver Jägle 8 years ago
parent 7213440400
commit 6dc96f30e0
  1. 109
      packages/rocketchat-channel-settings/client/views/channelSettings.js
  2. 93
      packages/rocketchat-lib/client/lib/ChannelActions.js
  3. 7
      packages/rocketchat-lib/client/lib/index.js
  4. 78
      packages/rocketchat-ui/client/views/app/popover.js

@ -1,6 +1,7 @@
import toastr from 'toastr'; import toastr from 'toastr';
import s from 'underscore.string'; import s from 'underscore.string';
import { RocketChat, RoomSettingsEnum } from 'meteor/rocketchat:lib'; import { RocketChat, RoomSettingsEnum } from 'meteor/rocketchat:lib';
import { hide, leave, erase } from 'meteor/rocketchat:lib';
const common = { const common = {
canLeaveRoom() { canLeaveRoom() {
const { cl: canLeave, t: roomType } = Template.instance().room; const { cl: canLeave, t: roomType } = Template.instance().room;
@ -27,7 +28,7 @@ const common = {
}; };
const call = (method, ...params) => { const call = (method, ...params) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
Meteor.call(method, ...params, (err, result)=> { Meteor.call(method, ...params, (err, result) => {
if (err) { if (err) {
handleError(err); handleError(err);
return reject(err); return reject(err);
@ -46,11 +47,11 @@ Template.channelSettingsEditing.events({
this.value.set(e.currentTarget.checked); this.value.set(e.currentTarget.checked);
}, },
'click .js-reset'(e, t) { 'click .js-reset'(e, t) {
const {settings} = t; const { settings } = t;
Object.keys(settings).forEach(key => settings[key].value.set(settings[key].default.get())); Object.keys(settings).forEach(key => settings[key].value.set(settings[key].default.get()));
}, },
async 'click .js-save'(e, t) { async 'click .js-save'(e, t) {
const {settings} = t; const { settings } = t;
Object.keys(settings).forEach(async name => { Object.keys(settings).forEach(async name => {
const setting = settings[name]; const setting = settings[name];
const value = setting.value.get(); const value = setting.value.get();
@ -201,7 +202,7 @@ Template.channelSettingsEditing.onCreated(function() {
}; };
if (room['default']) { if (room['default']) {
if (RocketChat.authz.hasRole(Meteor.userId(), 'admin')) { if (RocketChat.authz.hasRole(Meteor.userId(), 'admin')) {
return new Promise((resolve, reject)=> { return new Promise((resolve, reject) => {
modal.open({ modal.open({
title: t('Room_default_change_to_private_will_be_default_no_more'), title: t('Room_default_change_to_private_will_be_default_no_more'),
type: 'warning', type: 'warning',
@ -269,7 +270,7 @@ Template.channelSettingsEditing.onCreated(function() {
return RocketChat.authz.hasAtLeastOnePermission(['archive-room', 'unarchive-room'], room._id); return RocketChat.authz.hasAtLeastOnePermission(['archive-room', 'unarchive-room'], room._id);
}, },
save(value) { save(value) {
return new Promise((resolve, reject)=>{ return new Promise((resolve, reject) => {
modal.open({ modal.open({
title: t('Are_you_sure'), title: t('Are_you_sure'),
type: 'warning', type: 'warning',
@ -347,7 +348,7 @@ Template.channelSettingsEditing.onCreated(function() {
}; };
Object.keys(this.settings).forEach(key => { Object.keys(this.settings).forEach(key => {
const setting = this.settings[key]; const setting = this.settings[key];
const def =setting.getValue ? setting.getValue(this.room): this.room[key]; const def = setting.getValue ? setting.getValue(this.room) : this.room[key];
setting.default = new ReactiveVar(def || false); setting.default = new ReactiveVar(def || false);
setting.value = new ReactiveVar(def || false); setting.value = new ReactiveVar(def || false);
}); });
@ -368,7 +369,7 @@ Template.channelSettingsEditing.helpers({
return this.value.get();// ? '' : 'checked'; return this.value.get();// ? '' : 'checked';
}, },
modified(text = '') { modified(text = '') {
const {settings} = Template.instance(); const { settings } = Template.instance();
return !Object.keys(settings).some(key => settings[key].default.get() !== settings[key].value.get()) ? text : ''; return !Object.keys(settings).some(key => settings[key].default.get() !== settings[key].value.get()) ? text : '';
}, },
equal(text = '', text2 = '', ret = '*') { equal(text = '', text2 = '', ret = '*') {
@ -385,7 +386,7 @@ Template.channelSettingsEditing.helpers({
return 'hashtag'; return 'hashtag';
case 'l': case 'l':
return 'livechat'; return 'livechat';
default : default:
return null; return null;
} }
}, },
@ -420,96 +421,20 @@ Template.channelSettings.events({
t.editing.set(true); t.editing.set(true);
}, },
'click .js-leave'(e, instance) { 'click .js-leave'(e, instance) {
let warnText; const { name, t: type } = instance.room;
const { rid, name, t : type } = instance.room; const rid = instance.room._id;
switch (type) { leave(type, rid, name);
case 'c': warnText = 'Leave_Room_Warning'; break;
case 'p': warnText = 'Leave_Group_Warning'; break;
case 'd': warnText = 'Leave_Private_Warning'; break;
case 'l': warnText = 'Leave_Livechat_Warning'; break;
}
modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_leave_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: true,
html: false
}, async function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}
await call('leaveRoom', rid);
if (rid === Session.get('openedRoom')) {
Session.delete('openedRoom');
}
});
}, },
'click .js-hide'(e, instance) { 'click .js-hide'(e, instance) {
let warnText; const { name, t: type } = instance.room;
const { rid, name, t: type } = instance.room; const rid = instance.room._id;
switch (type) { hide(type, rid, name);
case 'c': warnText = 'Hide_Room_Warning'; break;
case 'p': warnText = 'Hide_Group_Warning'; break;
case 'd': warnText = 'Hide_Private_Warning'; break;
case 'l': warnText = 'Hide_Livechat_Warning'; break;
}
modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_hide_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: true,
html: false
}, async function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}
await call('hideRoom', rid);
if (rid === Session.get('openedRoom')) {
Session.delete('openedRoom');
}
});
}, },
'click .js-cancel'(e, t) { 'click .js-cancel'(e, t) {
t.editing.set(false); t.editing.set(false);
}, },
'click .js-delete'() { 'click .js-delete'() {
return modal.open({ return erase(this.rid);
title: t('Are_you_sure'),
text: t('Delete_Room_Warning'),
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_delete_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: false,
html: false
}, () => {
call('eraseRoom', this.rid).then(() => {
modal.open({
title: t('Deleted'),
text: t('Room_has_been_deleted'),
type: 'success',
timer: 2000,
showConfirmButton: false
});
});
});
} }
}); });
@ -558,7 +483,7 @@ Template.channelSettingsInfo.helpers({
return 'hashtag'; return 'hashtag';
case 'l': case 'l':
return 'livechat'; return 'livechat';
default : default:
return null; return null;
} }
} }

@ -0,0 +1,93 @@
import { UiTextContext } from 'meteor/rocketchat:lib';
export function hide(type, rid, name) {
const warnText = RocketChat.roomTypes.roomTypes[type].getUiText(UiTextContext.HIDE_WARNING);
modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_hide_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: true,
html: false
}, function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}
Meteor.call('hideRoom', rid, function(err) {
if (err) {
handleError(err);
} else if (rid === Session.get('openedRoom')) {
Session.delete('openedRoom');
}
});
});
return false;
}
export function leave(type, rid, name) {
const warnText = RocketChat.roomTypes.roomTypes[type].getUiText(UiTextContext.LEAVE_WARNING);
modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_leave_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: false,
html: false
}, function(isConfirm) {
if (isConfirm) {
Meteor.call('leaveRoom', rid, function(err) {
if (err) {
modal.open({
title: t('Warning'),
text: handleError(err, false),
type: 'warning',
html: false
});
} else {
modal.close();
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}
RoomManager.close(rid);
}
});
}
});
return false;
}
export function erase(rid) {
modal.open({
title: t('Are_you_sure'),
text: t('Delete_Room_Warning'),
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_delete_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: false,
html: false
}, () => {
call('eraseRoom', rid).then(() => {
modal.open({
title: t('Deleted'),
text: t('Room_has_been_deleted'),
type: 'success',
timer: 2000,
showConfirmButton: false
});
});
});
}

@ -8,12 +8,15 @@
import { RocketChatTabBar } from './RocketChatTabBar'; import { RocketChatTabBar } from './RocketChatTabBar';
import { RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext } from '../../lib/RoomTypeConfig'; import { RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext } from '../../lib/RoomTypeConfig';
import {hide, leave, erase} from './ChannelActions';
export { export {
RocketChatTabBar, RocketChatTabBar,
RoomSettingsEnum, RoomSettingsEnum,
RoomTypeConfig, RoomTypeConfig,
RoomTypeRouteConfig, RoomTypeRouteConfig,
UiTextContext UiTextContext,
hide,
leave,
erase
}; };

@ -1,7 +1,7 @@
/* globals popover isRtl */ /* globals popover isRtl */
import _ from 'underscore'; import _ from 'underscore';
import {UiTextContext} from 'meteor/rocketchat:lib'; import { hide, leave } from 'meteor/rocketchat:lib';
this.popover = { this.popover = {
renderedPopover: null, renderedPopover: null,
@ -41,7 +41,7 @@ Template.popover.onRendered(function() {
const activeElement = this.data.activeElement; const activeElement = this.data.activeElement;
const popoverContent = this.firstNode.children[0]; const popoverContent = this.firstNode.children[0];
const position = _.throttle(() => { const position = _.throttle(() => {
const position = typeof this.data.position === 'function'? this.data.position() : this.data.position; const position = typeof this.data.position === 'function' ? this.data.position() : this.data.position;
const customCSSProperties = typeof this.data.customCSSProperties === 'function' ? this.data.customCSSProperties() : this.data.customCSSProperties; const customCSSProperties = typeof this.data.customCSSProperties === 'function' ? this.data.customCSSProperties() : this.data.customCSSProperties;
const mousePosition = typeof this.data.mousePosition === 'function' ? this.data.mousePosition() : this.data.mousePosition; const mousePosition = typeof this.data.mousePosition === 'function' ? this.data.mousePosition() : this.data.mousePosition;
if (position) { if (position) {
@ -114,7 +114,7 @@ Template.popover.events({
const id = event.currentTarget.dataset.id; const id = event.currentTarget.dataset.id;
const action = RocketChat.messageBox.actions.getById(id); const action = RocketChat.messageBox.actions.getById(id);
if ((action[0] != null ? action[0].action : undefined) != null) { if ((action[0] != null ? action[0].action : undefined) != null) {
action[0].action({rid: t.data.data.rid, messageBox: document.querySelector('.rc-message-box'), element: event.currentTarget, event}); action[0].action({ rid: t.data.data.rid, messageBox: document.querySelector('.rc-message-box'), element: event.currentTarget, event });
if (id !== 'audio-message') { if (id !== 'audio-message') {
popover.close(); popover.close();
} }
@ -210,77 +210,11 @@ Template.popover.events({
const action = e.currentTarget.dataset.id; const action = e.currentTarget.dataset.id;
if (action === 'hide') { if (action === 'hide') {
const warnText = RocketChat.roomTypes.roomTypes[template].getUiText(UiTextContext.HIDE_WARNING); hide(template, rid, name);
modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_hide_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: true,
html: false
}, function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}
Meteor.call('hideRoom', rid, function(err) {
if (err) {
handleError(err);
} else if (rid === Session.get('openedRoom')) {
Session.delete('openedRoom');
}
});
});
return false;
} }
if (action === 'leave') { if (action === 'leave') {
let warnText; leave(template, rid, name);
switch (template) {
case 'c': warnText = 'Leave_Room_Warning'; break;
case 'p': warnText = 'Leave_Group_Warning'; break;
case 'd': warnText = 'Leave_Private_Warning'; break;
case 'l': warnText = 'Hide_Livechat_Warning'; break;
}
modal.open({
title: t('Are_you_sure'),
text: warnText ? t(warnText, name) : '',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: t('Yes_leave_it'),
cancelButtonText: t('Cancel'),
closeOnConfirm: false,
html: false
}, function(isConfirm) {
if (isConfirm) {
Meteor.call('leaveRoom', rid, function(err) {
if (err) {
modal.open({
title: t('Warning'),
text: handleError(err, false),
type: 'warning',
html: false
});
} else {
modal.close();
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName()) && (Session.get('openedRoom') === rid)) {
FlowRouter.go('home');
}
RoomManager.close(rid);
}
});
}
});
return false;
} }
if (action === 'read') { if (action === 'read') {
@ -294,7 +228,7 @@ Template.popover.events({
return handleError(error); return handleError(error);
} }
const subscription = ChatSubscription.findOne({rid}); const subscription = ChatSubscription.findOne({ rid });
if (subscription == null) { if (subscription == null) {
return; return;
} }

Loading…
Cancel
Save