0e2c47c920c4f4edf9e1a8822d5afa10f2ad3970
[Packages/TYPO3.CMS.git] / typo3 / sysext / recordlist / Resources / Public / JavaScript / LinkBrowser.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/Recordlist/LinkBrowser
16 * LinkBrowser communication with parent window
17 */
18 define(['jquery'], function($) {
19 'use strict';
20
21 /**
22 *
23 * @type {{thisScriptUrl: string, urlParameters: {}, parameters: {}, addOnParams: string, linkAttributeFields: Array, additionalLinkAttributes: {}, finalizeFunction: null}}
24 * @exports TYPO3/CMS/Recordlist/LinkBrowser
25 */
26 var LinkBrowser = {
27 thisScriptUrl: '',
28 urlParameters: {},
29 parameters: {},
30 addOnParams: '',
31 linkAttributeFields: [],
32 additionalLinkAttributes: {},
33 finalizeFunction: null
34 };
35
36 /**
37 * Collect the link attributes values as object
38 *
39 * @returns {Object}
40 */
41 LinkBrowser.getLinkAttributeValues = function() {
42 var attributeValues = {};
43 $.each(LinkBrowser.linkAttributeFields, function(index, fieldName) {
44 var val = $('[name="l' + fieldName + '"]').val();
45 if (val) {
46 attributeValues[fieldName] = val;
47 }
48 });
49 $.extend(attributeValues, LinkBrowser.additionalLinkAttributes);
50 return attributeValues;
51 };
52
53 /**
54 *
55 */
56 LinkBrowser.loadTarget = function() {
57 $('.t3js-linkTarget').val($(this).val());
58 this.selectedIndex = 0;
59 };
60
61 /**
62 * Encode objects to GET parameter arrays in PHP notation
63 *
64 * @param {Object} obj
65 * @param {String} prefix
66 * @param {String} url
67 * @returns {String}
68 */
69 LinkBrowser.encodeGetParameters = function(obj, prefix, url) {
70 var str = [];
71 for(var p in obj) {
72 if (obj.hasOwnProperty(p)) {
73 var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p];
74 if (url.indexOf(k + "=") === -1) {
75 str.push(
76 typeof v === "object"
77 ? LinkBrowser.encodeGetParameters(v, k, url)
78 : encodeURIComponent(k) + "=" + encodeURIComponent(v)
79 );
80 }
81 }
82 }
83 return '&' + str.join("&");
84 };
85
86 /**
87 * Set an additional attribute for the link
88 *
89 * @param {String} name
90 * @param value
91 */
92 LinkBrowser.setAdditionalLinkAttribute = function(name, value) {
93 LinkBrowser.additionalLinkAttributes[name] = value;
94 };
95
96 /**
97 * Stores the final link
98 *
99 * This method MUST be overridden in the actual implementation of the link browser.
100 * The function is responsible for encoding the link (and possible link attributes) and
101 * returning it to the caller (e.g. FormEngine, RTE, etc)
102 *
103 * @param {String} link The select element or anything else which identifies the link (e.g. "page:<pageUid>" or "file:<uid>")
104 */
105 LinkBrowser.finalizeFunction = function(link) {
106 throw 'The link browser requires the finalizeFunction to be set. Seems like you discovered a major bug.';
107 };
108
109 $(function() {
110 var data = $('body').data();
111
112 LinkBrowser.thisScriptUrl = data.thisScriptUrl;
113 LinkBrowser.urlParameters = data.urlParameters;
114 LinkBrowser.parameters = data.parameters;
115 LinkBrowser.addOnParams = data.addOnParams;
116 LinkBrowser.linkAttributeFields = data.linkAttributeFields;
117
118 $('.t3js-targetPreselect').on('change', LinkBrowser.loadTarget);
119 });
120
121 /**
122 * Global jumpTo function
123 *
124 * Used by tree implementation
125 *
126 * @param {String} URL
127 * @param {String} anchor
128 * @returns {Boolean}
129 */
130 window.jumpToUrl = function(URL, anchor) {
131 if (URL.charAt(0) === '?') {
132 URL = LinkBrowser.thisScriptUrl + URL.substring(1);
133 }
134 var urlParameters = LinkBrowser.encodeGetParameters(LinkBrowser.urlParameters, '', URL);
135 var parameters = LinkBrowser.encodeGetParameters(LinkBrowser.getLinkAttributeValues(), 'linkAttributes', '');
136
137 window.location.href = URL + urlParameters + parameters + LinkBrowser.addOnParams + (typeof(anchor) === "string" ? anchor : '');
138 return false;
139 };
140
141 return LinkBrowser;
142 });