1
|
|
|
/** global: Craft */ |
2
|
|
|
/** global: Garnish */ |
3
|
|
|
/** global: $ */ |
4
|
|
|
|
5
|
|
|
(function (window) { |
6
|
|
|
|
7
|
|
|
if (!window.Craft || !window.Garnish || !window.$) { |
8
|
|
|
return; |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
Craft.CpFieldInspectPlugin = { |
12
|
|
|
|
13
|
|
|
elementEditors: {}, |
14
|
|
|
|
15
|
|
|
settings: { |
16
|
|
|
settingsClassSelector: 'cp-field-inspect-settings', |
17
|
|
|
infoClassSelector: 'cp-field-inspect-info', |
18
|
|
|
redirectKey: '_CpFieldInspectRedirectTo', |
19
|
|
|
actionInputKeys: [ |
20
|
|
|
'[value="fields/save-field"]', |
21
|
|
|
'[value="sections/save-entry-type"]', |
22
|
|
|
'[value="globals/save-set"]', |
23
|
|
|
'[value="categories/save-group"]', |
24
|
|
|
'[value="volumes/save-volume"]', |
25
|
|
|
'[value="commerce/product-types/save-product-type"]' |
26
|
|
|
] |
27
|
|
|
}, |
28
|
|
|
|
29
|
|
|
init: function (data) { |
30
|
|
|
|
31
|
|
|
var _this = this; |
32
|
|
|
|
33
|
|
|
this.data = data; |
34
|
|
|
this.setPathAndRedirect(); |
35
|
|
|
|
36
|
|
|
// Initialize Live Preview support |
37
|
|
|
var now = new Date().getTime(), |
38
|
|
|
livePreviewPoller = (function getLivePreview() { |
39
|
|
|
if (Craft.livePreview) { |
40
|
|
|
Craft.livePreview.on('enter', this.onLivePreviewEnter.bind(this)); |
41
|
|
|
Craft.livePreview.on('exit', this.onLivePreviewExit.bind(this)); |
42
|
|
|
} else if (new Date().getTime() - now < 2000) { |
43
|
|
|
Garnish.requestAnimationFrame(livePreviewPoller); |
44
|
|
|
} |
45
|
|
|
}).bind(this); |
46
|
|
|
|
47
|
|
|
livePreviewPoller(); |
48
|
|
|
|
49
|
|
|
// Poll for address bar change |
50
|
|
|
var url = window.location.href; |
51
|
|
|
this.addressBarChangeInterval = setInterval(function () { |
52
|
|
|
var newUrl = window.location.href; |
53
|
|
|
if (newUrl === url) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
url = newUrl; |
57
|
|
|
try { |
58
|
|
|
Craft.postActionRequest('cp-field-inspect/default/get-redirect-hash', { url: url }, $.proxy(function (response) { |
59
|
|
|
this.data.redirectUrl = response.data || this.data.redirectUrl || null; |
60
|
|
|
}, _this)); |
61
|
|
|
} catch (error) { |
62
|
|
|
console.error(error); |
63
|
|
|
} |
64
|
|
|
}, 100); |
65
|
|
|
|
66
|
|
|
// Add event handlers |
67
|
|
|
Garnish.$doc |
68
|
|
|
.on('click', '[data-cpfieldlinks-sourcebtn]', $.proxy(this.onSourceEditBtnClick, this)) |
69
|
|
|
.on('click', '.matrix .btn.add, .matrix .btn[data-type]', $.proxy(this.onMatrixBlockAddButtonClick, this)) |
70
|
|
|
.ajaxComplete($.proxy(this.onAjaxComplete, this)); |
71
|
|
|
|
72
|
|
|
Garnish.requestAnimationFrame($.proxy(this.addFieldLinks, this)); |
73
|
|
|
}, |
74
|
|
|
|
75
|
|
|
initElementEditor: function () { |
76
|
|
|
var now = new Date().getTime(), |
77
|
|
|
doInitElementEditor = $.proxy(function () { |
78
|
|
|
var timestamp = new Date().getTime(), |
79
|
|
|
$elementEditor = $('.elementeditor:last'), |
80
|
|
|
$hud = $elementEditor.length > 0 ? $elementEditor.closest('.hud') : false, |
81
|
|
|
elementEditor = $hud && $hud.length > 0 ? $hud.data('elementEditor') : false; |
82
|
|
|
if (elementEditor && elementEditor.hud) { |
83
|
|
|
this.elementEditors[elementEditor._namespace] = elementEditor; |
84
|
|
|
elementEditor.hud.on('hide', $.proxy(this.destroyElementEditor, this, elementEditor)); |
85
|
|
|
Garnish.requestAnimationFrame($.proxy(this.addFieldLinks, this)); |
86
|
|
|
} else if (timestamp - now < 2000) { // Poll for 2 secs |
87
|
|
|
Garnish.requestAnimationFrame(doInitElementEditor); |
88
|
|
|
} |
89
|
|
|
}, this); |
90
|
|
|
doInitElementEditor(); |
91
|
|
|
}, |
92
|
|
|
|
93
|
|
|
destroyElementEditor: function (elementEditor) { |
94
|
|
|
if (this.elementEditors.hasOwnProperty(elementEditor._namespace)) { |
95
|
|
|
delete this.elementEditors[elementEditor._namespace]; |
96
|
|
|
} |
97
|
|
|
}, |
98
|
|
|
|
99
|
|
|
setPathAndRedirect: function () { |
100
|
|
|
var redirectTo = Craft.getLocalStorage(this.settings.redirectKey); |
101
|
|
|
if (redirectTo) { |
102
|
|
|
var $actionInput = $('input[type="hidden"][name="action"]').filter(this.settings.actionInputKeys.join(',')); |
103
|
|
|
var $redirectInput = $('input[type="hidden"][name="redirect"]'); |
104
|
|
|
if ($actionInput.length > 0 && $redirectInput.length > 0) { |
105
|
|
|
$redirectInput.attr('value', redirectTo); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
Craft.setLocalStorage(this.settings.redirectKey, null); |
109
|
|
|
}, |
110
|
|
|
|
111
|
|
|
addFieldLinks: function () { |
112
|
|
|
|
113
|
|
|
var targets = [$(this.getFieldContextSelector())]; |
114
|
|
|
|
115
|
|
|
if (this.elementEditors && Object.keys(this.elementEditors).length) { |
116
|
|
|
for (var key in this.elementEditors) { |
117
|
|
|
if (this.elementEditors.hasOwnProperty(key)) { |
118
|
|
|
targets.push(this.elementEditors[key].$form); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!targets.length) { |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
var _this = this; |
128
|
|
|
|
129
|
|
|
for (var i = 0; i < targets.length; ++i) { |
130
|
|
|
|
131
|
|
|
var $target = targets[i]; |
132
|
|
|
if (!$target || !$target.length) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
var $copyFieldHandleButtons = $target.find('.field .heading [id$=-field-attribute].code:not([data-cpfieldlinks-inited])'); |
137
|
|
|
$copyFieldHandleButtons.each(function () { |
138
|
|
|
|
139
|
|
|
var $btn = $(this); |
140
|
|
|
$btn.attr('data-cpfieldlinks-inited', true); |
141
|
|
|
|
142
|
|
|
var field = $btn.parents('.field').get().slice(-1).pop(); |
143
|
|
|
if (!field) { |
144
|
|
|
return; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
var fieldId = _this.getFieldId(field); |
148
|
|
|
if (!fieldId) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$btn |
153
|
|
|
.append('<span data-icon="settings" title="' + (_this.data.editFieldBtnLabel || 'Edit field settings') + '" />') |
154
|
|
|
.on('click', '[data-icon="settings"]', function (e) { |
155
|
|
|
e.preventDefault(); |
156
|
|
|
e.stopPropagation(); |
157
|
|
|
_this.redirectToFieldSettings(fieldId); |
158
|
|
|
}); |
159
|
|
|
|
160
|
|
|
}).on('mouseleave', function () { |
161
|
|
|
$(this).blur(); |
162
|
|
|
}); |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
}, |
166
|
|
|
|
167
|
|
|
doRedirect: function (href) { |
168
|
|
|
Craft.setLocalStorage(this.settings.redirectKey, this.data.redirectUrl || null); |
169
|
|
|
window.location.href = href; |
170
|
|
|
}, |
171
|
|
|
|
172
|
|
|
redirectToFieldSettings: function (fieldId) { |
173
|
|
|
var href = Craft.CpFieldInspectPlugin.data.baseEditFieldUrl + '/' + fieldId; |
174
|
|
|
this.doRedirect(href); |
175
|
|
|
}, |
176
|
|
|
|
177
|
|
|
getFieldId: function (field) { |
178
|
|
|
var id = $(field).attr('id'); |
179
|
|
|
if (!id) { |
180
|
|
|
return null; |
181
|
|
|
} |
182
|
|
|
var segments = id.split('-'); |
183
|
|
|
if (segments.length < 3) { |
184
|
|
|
return null; |
185
|
|
|
} |
186
|
|
|
var handle = segments[segments.length - 2]; |
187
|
|
|
return (this.data.fields || {})[handle] || null; |
188
|
|
|
}, |
189
|
|
|
|
190
|
|
|
getFieldContextSelector: function () { |
191
|
|
|
if (this.isLivePreview) { |
192
|
|
|
return '.lp-editor'; |
193
|
|
|
} |
194
|
|
|
return '#main'; |
195
|
|
|
}, |
196
|
|
|
|
197
|
|
|
onLivePreviewEnter: function () { |
198
|
|
|
this.isLivePreview = true; |
199
|
|
|
Garnish.requestAnimationFrame($.proxy(this.addFieldLinks, this)); |
200
|
|
|
}, |
201
|
|
|
|
202
|
|
|
onLivePreviewExit: function () { |
203
|
|
|
this.isLivePreview = false; |
204
|
|
|
Garnish.requestAnimationFrame($.proxy(this.addFieldLinks, this)); |
205
|
|
|
}, |
206
|
|
|
|
207
|
|
|
onSourceEditBtnClick: function (e) { |
208
|
|
|
e.preventDefault(); |
209
|
|
|
this.doRedirect(e.target.href); |
210
|
|
|
}, |
211
|
|
|
|
212
|
|
|
onMatrixBlockAddButtonClick: function () { |
213
|
|
|
Garnish.requestAnimationFrame($.proxy(this.addFieldLinks, this)); |
214
|
|
|
}, |
215
|
|
|
|
216
|
|
|
onAjaxComplete: function(e, status, requestData) { |
217
|
|
|
if (requestData.url.indexOf('switch-entry-type') === -1) { |
218
|
|
|
return; |
219
|
|
|
} |
220
|
|
|
const $entryTypeSelect = $('#entryType'); |
221
|
|
|
if ($entryTypeSelect.length) { |
222
|
|
|
const typeId = $entryTypeSelect.val(); |
223
|
|
|
$('[data-cpfieldlinks-sourcebtn][data-typeid]:not([data-typeid="' + typeId + '"]').hide(); |
224
|
|
|
$('[data-cpfieldlinks-sourcebtn][data-typeid="' + typeId + '"]').show(); |
225
|
|
|
} |
226
|
|
|
this.addFieldLinks(); |
227
|
|
|
} |
228
|
|
|
}; |
229
|
|
|
|
230
|
|
|
} (window)); |
231
|
|
|
|