cef55807e152d2e07947ce7a065f825c3738ece4
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Resources / Public / JavaScript / Modules / UpgradeDocs.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/Install/UpgradeDocs
16 */
17 define([
18 'jquery',
19 'TYPO3/CMS/Install/Router',
20 'TYPO3/CMS/Install/ProgressBar',
21 'TYPO3/CMS/Install/InfoBox',
22 'TYPO3/CMS/Install/Severity',
23 'bootstrap',
24 'chosen'
25 ], function($, Router, ProgressBar, InfoBox, Severity) {
26 'use strict';
27
28 return {
29 selectorGridderOpener: 't3js-upgradeDocs-open',
30 selectorContentContainer: '.t3js-upgradeDocs-content',
31 selectorMarkReadToken: '#t3js-upgradeDocs-markRead-token',
32 selectorUnmarkReadToken: '#t3js-upgradeDocs-unmarkRead-token',
33 selectorRestFileItem: '.upgrade_analysis_item_to_filter',
34 selectorFulltextSearch: '.gridder-show .t3js-upgradeDocs-fulltext-search',
35 selectorChosenField: '.gridder-show .t3js-upgradeDocs-chosen-select',
36
37 chosenField: null,
38 fulltextSearchField: null,
39
40 initialize: function() {
41 var self = this;
42
43 // Get content on card open
44 $(document).on('cardlayout:card-opened', function(event, $card) {
45 if ($card.hasClass(self.selectorGridderOpener)) {
46 self.getContent();
47 }
48 });
49
50 // Mark a file as read
51 $(document).on('click', '.t3js-upgradeDocs-markRead', function(event) {
52 self.markRead(event.target);
53 });
54 $(document).on('click', '.t3js-upgradeDocs-unmarkRead', function(event) {
55 self.unmarkRead(event.target);
56 });
57
58 // Make jquerys "contains" work case-insensitive
59 jQuery.expr[':'].contains = jQuery.expr.createPseudo(function(arg) {
60 return function(elem) {
61 return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
62 };
63 });
64 },
65
66 getContent: function() {
67 var self = this;
68 var outputContainer = $(this.selectorContentContainer);
69 var message = ProgressBar.render(Severity.loading, 'Loading...', '');
70 outputContainer.empty().html(message);
71 $.ajax({
72 url: Router.getUrl('upgradeDocsGetContent'),
73 cache: false,
74 success: function(data) {
75 if (data.success === true && data.html !== 'undefined' && data.html.length > 0) {
76 outputContainer.empty().append(data.html);
77 $('[data-toggle="tooltip"]').tooltip({container: 'body'});
78 self.chosenField = $(self.selectorChosenField);
79 self.fulltextSearchField = $(self.selectorFulltextSearch);
80 self.initializeChosenSelector();
81 self.chosenField.on('change', function() {
82 self.combinedFilterSearch();
83 });
84 self.fulltextSearchField.on('keyup', function() {
85 self.combinedFilterSearch();
86 });
87 } else {
88 var message = InfoBox.render(Severity.error, 'Something went wrong', '');
89 outputContainer.empty().append(message);
90 }
91 },
92 error: function(xhr) {
93 Router.handleAjaxError(xhr);
94 }
95 });
96 },
97
98 initializeChosenSelector: function() {
99 var self = this;
100 var tagString = '';
101 $(this.selectorRestFileItem).each(function() {
102 tagString += $(this).data('item-tags') + ',';
103 });
104 var tagArray = this.trimExplodeAndUnique(',', tagString);
105 $.each(tagArray, function(i, tag) {
106 self.chosenField.append('<option>' + tag + '</option>');
107 });
108 var config = {
109 '.chosen-select': {width: "100%", placeholder_text_multiple: "tags"},
110 '.chosen-select-deselect': {allow_single_deselect: true},
111 '.chosen-select-no-single': {disable_search_threshold: 10},
112 '.chosen-select-no-results': {no_results_text: 'Oops, nothing found!'},
113 '.chosen-select-width': {width: "100%"}
114 };
115 for (var selector in config) {
116 $(selector).chosen(config[selector]);
117 }
118 this.chosenField.trigger('chosen:updated');
119 },
120
121 combinedFilterSearch: function() {
122 var $items = $('div.item');
123 if (this.chosenField.val().length < 1 && this.fulltextSearchField.val().length < 1) {
124 $('.panel-version:not(:first) > .panel-collapse').collapse('hide');
125 $items.removeClass('hidden searchhit filterhit');
126 return false;
127 }
128 $items.addClass('hidden').removeClass('searchhit filterhit');
129
130 // apply tags
131 if (this.chosenField.val().length > 0) {
132 $items
133 .addClass('hidden')
134 .removeClass('filterhit');
135 var orTags = [];
136 var andTags = [];
137 $.each(this.chosenField.val(), function(index, item) {
138 var tagFilter = '[data-item-tags*="' + item + '"]';
139 if (item.indexOf(':') > 0) {
140 orTags.push(tagFilter);
141 } else {
142 andTags.push(tagFilter);
143 }
144 });
145 var andString = andTags.join('');
146 var tags = [];
147 if (orTags.length) {
148 for (var i = 0; i < orTags.length; i++) {
149 tags.push(andString + orTags[i]);
150 }
151 } else {
152 tags.push(andString);
153 }
154 var tagSelection = tags.join(',');
155 $(tagSelection)
156 .removeClass('hidden')
157 .addClass('searchhit filterhit');
158 } else {
159 $items
160 .addClass('filterhit')
161 .removeClass('hidden');
162 }
163 // apply fulltext search
164 var typedQuery = this.fulltextSearchField.val();
165 $('div.item.filterhit').each(function() {
166 var $item = $(this);
167 if ($(':contains(' + typedQuery + ')', $item).length > 0 || $('input[value*="' + typedQuery + '"]', $item).length > 0) {
168 $item.removeClass('hidden').addClass('searchhit');
169 } else {
170 $item.removeClass('searchhit').addClass('hidden');
171 }
172 });
173
174 $('.searchhit').closest('.panel-collapse').collapse('show');
175
176 //check for empty panels
177 $('.panel-version').each(function() {
178 if ($(this).find('.searchhit', '.filterhit').length < 1) {
179 $(this).find(' > .panel-collapse').collapse('hide');
180 }
181 });
182 },
183
184 markRead: function(element) {
185 var $button = $(element).closest('a');
186 $button.toggleClass('t3js-upgradeDocs-unmarkRead t3js-upgradeDocs-markRead');
187 $button.find('i').toggleClass('fa-check fa-ban');
188 $button.closest('.panel').appendTo('.panel-body-read');
189 $.ajax({
190 method: 'POST',
191 url: Router.getUrl(),
192 data: {
193 'install': {
194 'ignoreFile': $button.data('filepath'),
195 'token': $(this.selectorMarkReadToken).text(),
196 'action': 'upgradeDocsMarkRead'
197 }
198 },
199 error: function(xhr) {
200 Router.handleAjaxError(xhr);
201 }
202 });
203 },
204
205 unmarkRead: function(element) {
206 var $button = $(element).closest('a');
207 var version = $button.closest('.panel').data('item-version');
208 $button.toggleClass('t3js-upgradeDocs-markRead t3js-upgradeDocs-unmarkRead');
209 $button.find('i').toggleClass('fa-check fa-ban');
210 $button.closest('.panel').appendTo('*[data-group-version="' + version + '"] .panel-body');
211 $.ajax({
212 method: 'POST',
213 url: Router.getUrl(),
214 data: {
215 'install': {
216 'ignoreFile': $button.data('filepath'),
217 'token': $(this.selectorUnmarkReadToken).text(),
218 action: 'upgradeDocsUnmarkRead'
219 }
220 },
221 error: function(xhr) {
222 Router.handleAjaxError(xhr);
223 }
224 });
225 },
226
227 trimExplodeAndUnique: function(delimiter, string) {
228 var result = [];
229 var items = string.split(delimiter);
230 for (var i = 0; i < items.length; i++) {
231 var item = items[i].trim();
232 if (item.length > 0) {
233 if ($.inArray(item, result) === -1) {
234 result.push(item);
235 }
236 }
237 }
238 return result;
239 }
240 };
241 });