[BUGFIX] Create proper array of cached icons
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Resources / Public / JavaScript / Icons.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 DocumentHeader source code.
10 *
11 * The TYPO3 project - inspiring people to share!
12 */
13
14 /**
15 * Uses the icon API of the core to fetch icons via AJAX.
16 */
17 define(['jquery'], function($) {
18 'use strict';
19
20 var Icons = {
21 cache: {},
22 sizes: {
23 small: 'small',
24 default: 'default',
25 large: 'large',
26 overlay: 'overlay'
27 },
28 states: {
29 default: 'default',
30 disabled: 'disabled'
31 }
32 };
33
34 /**
35 * Get the icon by its identifier.
36 *
37 * @param {string} identifier
38 * @param {string} size
39 * @param {string} overlayIdentifier
40 * @param {string} state
41 * @return {Promise<Array>}
42 */
43 Icons.getIcon = function(identifier, size, overlayIdentifier, state) {
44 return $.when.apply($, Icons.fetch([[identifier, size, overlayIdentifier, state]]));
45 };
46
47 /**
48 * Fetches multiple icons by passing the parameters of getIcon() for each requested
49 * icon as array.
50 *
51 * @param {Array} icons
52 * @return {Promise<Array>}
53 */
54 Icons.getIcons = function(icons) {
55 if (!icons instanceof Array) {
56 throw 'Icons must be an array of multiple definitions.';
57 }
58 return $.when.apply($, Icons.fetch(icons));
59 };
60
61 /**
62 * Performs the AJAX request to fetch the icon.
63 *
64 * @param {Array} icons
65 * @return {Array}
66 * @private
67 */
68 Icons.fetch = function(icons) {
69 var promises = [],
70 requestedIcons = {},
71 cachedRequestedIcons = {};
72
73 for (var i = 0; i < icons.length; ++i) {
74 /**
75 * Icon keys:
76 *
77 * 0: identifier
78 * 1: size
79 * 2: overlayIdentifier
80 * 3: state
81 */
82 var icon = icons[i];
83 icon[1] = icon[1] || Icons.sizes.default;
84 icon[3] = icon[3] || Icons.states.default;
85
86 var cacheIdentifier = icon.join('_');
87 if (Icons.isCached(cacheIdentifier)) {
88 $.extend(cachedRequestedIcons, Icons.getFromCache(cacheIdentifier));
89 } else {
90 requestedIcons[icon[0]] = {
91 cacheIdentifier: cacheIdentifier,
92 icon: icon
93 };
94 }
95 }
96
97 if (Object.keys(cachedRequestedIcons).length > 0) {
98 promises.push(cachedRequestedIcons);
99 }
100
101 if (Object.keys(requestedIcons).length > 0) {
102 promises.push(
103 $.ajax({
104 url: TYPO3.settings.ajaxUrls['icons'],
105 data: {
106 requestedIcons: JSON.stringify(
107 $.map(requestedIcons, function(o) {
108 return [o['icon']];
109 })
110 )
111 },
112 success: function(data) {
113 $.each(data, function(identifier, markup) {
114 var cacheIdentifier = requestedIcons[identifier].cacheIdentifier,
115 cacheEntry = {};
116
117 cacheEntry[identifier] = markup;
118 Icons.putInCache(cacheIdentifier, cacheEntry);
119 });
120 }
121 })
122 );
123 }
124
125 return promises;
126 };
127
128 /**
129 * Check whether icon was fetched already
130 *
131 * @param {String} cacheIdentifier
132 * @returns {Boolean}
133 * @private
134 */
135 Icons.isCached = function(cacheIdentifier) {
136 return typeof Icons.cache[cacheIdentifier] !== 'undefined';
137 };
138
139 /**
140 * Get icon from cache
141 *
142 * @param {String} cacheIdentifier
143 * @returns {String}
144 * @private
145 */
146 Icons.getFromCache = function(cacheIdentifier) {
147 return Icons.cache[cacheIdentifier];
148 };
149
150 /**
151 * Put icon into cache
152 *
153 * @param {String} cacheIdentifier
154 * @param {Object} markup
155 * @private
156 */
157 Icons.putInCache = function(cacheIdentifier, markup) {
158 Icons.cache[cacheIdentifier] = markup;
159 };
160
161 return Icons;
162 });