Skip to content
Snippets Groups Projects
channelSettings.coffee 1.77 KiB
Newer Older
Template.channelSettings.helpers
Marcelo Schmidt's avatar
Marcelo Schmidt committed
	canEdit: ->
		return true
	editing: (field) ->
		return false
	notDirect: ->
Marcelo Schmidt's avatar
Marcelo Schmidt committed
		return ChatRoom.findOne(@rid)?.t isnt 'd'
	roomType: ->
Marcelo Schmidt's avatar
Marcelo Schmidt committed
		return ChatRoom.findOne(@rid)?.t
Marcelo Schmidt's avatar
Marcelo Schmidt committed
	roomTypeDescription: ->
		return if ChatRoom.findOne(@rid)?.t is 'c' then t('Channel') else t('Private_Group')
	roomName: ->
		return ChatRoom.findOne(@rid)?.name
	roomTopic: ->
		return ChatRoom.findOne(@rid)?.topic
Marcelo Schmidt's avatar
Marcelo Schmidt committed
Template.channelSettings.events
	'click .save': (e, t) ->
		e.preventDefault()

		settings =
Marcelo Schmidt's avatar
Marcelo Schmidt committed
			roomType: t.$('input[name=roomType]:checked').val()
			roomName: t.$('input[name=roomName]').val()
			roomTopic: t.$('input[name=roomTopic]').val()
Marcelo Schmidt's avatar
Marcelo Schmidt committed

		if t.validate()
			Meteor.call 'saveRoomSettings', t.data.rid, settings, (err, results) ->
				if err
					if err.error in [ 'duplicate-name', 'name-invalid' ]
						return toastr.error TAPi18n.__(err.reason, err.details.channelName)
					if err.error is 'invalid-room-type'
						return toastr.error TAPi18n.__(err.reason, err.details.roomType)
					return toastr.error TAPi18n.__(err.reason)
				toastr.success TAPi18n.__ 'Settings_updated'
Template.channelSettings.onCreated ->
	@validateRoomType = =>
		type = @$('input[name=roomType]:checked').val()
		if type not in ['c', 'p']
			toastr.error t('Invalid_room_type', type)
		return true

	@validateRoomName = =>
		rid = Template.currentData()?.rid
		room = ChatRoom.findOne rid

		if room.u._id isnt Meteor.userId() or room.t not in ['c', 'p']
			toastr.error t('Not_allowed')
			return false

		name = $('input[name=roomName]').val()
		if not /^[0-9a-z-_]+$/.test name
			toastr.error t('Invalid_room_name', name)
			return false

		return true

	@validateRoomTopic = =>
		return true

	@validate = =>
		return @validateRoomType() and @validateRoomName() and @validateRoomTopic()