Skip to content
Snippets Groups Projects
channelSettings.coffee 6.76 KiB
Newer Older
Rodrigo Nascimento's avatar
Rodrigo Nascimento committed
import toastr from 'toastr'
Template.channelSettings.helpers
	toArray: (obj) ->
		arr = []
		for key, value of obj
			arr.push
				$key: key
				$value: value
		return arr

	valueOf: (obj, key) ->
		return obj?[key]

Karl Prieb's avatar
Karl Prieb committed
	isTextInput: (value) ->
		return value == 'text' or value == 'markdown'

	showSetting: (setting, room) ->
		if setting.showInDirect is false
			return room.t isnt 'd'
		return true

	settings: ->
		return Template.instance().settings

	getRoom: ->
		return ChatRoom.findOne(@rid)

Marcelo Schmidt's avatar
Marcelo Schmidt committed
	editing: (field) ->
		return Template.instance().editing.get() is field
Marcelo Schmidt's avatar
Marcelo Schmidt committed
	channelSettings: ->
		return RocketChat.ChannelSettings.getOptions()

	unscape: (value) ->
		return s.unescapeHTML value

	canDeleteRoom: ->
		roomType = ChatRoom.findOne(@rid, { fields: { t: 1 }})?.t
		return roomType? and RocketChat.authz.hasAtLeastOnePermission("delete-#{roomType}", @rid)
	readOnly: ->
		return  ChatRoom.findOne(@rid, { fields: { ro: 1 }})?.ro
	readOnlyDescription: ->
		readOnly = ChatRoom.findOne(@rid, { fields: { ro: 1 }})?.ro
		if readOnly is true
			return t('True')
		else
			return t('False')
Marcelo Schmidt's avatar
Marcelo Schmidt committed
Template.channelSettings.events
	'click .delete': ->
		swal {
			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
		}, =>
			swal.disableButtons()

			Meteor.call 'eraseRoom', @rid, (error, result) ->
				if error
					handleError(error)
					swal.enableButtons()
				else
					swal
						title: t('Deleted')
						text: t('Room_has_been_deleted')
						type: 'success'
						timer: 2000
						showConfirmButton: false

	'keydown input[type=text]': (e, t) ->
		if e.keyCode is 13
			e.preventDefault()
			t.saveSetting()
Marcelo Schmidt's avatar
Marcelo Schmidt committed

	'click [data-edit]': (e, t) ->
		e.preventDefault()
		t.editing.set($(e.currentTarget).data('edit'))
		setTimeout (-> t.$('input.editing').focus().select()), 100

Karl Prieb's avatar
Karl Prieb committed
	'change [type="radio"]': (e, t) ->
		t.editing.set($(e.currentTarget).attr('name'))

	'change [type="checkbox"]': (e, t) ->
		t.editing.set($(e.currentTarget).attr('name'))
		t.saveSetting()

	'click .cancel': (e, t) ->
		e.preventDefault()
		t.editing.set()
Marcelo Schmidt's avatar
Marcelo Schmidt committed
	'click .save': (e, t) ->
		e.preventDefault()
		t.saveSetting()
Template.channelSettings.onCreated ->
	@editing = new ReactiveVar

	@settings =
		name:
			type: 'text'
			label: 'Name'
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				if not RocketChat.authz.hasAllPermission('edit-room', room._id) or room.t not in ['c', 'p']
					return toastr.error t('error-not-allowed')

				try
					nameValidation = new RegExp '^' + RocketChat.settings.get('UTF8_Names_Validation') + '$'
				catch
					nameValidation = new RegExp '^[0-9a-zA-Z-_.]+$'

				if not nameValidation.test value
					return toastr.error t('error-invalid-room-name', { room_name: name: value })

Maki Nishifuji's avatar
Maki Nishifuji committed
				RocketChat.callbacks.run 'roomNameChanged', { _id: room._id, name: value }
				Meteor.call 'saveRoomSettings', room._id, 'roomName', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_name_changed_successfully'

		topic:
			type: 'markdown'
			label: 'Topic'
			canView: (room) => true
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				Meteor.call 'saveRoomSettings', room._id, 'roomTopic', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_topic_changed_successfully'
					RocketChat.callbacks.run 'roomTopicChanged', room

		description:
			type: 'text'
			label: 'Description'
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				Meteor.call 'saveRoomSettings', room._id, 'roomDescription', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_description_changed_successfully'

		t:
			type: 'select'
			label: 'Type'
			options:
				c: 'Channel'
				p: 'Private_Group'
			canView: (room) ->
				if not room.t in ['c', 'p']
					return false
				else if room.t is 'p' and not RocketChat.authz.hasAllPermission('create-c')
					return false
				else if room.t is 'c' and not RocketChat.authz.hasAllPermission('create-p')
					return false
				return true
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				if value not in ['c', 'p']
					return toastr.error t('error-invalid-room-type', value)

				RocketChat.callbacks.run 'roomTypeChanged', room
				Meteor.call 'saveRoomSettings', room._id, 'roomType', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_type_changed_successfully'

		ro:
			type: 'boolean'
			label: 'Read_only'
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAllPermission('set-readonly', room._id)
			save: (value, room) ->
				Meteor.call 'saveRoomSettings', room._id, 'readOnly', value, (err, result) ->
					return handleError err if err
Maki Nishifuji's avatar
Maki Nishifuji committed
					toastr.success TAPi18n.__ 'Read_only_changed_successfully'
		archived:
			type: 'boolean'
			label: 'Room_archivation_state_true'
			canView: (room) => room.t isnt 'd'
			canEdit: (room) => RocketChat.authz.hasAtLeastOnePermission(['archive-room', 'unarchive-room'], room._id)
			save: (value, room) ->
				if value is true
					Meteor.call 'archiveRoom', room._id, (err, results) ->
						return handleError err if err
						toastr.success TAPi18n.__ 'Room_archived'
						RocketChat.callbacks.run 'archiveRoom', room
				else
					Meteor.call 'unarchiveRoom', room._id, (err, results) ->
						return handleError err if err
						toastr.success TAPi18n.__ 'Room_unarchived'
						RocketChat.callbacks.run 'unarchiveRoom', room
		joinCode:
			type: 'text'
			label: 'Code'
			canView: (room) => room.t is 'c' and RocketChat.authz.hasAllPermission('edit-room', room._id)
			canEdit: (room) => RocketChat.authz.hasAllPermission('edit-room', room._id)
			save: (value, room) ->
				Meteor.call 'saveRoomSettings', room._id, 'joinCode', value, (err, result) ->
					return handleError err if err
					toastr.success TAPi18n.__ 'Room_code_changed_successfully'
					RocketChat.callbacks.run 'roomCodeChanged', room
	@saveSetting = =>
		room = ChatRoom.findOne @data?.rid
		field = @editing.get()
Karl Prieb's avatar
Karl Prieb committed
		console.log('FIELD', field)
		if @settings[field].type is 'select'
			value = @$(".channel-settings form [name=#{field}]:checked").val()
		else if @settings[field].type is 'boolean'
Karl Prieb's avatar
Karl Prieb committed
			value = @$(".channel-settings form [name=#{field}]:checked").val() is 'on'
		else
			value = @$(".channel-settings form [name=#{field}]").val()
		if value isnt room[field]
			@settings[field].save(value, room)
		@editing.set()