wisemapping-frontend/packages/mindplot/src/components/widget/LinkEditor.js

154 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-07-16 16:41:58 +02:00
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "powered by wisemapping" text requirement on every single page;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the license at
*
* http://www.wisemapping.org/license
*
* 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.
*/
2021-12-14 18:13:39 +01:00
import $ from 'jquery';
2021-12-13 22:46:50 +01:00
import { $assert } from '@wisemapping/core-js';
2021-12-15 02:54:11 +01:00
import { $msg } from '../Messages';
import BootstrapDialog from '../libraries/bootstrap/BootstrapDialog';
2021-07-16 16:41:58 +02:00
2021-12-13 22:58:59 +01:00
class LinkEditor extends BootstrapDialog {
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @constructs
* @param model
* @throws will throw an error if model is null or undefined
* @extends BootstrapDialog
*/
constructor(model) {
2021-10-05 02:05:34 +02:00
$assert(model, 'model can not be null');
super($msg('LINK'), {
2021-10-05 02:05:34 +02:00
cancelButton: true,
closeButton: true,
acceptButton: true,
removeButton: typeof model.getValue() !== 'undefined',
errorMessage: true,
onEventData: { model },
2021-10-05 02:05:34 +02:00
});
this._model = model;
2021-10-05 02:05:34 +02:00
this.css({ margin: '150px auto' });
const panel = this._buildPanel(model);
this.setContent(panel);
}
2021-10-05 02:05:34 +02:00
_buildPanel(model) {
const result = $('<div></div>').css('padding-top', '5px');
this.form = $('<form></form>').attr({
action: 'none',
id: 'linkFormId',
});
const text = $('<p></p>').text('Paste your url here:');
text.css('margin', '0px 0px 20px');
this.form.append(text);
const section = $('<div></div>').attr({
class: 'input-group',
});
// Add Input
const input = $('<input id="inputUrl"/>').attr({
placeholder: 'http://www.example.com/',
required: 'true',
autofocus: 'autofocus',
class: 'form-control',
});
input.on('keypress', (event) => {
event.stopPropagation();
});
if (model.getValue() != null) {
input.val(model.getValue());
}
// Open Button
const openButton = $('<button></button>').attr({
type: 'button',
class: 'btn btn-default',
});
openButton.html($msg('OPEN_LINK')).css('margin-left', '0px');
openButton.click(() => {
window.open(input.val(), '_blank', 'status=1,width=700,height=450,resize=1');
});
const spanControl = $('<span class="input-group-btn"></span>').append(openButton);
section.append(input);
section.append(spanControl);
this.form.append(section);
const me = this;
2021-12-19 18:07:01 +01:00
this.form.unbind('submit').submit((event) => {
event.preventDefault();
if (me.checkURL(input.val())) {
me.cleanError();
const inputValue = input.val();
if (inputValue != null && $.trim(inputValue) !== '') {
model.setValue(inputValue);
2021-07-16 16:41:58 +02:00
}
2021-12-19 18:07:01 +01:00
me.close();
this.formSubmitted = true;
} else {
me.alertError($msg('URL_ERROR'));
event.stopPropagation();
}
});
2021-10-05 02:05:34 +02:00
result.append(this.form);
return result;
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-12-14 18:06:09 +01:00
* checks whether the input is a valid url
* @return {Boolean} true if the url is valid
*/
2021-10-05 02:05:34 +02:00
checkURL(url) {
const regex = /^(http|https|ftp):\/\/[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i;
2021-10-05 02:05:34 +02:00
return (regex.test(url));
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-12-14 18:06:09 +01:00
* overrides abstract parent method
* triggered when the user clicks the accept button - submits the url input
* @param event
*/
2021-10-05 02:05:34 +02:00
onAcceptClick(event) {
this.formSubmitted = false;
$('#linkFormId').trigger('submit');
if (!this.formSubmitted) {
event.stopPropagation();
}
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-12-14 18:06:09 +01:00
* overrides parent method
* sets the url input on focus
*/
2021-10-05 02:05:34 +02:00
onDialogShown() {
$(this).find('#inputUrl').focus();
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-12-14 18:06:09 +01:00
* overrides abstract parent method
* triggered when the user clicks the remove button - deletes the link
*/
2021-10-05 02:05:34 +02:00
onRemoveClick(event) {
event.data.model.setValue(null);
event.data.dialog.close();
}
}
2021-07-16 16:41:58 +02:00
export default LinkEditor;