5abcbd71facb722eb769b18e49b5b69ad38efcee
[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
72 for (var i = 0; i < icons.length; ++i) {
73 /**
74 * Icon keys:
75 *
76 * 0: identifier
77 * 1: size
78 * 2: overlayIdentifier
79 * 3: state
80 */
81 var icon = icons[i];
82 icon[1] = icon[1] || Icons.sizes.default;
83 icon[3] = icon[3] || Icons.states.default;
84
85 var cacheIdentifier = icon.join('_');
86 if (Icons.isCached(cacheIdentifier)) {
87 promises.push(Icons.getFromCache(cacheIdentifier));
88 } else {
89 requestedIcons[icon[0]] = {
90 cacheIdentifier: cacheIdentifier,
91 icon: icon
92 };
93 }
94 }
95
96 if (Object.keys(requestedIcons).length > 0) {
97 promises.push(
98 $.ajax({
99 url: TYPO3.settings.ajaxUrls['icons_get'],
100 data: {
101 requestedIcons: JSON.stringify(
102 $.map(requestedIcons, function(o) {
103 return [o['icon']];
104 })
105 )
106 },
107 success: function(data) {
108 $.each(data, function(identifier, markup) {
109 var cacheIdentifier = requestedIcons[identifier].cacheIdentifier,
110 cacheEntry = {};
111
112 cacheEntry[identifier] = markup;
113 Icons.putInCache(cacheIdentifier, cacheEntry);
114 });
115 }
116 })
117 );
118 }
119
120 return promises;
121 };
122
123 /**
124 * Check whether icon was fetched already
125 *
126 * @param {String} cacheIdentifier
127 * @returns {Boolean}
128 * @private
129 */
130 Icons.isCached = function(cacheIdentifier) {
131 return typeof Icons.cache[cacheIdentifier] !== 'undefined';
132 };
133
134 /**
135 * Get icon from cache
136 *
137 * @param {String} cacheIdentifier
138 * @returns {String}
139 * @private
140 */
141 Icons.getFromCache = function(cacheIdentifier) {
142 return Icons.cache[cacheIdentifier];
143 };
144
145 /**
146 * Put icon into cache
147 *
148 * @param {String} cacheIdentifier
149 * @param {Object} markup
150 * @private
151 */
152 Icons.putInCache = function(cacheIdentifier, markup) {
153 Icons.cache[cacheIdentifier] = markup;
154 };
155
156 return Icons;
157 });