c0f9dd95207e7f79dbfb9830ddec7c4398903dfd
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / PageActions.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 * JavaScript implementations for page actions
16 */
17 define('TYPO3/CMS/Backend/PageActions', ['jquery'], function($) {
18 var PageActions = {
19 settings: {
20 pageId: 0,
21 canEditPage: false,
22 language: {
23 pageOverlayId: 0
24 }
25 },
26 identifier: {
27 $pageTitle: $('h1')
28 }
29 };
30
31 /**
32 * Initialize page title renaming
33 */
34 PageActions.initializePageTitleRenaming = function() {
35 if (!(PageActions.settings.pageId > 0 && PageActions.settings.canEditPage)) {
36 return;
37 }
38 var $inputFieldWrap = $(
39 '<form class="row">' +
40 '<div class="col-lg-4 col-md-6 col-sm-12">' +
41 '<div class="input-group">' +
42 '<input class="form-control">' +
43 '<span class="input-group-btn">' +
44 '<button class="btn btn-default" type="button" data-action="submit"><span class="t3-icon fa fa-floppy-o"></span></button>' +
45 '</span>' +
46 '<span class="input-group-btn">' +
47 '<button class="btn btn-danger" type="button" data-action="cancel"><span class="t3-icon fa fa-times"></span></button>' +
48 '</span>' +
49 '</div>' +
50 '</div>' +
51 '</form>'
52 ),
53 $inputField = $inputFieldWrap.find('input');
54
55 $inputFieldWrap.find('[data-action=cancel]').on('click', function() {
56 $inputFieldWrap.replaceWith(PageActions.identifier.$pageTitle);
57 PageActions.initializePageTitleRenaming();
58 });
59
60 $inputFieldWrap.find('[data-action=submit]').on('click', function() {
61 var newPageTitle = $.trim($inputField.val());
62 if (newPageTitle !== '' && PageActions.identifier.$pageTitle.text() !== newPageTitle) {
63 PageActions.saveChanges($inputField);
64 } else {
65 $inputFieldWrap.find('[data-action=cancel]').trigger('click');
66 }
67 });
68
69 // the form stuff is a wacky workaround to prevent the submission of the docheader form
70 $inputField.parents('form').on('submit', function(e) {
71 e.preventDefault();
72 return false;
73 });
74
75 PageActions.identifier.$pageTitle.on('dblclick', function() {
76 var $h1 = $(this);
77 $h1.replaceWith($inputFieldWrap);
78 $inputField.val($h1.text()).focus();
79
80 $inputField.on('keyup', function(e) {
81 switch (e.which) {
82 case 13: // enter
83 $inputFieldWrap.find('[data-action=submit]').trigger('click');
84 break;
85 case 27: // escape
86 $inputFieldWrap.find('[data-action=cancel]').trigger('click');
87 break;
88 }
89 });
90 });
91 };
92
93 /**
94 * Set the page id (used in the RequireJS callback)
95 */
96 PageActions.setPageId = function(pageId) {
97 PageActions.settings.pageId = pageId;
98 };
99
100 /**
101 * Set if user can edit the page properties
102 */
103 PageActions.setCanEditPage = function(allowed) {
104 PageActions.settings.canEditPage = allowed;
105 };
106
107 /**
108 * Set the overlay id
109 */
110 PageActions.setLanguageOverlayId = function(overlayId) {
111 PageActions.settings.language.pageOverlayId = overlayId;
112 };
113
114 /**
115 * Save the changes and reload the page tree
116 */
117 PageActions.saveChanges = function($field) {
118 var $inputFieldWrap = $field.parents('form');
119 $inputFieldWrap.find('button').addClass('disabled');
120 $field.attr('disabled', 'disabled');
121
122 var parameters = {},
123 pagesTable,
124 recordUid;
125
126 if (PageActions.settings.language.pageOverlayId === 0) {
127 pagesTable = 'pages';
128 recordUid = PageActions.settings.pageId;
129 } else {
130 pagesTable = 'pages_language_overlay';
131 recordUid = PageActions.settings.language.pageOverlayId;
132 }
133
134 parameters['data'] = {};
135 parameters['data'][pagesTable] = {};
136 parameters['data'][pagesTable][recordUid] = {title: $field.val()};
137 require(['TYPO3/CMS/Backend/AjaxDataHandler'], function(DataHandler) {
138 DataHandler.process(parameters).done(function(result) {
139 $inputFieldWrap.find('[data-action=cancel]').trigger('click');
140 PageActions.identifier.$pageTitle.text($field.val());
141 top.TYPO3.Backend.NavigationContainer.PageTree.refreshTree();
142 }).fail(function() {
143 $inputFieldWrap.find('[data-action=cancel]').trigger('click');
144 });
145 });
146 };
147
148 return function() {
149 TYPO3.PageActions = PageActions;
150 return PageActions;
151 }();
152 });