2018-02-14 17:19:09 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';
|
2019-02-06 16:05:21 +01:00
|
|
|
import { CoreAppProvider } from '@providers/app';
|
|
|
|
import { CoreConfigProvider } from '@providers/config';
|
|
|
|
import { CoreEventsProvider } from '@providers/events';
|
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreUtilsProvider } from '@providers/utils/utils';
|
|
|
|
import { CoreTextUtilsProvider } from '@providers/utils/text';
|
2019-02-06 16:05:21 +01:00
|
|
|
import { CoreConstants } from '@core/constants';
|
2018-02-14 17:19:09 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to display a "send message form".
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
* This component will display a standalone send message form in order to have a better UX.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* <core-send-message-form (onSubmit)="sendMessage($event)" [placeholder]="'core.messages.newmessage' | translate"
|
|
|
|
* [show-keyboard]="showKeyboard"></core-send-message-form>
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'core-send-message-form',
|
2018-06-12 08:41:17 +02:00
|
|
|
templateUrl: 'core-send-message-form.html'
|
2018-02-14 17:19:09 +01:00
|
|
|
})
|
|
|
|
export class CoreSendMessageFormComponent implements OnInit {
|
2018-04-11 13:01:56 +02:00
|
|
|
@Input() message: string; // Input text.
|
2018-02-14 17:19:09 +01:00
|
|
|
@Input() placeholder = ''; // Placeholder for the input area.
|
|
|
|
@Input() showKeyboard = false; // If keyboard is shown or not.
|
|
|
|
@Output() onSubmit: EventEmitter<string>; // Send data when submitting the message form.
|
|
|
|
@Output() onResize: EventEmitter<void>; // Emit when resizing the textarea.
|
|
|
|
|
2019-02-06 16:05:21 +01:00
|
|
|
protected sendOnEnter: boolean;
|
|
|
|
|
|
|
|
constructor(private utils: CoreUtilsProvider, private textUtils: CoreTextUtilsProvider, configProvider: CoreConfigProvider,
|
|
|
|
eventsProvider: CoreEventsProvider, sitesProvider: CoreSitesProvider, private appProvider: CoreAppProvider) {
|
|
|
|
|
2018-02-14 17:19:09 +01:00
|
|
|
this.onSubmit = new EventEmitter();
|
|
|
|
this.onResize = new EventEmitter();
|
2019-02-06 16:05:21 +01:00
|
|
|
|
|
|
|
configProvider.get(CoreConstants.SETTINGS_SEND_ON_ENTER, !this.appProvider.isMobile()).then((sendOnEnter) => {
|
|
|
|
this.sendOnEnter = !!sendOnEnter;
|
|
|
|
});
|
|
|
|
|
|
|
|
eventsProvider.on(CoreEventsProvider.SEND_ON_ENTER_CHANGED, (newValue) => {
|
|
|
|
this.sendOnEnter = newValue;
|
|
|
|
}, sitesProvider.getCurrentSiteId());
|
2018-02-14 17:19:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.showKeyboard = this.utils.isTrueOrOne(this.showKeyboard);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Form submitted.
|
2018-08-06 08:51:15 +02:00
|
|
|
*
|
|
|
|
* @param {Event} $event Mouse event.
|
2018-02-14 17:19:09 +01:00
|
|
|
*/
|
2018-08-06 08:51:15 +02:00
|
|
|
submitForm($event: Event): void {
|
|
|
|
$event.preventDefault();
|
|
|
|
$event.stopPropagation();
|
2018-02-14 17:19:09 +01:00
|
|
|
|
2018-08-06 08:51:15 +02:00
|
|
|
let value = this.message.trim();
|
2018-02-14 17:19:09 +01:00
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
// Silent error.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-06 08:51:15 +02:00
|
|
|
this.message = ''; // Reset the form.
|
|
|
|
|
2018-02-14 17:19:09 +01:00
|
|
|
value = this.textUtils.replaceNewLines(value, '<br>');
|
|
|
|
this.onSubmit.emit(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Textarea resized.
|
|
|
|
*/
|
|
|
|
textareaResized(): void {
|
|
|
|
this.onResize.emit();
|
|
|
|
}
|
2019-02-06 16:05:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enter key clicked.
|
|
|
|
*
|
|
|
|
* @param {Event} e Event.
|
|
|
|
* @param {string} other The name of the other key that was clicked, undefined if no other key.
|
|
|
|
*/
|
|
|
|
enterClicked(e: Event, other: string): void {
|
|
|
|
if (this.sendOnEnter && !other) {
|
|
|
|
// Enter clicked, send the message.
|
|
|
|
this.submitForm(e);
|
|
|
|
} else if (!this.sendOnEnter && !this.appProvider.isMobile()) {
|
|
|
|
if ((this.appProvider.isMac() && other == 'meta') || (!this.appProvider.isMac() && other == 'control')) {
|
|
|
|
// Cmd+Enter or Ctrl+Enter, send message.
|
|
|
|
this.submitForm(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-14 17:19:09 +01:00
|
|
|
}
|