719768adebd0afe0efa3f7321342e83e7ec17ef4
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / OnlineMedia.js
1 /*
2 * This file is part of the TYPO3 CMS project.
3 *
4 * It is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License, either version 2
6 * of the License, or any later version.
7 *
8 * For the full copyright and license information, please read the
9 * LICENSE.txt file that was distributed with this source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Module: TYPO3/CMS/Backend/OnlineMedia
16 * Javascript for show the online media dialog
17 */
18 define(['jquery',
19 'nprogress',
20 'TYPO3/CMS/Backend/Modal',
21 'TYPO3/CMS/Backend/Severity',
22 'TYPO3/CMS/Lang/Lang'
23 ], function($, NProgress, Modal, Severity) {
24 'use strict';
25
26 /**
27 *
28 * @param element
29 * @constructor
30 * @exports TYPO3/CMS/Backend/OnlineMedia
31 */
32 var OnlineMediaPlugin = function(element) {
33 var me = this;
34 me.$btn = $(element);
35 me.target = me.$btn.data('target-folder');
36 me.irreObjectUid = me.$btn.data('file-irre-object');
37 me.allowed = me.$btn.data('online-media-allowed');
38 me.btnSubmit = me.$btn.data('data-btn-submit') || 'Add';
39 me.placeholder = me.$btn.data('placeholder') || 'Paste media url here...';
40
41 /**
42 *
43 * @param {String} url
44 */
45 me.addOnlineMedia = function(url) {
46 NProgress.start();
47 $.post(TYPO3.settings.ajaxUrls['online_media_create'],
48 {
49 url: url,
50 targetFolder: me.target,
51 allowed: me.allowed
52 },
53 function(data) {
54 if (data.file) {
55 inline.delayedImportElement(
56 me.irreObjectUid,
57 'sys_file',
58 data.file,
59 'file'
60 );
61 } else {
62 var $confirm = Modal.confirm(
63 'ERROR',
64 data.error,
65 Severity.error,
66 [{
67 text: TYPO3.lang['button.ok'] || 'OK',
68 btnClass: 'btn-' + Severity.getCssClass(Severity.error),
69 name: 'ok',
70 active: true
71 }]
72 ).on('confirm.button.ok', function() {
73 $confirm.modal('hide');
74 });
75 }
76 NProgress.done();
77 }
78 );
79 };
80
81 /**
82 * Trigger the modal
83 */
84 me.triggerModal = function() {
85 var $modal = Modal.show(
86 me.$btn.attr('title'),
87 '<div class="form-control-wrap">' +
88 '<input type="text" class="form-control online-media-url" placeholder="' + me.placeholder + '" />' +
89 '</div>',
90 Severity.notice,
91 [{
92 text: me.btnSubmit,
93 btnClass: 'btn',
94 name: 'ok',
95 trigger: function() {
96 var url = $modal.find('input.online-media-url').val();
97 if (url) {
98 $modal.modal('hide');
99 me.addOnlineMedia(url);
100 }
101 }
102 }]
103 );
104
105 $modal.on('shown.bs.modal', function() {
106 // focus the input field
107 $(this).find('input.online-media-url').first().focus().on('keydown', function(e) {
108 if (e.keyCode === 13) {
109 $modal.find('button[name="ok"]').trigger('click');
110 }
111 });
112 });
113 };
114
115 return {triggerModal: me.triggerModal};
116 };
117
118 $(document).on('click', '.t3js-online-media-add-btn', function(evt) {
119 evt.preventDefault();
120 var $this = $(this),
121 onlineMediaPlugin = $this.data('OnlineMediaPlugin');
122 if (!onlineMediaPlugin) {
123 $this.data('OnlineMediaPlugin', (onlineMediaPlugin = new OnlineMediaPlugin(this)));
124 }
125 onlineMediaPlugin.triggerModal();
126 });
127
128 });