|
1
|
|
|
/** |
|
2
|
|
|
* Spoon plugin for Craft CMS |
|
3
|
|
|
* |
|
4
|
|
|
* @author Angell & Co |
|
5
|
|
|
* @copyright Copyright (c) 2018 Angell & Co |
|
6
|
|
|
* @link https://angell.io |
|
7
|
|
|
* @package Spoon |
|
8
|
|
|
* @since 3.0.0 |
|
9
|
|
|
*/ |
|
10
|
|
|
(function($){ |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
if (typeof Spoon == 'undefined') |
|
14
|
|
|
{ |
|
15
|
|
|
Spoon = {}; |
|
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Overrides the default Matrix ‘add block’ buttons with our grouped ones |
|
20
|
|
|
* and keeps them up to date based on the current context. |
|
21
|
|
|
* |
|
22
|
|
|
* Also adds any field layouts that may exist for each block type |
|
23
|
|
|
* in the current context. |
|
24
|
|
|
*/ |
|
25
|
|
|
Spoon.FieldManipulator = Garnish.Base.extend( |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
// _matrixInputsNeedReinit: false, |
|
29
|
|
|
|
|
30
|
|
|
_handleMatrixInputInitProxy: null, |
|
31
|
|
|
_handleMatrixInputBlockAddedProxy: null, |
|
32
|
|
|
|
|
33
|
|
|
init: function(settings) |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
// Set up |
|
37
|
|
|
this.setSettings(settings, Spoon.FieldManipulator.defaults); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
// Work out if we’re in the 'entrytype' context so we can keep things up to date |
|
40
|
|
|
if (this.settings.context.split(':')[0] === 'entrytype') |
|
41
|
|
|
{ |
|
42
|
|
|
// Listen to entry type switch |
|
43
|
|
|
Garnish.on(Craft.EntryTypeSwitcher, 'beforeTypeChange', $.proxy(function(ev) |
|
44
|
|
|
{ |
|
45
|
|
|
this.settings.context = 'entrytype:' + ev.target.$typeSelect.val(); |
|
46
|
|
|
}, this)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
this._handleMatrixInputInitProxy = $.proxy(this, 'handleMatrixInputInit'); |
|
50
|
|
|
this._handleMatrixInputBlockAddedProxy = $.proxy(this, 'handleMatrixInputBlockAdded'); |
|
51
|
|
|
|
|
52
|
|
|
Garnish.on(Craft.MatrixInput, 'afterInit', this._handleMatrixInputInitProxy); |
|
53
|
|
|
Garnish.on(Craft.MatrixInput, 'blockAdded', this._handleMatrixInputBlockAddedProxy); |
|
54
|
|
|
|
|
55
|
|
|
if (typeof Craft.SuperTable !== "undefined") { |
|
56
|
|
|
Garnish.on(Craft.SuperTable.MatrixInputAlt, 'afterInit', this._handleMatrixInputInitProxy); |
|
57
|
|
|
Garnish.on(Craft.SuperTable.MatrixInputAlt, 'blockAdded', this._handleMatrixInputBlockAddedProxy); |
|
58
|
|
|
} |
|
59
|
|
|
}, |
|
60
|
|
|
|
|
61
|
|
|
handleMatrixInputInit: function(ev) |
|
62
|
|
|
{ |
|
63
|
|
|
var $matrixField = ev.target.$container, |
|
64
|
|
|
$blocks = ev.target.$blockContainer.children(); |
|
65
|
|
|
|
|
66
|
|
|
this.initBlockTypeGroups($matrixField); |
|
67
|
|
|
|
|
68
|
|
|
$blocks.each($.proxy(function(key,block) |
|
69
|
|
|
{ |
|
70
|
|
|
this.initBlocks($(block), $matrixField); |
|
71
|
|
|
}, this)); |
|
72
|
|
|
}, |
|
73
|
|
|
|
|
74
|
|
|
handleMatrixInputBlockAdded: function(ev) |
|
75
|
|
|
{ |
|
76
|
|
|
var $matrixField = ev.target.$container, |
|
77
|
|
|
$block = $(ev.$block); |
|
78
|
|
|
|
|
79
|
|
|
this.initBlocks($block, $matrixField); |
|
80
|
|
|
}, |
|
81
|
|
|
|
|
82
|
|
|
initBlockTypeGroups: function($matrixField) |
|
83
|
|
|
{ |
|
84
|
|
|
|
|
85
|
|
|
// check if we’ve already spooned this field |
|
86
|
|
|
if ( !$matrixField.data('spooned') ) |
|
87
|
|
|
{ |
|
88
|
|
|
|
|
89
|
|
|
// get matrix field handle out of the dom |
|
90
|
|
|
var matrixFieldHandle = this._getMatrixFieldName($matrixField); |
|
91
|
|
|
|
|
92
|
|
|
// Filter by the current matrix field |
|
93
|
|
|
var spoonedBlockTypes = []; |
|
94
|
|
|
|
|
95
|
|
|
// Check current context first |
|
96
|
|
|
if (typeof this.settings.blockTypes[this.settings.context] !== "undefined") |
|
97
|
|
|
{ |
|
98
|
|
|
spoonedBlockTypes = $.grep(this.settings.blockTypes[this.settings.context], function(e){ return e.fieldHandle === matrixFieldHandle; }); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// Check global context |
|
102
|
|
|
if (spoonedBlockTypes.length < 1 && typeof this.settings.blockTypes['global'] !== "undefined") |
|
103
|
|
|
{ |
|
104
|
|
|
spoonedBlockTypes = $.grep(this.settings.blockTypes['global'], function(e){ return e.fieldHandle === matrixFieldHandle; }); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// Check we have some config |
|
108
|
|
|
if ( spoonedBlockTypes.length >= 1 ) |
|
109
|
|
|
{ |
|
110
|
|
|
|
|
111
|
|
|
// add some data to tell us we’re spooned |
|
112
|
|
|
$matrixField.data('spooned', true); |
|
113
|
|
|
|
|
114
|
|
|
// store the data for when we loop the blocks themselves so we don’t have to run all this again |
|
115
|
|
|
$matrixField.data('spoon-block-types', spoonedBlockTypes); |
|
116
|
|
|
|
|
117
|
|
|
// find the original buttons |
|
118
|
|
|
var $origButtons = $matrixField.find('> .buttons').first(); |
|
119
|
|
|
|
|
120
|
|
|
// hide the original ones and start the button spooning process |
|
121
|
|
|
$origButtons.addClass('hidden'); |
|
122
|
|
|
|
|
123
|
|
|
// make our own container, not using .buttons as it gets event bindings |
|
124
|
|
|
// from MatrixInput.js that we really don't want |
|
125
|
|
|
var $spoonedButtonsContainer = $('<div class="buttons-spooned" />').insertAfter($origButtons); |
|
126
|
|
|
|
|
127
|
|
|
// the main button group |
|
128
|
|
|
var $mainButtons = $('<div class="btngroup" />').appendTo($spoonedButtonsContainer); |
|
129
|
|
|
|
|
130
|
|
|
// the secondary one, used when the container gets too small |
|
131
|
|
|
var $secondaryButtons = $('<div class="btn add icon menubtn hidden">'+Craft.t('app', 'Add a block')+'</div>').appendTo($spoonedButtonsContainer), |
|
132
|
|
|
$secondaryMenu = $('<div class="menu spoon-secondary-menu" />').appendTo($spoonedButtonsContainer); |
|
133
|
|
|
|
|
134
|
|
|
// loop each block type config |
|
135
|
|
|
for (var i = 0; i < spoonedBlockTypes.length; i++) |
|
136
|
|
|
{ |
|
137
|
|
|
|
|
138
|
|
|
// check if group exists, add if not |
|
139
|
|
|
if ( $mainButtons.find('[data-spooned-group="'+spoonedBlockTypes[i].groupName+'"]').length === 0 ) |
|
140
|
|
|
{ |
|
141
|
|
|
// main buttons |
|
142
|
|
|
var $mainMenuBtn = $('<div class="btn menubtn">'+Craft.t('site', spoonedBlockTypes[i]['groupName'])+'</div>').appendTo($mainButtons), |
|
|
|
|
|
|
143
|
|
|
$mainMenu = $('<div class="menu" data-spooned-group="'+spoonedBlockTypes[i]['groupName']+'" />').appendTo($mainButtons), |
|
144
|
|
|
$mainUl = $('<ul />').appendTo($mainMenu); |
|
145
|
|
|
|
|
146
|
|
|
// single group buttons |
|
147
|
|
|
if (i!==0) |
|
148
|
|
|
{ |
|
149
|
|
|
$('<hr>').appendTo($secondaryMenu); |
|
150
|
|
|
} |
|
151
|
|
|
$('<h6>'+Craft.t('site', spoonedBlockTypes[i]['groupName'])+'</h6>').appendTo($secondaryMenu); |
|
152
|
|
|
var $secondaryUl = $('<ul/>').appendTo($secondaryMenu); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// make a link |
|
156
|
|
|
$li = $('<li><a data-type="'+spoonedBlockTypes[i].matrixBlockType.handle+'">'+Craft.t('site', spoonedBlockTypes[i].matrixBlockType.name)+'</a></li>'); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
// add it to the main list |
|
159
|
|
|
$li.appendTo($mainUl); |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
// add a copy to the secondary one as well |
|
162
|
|
|
$li.clone().appendTo($secondaryUl); |
|
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
// make the MenuBtns work |
|
167
|
|
|
$mainButtons.find('.menubtn').each(function() |
|
168
|
|
|
{ |
|
169
|
|
|
|
|
170
|
|
|
new Garnish.MenuBtn($(this), |
|
|
|
|
|
|
171
|
|
|
{ |
|
172
|
|
|
onOptionSelect: function(option) |
|
173
|
|
|
{ |
|
174
|
|
|
// find our type and click the correct original btn! |
|
175
|
|
|
var type = $(option).data('type'); |
|
176
|
|
|
$origButtons.find('[data-type="'+type+'"]').trigger('click'); |
|
177
|
|
|
} |
|
178
|
|
|
}); |
|
179
|
|
|
|
|
180
|
|
|
}); |
|
181
|
|
|
|
|
182
|
|
|
new Garnish.MenuBtn($secondaryButtons, |
|
|
|
|
|
|
183
|
|
|
{ |
|
184
|
|
|
onOptionSelect: function(option) |
|
185
|
|
|
{ |
|
186
|
|
|
// find our type and click the correct original btn! |
|
187
|
|
|
var type = $(option).data('type'); |
|
188
|
|
|
$origButtons.find('[data-type="'+type+'"]').trigger('click'); |
|
189
|
|
|
} |
|
190
|
|
|
}); |
|
191
|
|
|
|
|
192
|
|
|
// Bind a resize to the $matrixField so we can work out which groups UI to show |
|
193
|
|
|
this.addListener($matrixField, 'resize', $.proxy(function() |
|
194
|
|
|
{ |
|
195
|
|
|
// Do we know what the button group width is yet? |
|
196
|
|
|
if (!$matrixField.data('spoon-main-buttons-width')) |
|
197
|
|
|
{ |
|
198
|
|
|
$matrixField.data('spoon-main-buttons-width', $mainButtons.width()); |
|
199
|
|
|
|
|
200
|
|
|
if (!$matrixField.data('spoon-main-buttons-width')) |
|
201
|
|
|
{ |
|
202
|
|
|
return; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
// Check the widths and do the hide/show |
|
207
|
|
|
var fieldWidth = $matrixField.width(), |
|
208
|
|
|
mainButtonsWidth = $matrixField.data('spoon-main-buttons-width'); |
|
209
|
|
|
if (fieldWidth < mainButtonsWidth) |
|
210
|
|
|
{ |
|
211
|
|
|
$secondaryButtons.removeClass('hidden'); |
|
212
|
|
|
$mainButtons.addClass('hidden'); |
|
213
|
|
|
} |
|
214
|
|
|
else |
|
215
|
|
|
{ |
|
216
|
|
|
$secondaryButtons.addClass('hidden'); |
|
217
|
|
|
$mainButtons.removeClass('hidden'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
}, this)); |
|
221
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
}, |
|
227
|
|
|
|
|
228
|
|
|
initBlocks: function($matrixBlock, $matrixField) |
|
229
|
|
|
{ |
|
230
|
|
|
|
|
231
|
|
|
if ( !$matrixBlock.data('spooned') ) |
|
232
|
|
|
{ |
|
233
|
|
|
|
|
234
|
|
|
// Set this so we don’t re-run this |
|
235
|
|
|
$matrixBlock.data('spooned', true); |
|
236
|
|
|
|
|
237
|
|
|
// Get the cached spooned block types data for this whole field |
|
238
|
|
|
var spoonedBlockTypes = $matrixField.data('spoon-block-types'); |
|
239
|
|
|
|
|
240
|
|
|
// Check we have some config |
|
241
|
|
|
if ( typeof spoonedBlockTypes !== "undefined" && spoonedBlockTypes.length >= 1 ) |
|
242
|
|
|
{ |
|
243
|
|
|
|
|
244
|
|
|
// First, sort out the settings menu |
|
245
|
|
|
var $settingsBtn = $matrixBlock.find('.actions .settings.menubtn'); |
|
246
|
|
|
this.initSettingsMenu($settingsBtn, spoonedBlockTypes, $matrixField); |
|
247
|
|
|
|
|
248
|
|
|
// Second, get the current block’s type out of the dom so we can do the field layout |
|
249
|
|
|
var matrixBlockTypeHandle = this._getMatrixBlockTypeHandle($matrixBlock); |
|
250
|
|
|
|
|
251
|
|
|
// Further filter our spoonedBlockTypes array by the current block’s type |
|
252
|
|
|
var spoonedBlockType = $.grep(spoonedBlockTypes, function(e){ return e.matrixBlockType.handle === matrixBlockTypeHandle; }); |
|
253
|
|
|
|
|
254
|
|
|
// Initialize the field layout on the block |
|
255
|
|
|
if ( spoonedBlockType.length === 1 && spoonedBlockType[0].fieldLayoutId !== null ) |
|
256
|
|
|
{ |
|
257
|
|
|
$matrixBlock.data('spooned-block-type', spoonedBlockType[0]); |
|
258
|
|
|
this.initBlockFieldLayout($matrixBlock, $matrixField); |
|
259
|
|
|
} |
|
260
|
|
|
// If that failed, do another check against the global context |
|
261
|
|
|
else if (this.settings.blockTypes.hasOwnProperty('global')) |
|
262
|
|
|
{ |
|
263
|
|
|
var matrixFieldHandle = this._getMatrixFieldName($matrixField); |
|
264
|
|
|
spoonedBlockTypes = $.grep(this.settings.blockTypes['global'], function(e){ return e.fieldHandle === matrixFieldHandle; }); |
|
265
|
|
|
|
|
266
|
|
|
if ( spoonedBlockTypes.length >= 1 ) |
|
267
|
|
|
{ |
|
268
|
|
|
spoonedBlockType = $.grep(spoonedBlockTypes, function(e){ return e.matrixBlockType.handle === matrixBlockTypeHandle; }); |
|
269
|
|
|
|
|
270
|
|
|
if ( spoonedBlockType.length === 1 && spoonedBlockType[0].fieldLayoutId !== null ) |
|
271
|
|
|
{ |
|
272
|
|
|
$matrixBlock.data('spooned-block-type', spoonedBlockType[0]); |
|
273
|
|
|
this.initBlockFieldLayout($matrixBlock, $matrixField); |
|
274
|
|
|
} |
|
275
|
|
|
else |
|
276
|
|
|
{ |
|
277
|
|
|
$matrixBlock.addClass('matrixblock-not-spooned'); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
else |
|
281
|
|
|
{ |
|
282
|
|
|
$matrixBlock.addClass('matrixblock-not-spooned'); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
else |
|
286
|
|
|
{ |
|
287
|
|
|
$matrixBlock.addClass('matrixblock-not-spooned'); |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
} |
|
291
|
|
|
else |
|
292
|
|
|
{ |
|
293
|
|
|
$matrixBlock.addClass('matrixblock-not-spooned'); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
} |
|
297
|
|
|
else |
|
298
|
|
|
{ |
|
299
|
|
|
// Fixes Redactor |
|
300
|
|
|
Garnish.$doc.trigger('scroll'); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
}, |
|
304
|
|
|
|
|
305
|
|
|
initBlockFieldLayout: function($matrixBlock, $matrixField) |
|
306
|
|
|
{ |
|
307
|
|
|
|
|
308
|
|
|
var spoonedBlockType = $matrixBlock.data('spooned-block-type'), |
|
309
|
|
|
tabs = spoonedBlockType.fieldLayoutModel.tabs, |
|
310
|
|
|
fields = spoonedBlockType.fieldLayoutModel.fields; |
|
311
|
|
|
|
|
312
|
|
|
// Check we have some tabs |
|
313
|
|
|
// TODO: would be nice to not show the tab nav if there is only one tab |
|
314
|
|
|
if ( tabs.length >= 1 ) |
|
315
|
|
|
{ |
|
316
|
|
|
// Add a class so we can style |
|
317
|
|
|
$matrixBlock.addClass('matrixblock-spooned'); |
|
318
|
|
|
|
|
319
|
|
|
// Get a namespaced id |
|
320
|
|
|
var namespace = $matrixField.prop('id') + '-' + $matrixBlock.data('id'), |
|
321
|
|
|
spoonedNamespace = 'spoon-' + namespace; |
|
322
|
|
|
|
|
323
|
|
|
// Add the tabs container |
|
324
|
|
|
var $tabs = $('<ul class="spoon-tabs"/>').appendTo($matrixBlock); |
|
325
|
|
|
|
|
326
|
|
|
// Make our own fields container and hide the native one, but keep its height |
|
327
|
|
|
var $spoonedFields = $('<div class="spoon-fields"/>').css({ 'opacity' : 0 }).appendTo($matrixBlock), |
|
328
|
|
|
$fields = $matrixBlock.find('> .fields'); |
|
329
|
|
|
$fields.css({ 'opacity' : 0 }); |
|
330
|
|
|
|
|
331
|
|
|
// Loop the tabs |
|
332
|
|
|
for (var i = 0; i < tabs.length; i++) |
|
333
|
|
|
{ |
|
334
|
|
|
|
|
335
|
|
|
// Set up the first one to be active |
|
336
|
|
|
var navClasses = '', |
|
337
|
|
|
paneClasses = ''; |
|
338
|
|
|
|
|
339
|
|
|
if (i==0) |
|
|
|
|
|
|
340
|
|
|
{ |
|
341
|
|
|
navClasses = ' sel'; |
|
342
|
|
|
} |
|
343
|
|
|
else |
|
344
|
|
|
{ |
|
345
|
|
|
paneClasses = ' hidden'; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
// Add the tab nav, if there is more than one |
|
349
|
|
|
if (tabs.length > 1) |
|
350
|
|
|
{ |
|
351
|
|
|
var $tabLi = $('<li/>').appendTo($tabs), |
|
352
|
|
|
$tabA = $('<a id="'+spoonedNamespace+'-'+i+'" class="tab'+navClasses+'">'+Craft.t('site', tabs[i].name)+'</a>') |
|
353
|
|
|
.appendTo($tabLi) |
|
354
|
|
|
.data('spooned-tab-target', '#'+spoonedNamespace+'-pane-'+i); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
// Make a tab pane |
|
358
|
|
|
var $pane = $('<div id="'+spoonedNamespace+'-pane-'+i+'" class="'+paneClasses+'"/>').appendTo($spoonedFields); |
|
359
|
|
|
|
|
360
|
|
|
// Filter the fields array by their associated tabId and loop over them |
|
361
|
|
|
var tabFields = $.grep(fields, function(e){ return e.tabId === tabs[i].id; }); |
|
|
|
|
|
|
362
|
|
|
for (var n = 0; n < tabFields.length; n++) |
|
363
|
|
|
{ |
|
364
|
|
|
// Move the required field to our new container |
|
365
|
|
|
$fields.find('#' + namespace + '-fields-' + tabFields[n].handle + '-field').appendTo($pane); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
// Now check for errors and update the tab if needed |
|
369
|
|
|
if ($pane.find('.field.has-errors').length > 0) { |
|
370
|
|
|
$tabA.addClass('error'); |
|
|
|
|
|
|
371
|
|
|
$tabA.append(' <span data-icon="alert" />'); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
} |
|
375
|
|
|
|
|
376
|
|
|
// Bind events to tab nav clicks |
|
377
|
|
|
if (tabs.length > 1) |
|
378
|
|
|
{ |
|
379
|
|
|
this.addListener($tabs.find('a'), 'click', 'onTabClick'); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
// Force the fields to be removed from the layout |
|
383
|
|
|
$fields.hide(); |
|
384
|
|
|
|
|
385
|
|
|
$spoonedFields.velocity({opacity: 1}, 'fast', $.proxy(function() |
|
386
|
|
|
{ |
|
387
|
|
|
// Re-initialize the Craft UI |
|
388
|
|
|
Craft.initUiElements($spoonedFields); |
|
389
|
|
|
}, this)); |
|
390
|
|
|
|
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
}, |
|
394
|
|
|
|
|
395
|
|
|
onTabClick: function(ev) |
|
396
|
|
|
{ |
|
397
|
|
|
|
|
398
|
|
|
ev.preventDefault(); |
|
399
|
|
|
ev.stopPropagation(); |
|
400
|
|
|
|
|
401
|
|
|
var $tab = $(ev.target), |
|
402
|
|
|
$tabNav = $tab.parent().parent('.spoon-tabs'), |
|
403
|
|
|
targetSelector = $tab.data('spooned-tab-target'), |
|
404
|
|
|
$target = $(targetSelector); |
|
405
|
|
|
|
|
406
|
|
|
// Toggle tab nav state |
|
407
|
|
|
$tabNav.find('a.sel').removeClass('sel'); |
|
408
|
|
|
$tab.addClass('sel'); |
|
409
|
|
|
|
|
410
|
|
|
// Toggle the pane state |
|
411
|
|
|
$target.siblings('div').addClass('hidden'); |
|
412
|
|
|
$target.removeClass('hidden'); |
|
413
|
|
|
|
|
414
|
|
|
}, |
|
415
|
|
|
|
|
416
|
|
|
initSettingsMenu: function($settingsBtn, spoonedBlockTypes, $matrixField) |
|
417
|
|
|
{ |
|
418
|
|
|
Garnish.requestAnimationFrame($.proxy(function() |
|
419
|
|
|
{ |
|
420
|
|
|
// Get the Garnish.MenuBtn object |
|
421
|
|
|
var menuBtn = $settingsBtn.data('menubtn') || false; |
|
422
|
|
|
|
|
423
|
|
|
// If there wasn’t one then fail and try again |
|
424
|
|
|
if (!menuBtn) |
|
425
|
|
|
{ |
|
426
|
|
|
this.initSettingsMenu($settingsBtn, spoonedBlockTypes, $matrixField); |
|
427
|
|
|
return; |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
// Get the field handle |
|
431
|
|
|
var matrixFieldHandle = this._getMatrixFieldName($matrixField); |
|
432
|
|
|
|
|
433
|
|
|
// Get the actual menu out of it once we get this far |
|
434
|
|
|
var $menu = menuBtn.menu.$container; |
|
435
|
|
|
$menu.addClass('spoon-settings-menu'); |
|
436
|
|
|
|
|
437
|
|
|
// Hide all the li’s with add block links in them |
|
438
|
|
|
$menu.find('a[data-action="add"]').parents('li').addClass('hidden'); |
|
439
|
|
|
|
|
440
|
|
|
// Remove all the padded classes on hr’s |
|
441
|
|
|
$menu.find('hr').removeClass('padded'); |
|
442
|
|
|
|
|
443
|
|
|
// Get the correct ul to play with in the menu container |
|
444
|
|
|
var $origUl = $menu.find('a[data-action="add"]').parents('li').parent('ul'); |
|
445
|
|
|
|
|
446
|
|
|
// Loop the given block type data and adjust the menu to match the groups |
|
447
|
|
|
for (var i = 0; i < spoonedBlockTypes.length; i++) |
|
448
|
|
|
{ |
|
449
|
|
|
var handle = spoonedBlockTypes[i].matrixBlockType.handle; |
|
450
|
|
|
|
|
451
|
|
|
// Make a new group ul if needed |
|
452
|
|
|
if ( $menu.find('[data-spooned-group="'+spoonedBlockTypes[i].groupName+'"]').length === 0 ) |
|
453
|
|
|
{ |
|
454
|
|
|
var nestedSettingsHandles = $.grep(this.settings.nestedSettingsHandles, function(a){ return a === matrixFieldHandle; }); |
|
455
|
|
|
if (nestedSettingsHandles.length) { |
|
456
|
|
|
var $newUl = $('<ul class="padded hidden" data-spooned-group="'+spoonedBlockTypes[i].groupName+'" />'); |
|
457
|
|
|
if (i!==0) |
|
458
|
|
|
{ |
|
459
|
|
|
$('<hr/>').insertBefore($origUl); |
|
460
|
|
|
} |
|
461
|
|
|
|
|
462
|
|
|
var $groupHeading = $('<a class="fieldtoggle">' + Craft.t('site', spoonedBlockTypes[i].groupName) + '</a>'); |
|
463
|
|
|
$groupHeading.insertBefore($origUl); |
|
464
|
|
|
|
|
465
|
|
|
$newUl.insertBefore($origUl); |
|
466
|
|
|
|
|
467
|
|
|
this.addListener($groupHeading, 'click', function(event) { |
|
468
|
|
|
var $trigger = $(event.currentTarget), |
|
469
|
|
|
$target = $trigger.next('ul'); |
|
470
|
|
|
|
|
471
|
|
|
if ($target.hasClass('hidden')) { |
|
472
|
|
|
$target.removeClass('hidden'); |
|
473
|
|
|
$trigger.addClass('expanded'); |
|
474
|
|
|
} else { |
|
475
|
|
|
$target.addClass('hidden'); |
|
476
|
|
|
$trigger.removeClass('expanded'); |
|
477
|
|
|
} |
|
478
|
|
|
}); |
|
479
|
|
|
} else { |
|
480
|
|
|
var $newUl = $('<ul class="padded" data-spooned-group="'+spoonedBlockTypes[i].groupName+'" />'); |
|
|
|
|
|
|
481
|
|
|
if (i!==0) |
|
482
|
|
|
{ |
|
483
|
|
|
$('<hr/>').insertBefore($origUl); |
|
484
|
|
|
} |
|
485
|
|
|
$('<h6>' + Craft.t('site', spoonedBlockTypes[i].groupName) + '</h6>').insertBefore($origUl); |
|
486
|
|
|
$newUl.insertBefore($origUl); |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
// Add the li |
|
492
|
|
|
var $li = $menu.find('a[data-type="'+handle+'"]').parents('li'); |
|
493
|
|
|
|
|
494
|
|
|
$newUl.append($li); |
|
|
|
|
|
|
495
|
|
|
$li.removeClass('hidden'); |
|
496
|
|
|
} |
|
497
|
|
|
|
|
498
|
|
|
}, this)); |
|
499
|
|
|
}, |
|
500
|
|
|
|
|
501
|
|
|
/** |
|
502
|
|
|
* This simply returns a fieldHandle if it can get one or false if not |
|
503
|
|
|
*/ |
|
504
|
|
|
_getMatrixFieldName: function($matrixField) |
|
505
|
|
|
{ |
|
506
|
|
|
|
|
507
|
|
|
var matrixFieldId = $matrixField.parentsUntil('.field').parent().prop('id'), |
|
508
|
|
|
parts = matrixFieldId.split("-"); |
|
509
|
|
|
|
|
510
|
|
|
// Matrix inside Something (e.g. Super Table) inside Matrix |
|
511
|
|
|
if (parts.length === 9) { |
|
512
|
|
|
var matrixFieldHandle = parts[parts.length-8] + '-' + parts[parts.length-5] + '-' + parts[parts.length-2]; |
|
513
|
|
|
} |
|
514
|
|
|
// Matrix inside Something (e.g. Super Table) |
|
515
|
|
|
else if (parts.length === 6) { |
|
516
|
|
|
var matrixFieldHandle = parts[parts.length-5] + '-' + parts[parts.length-2]; |
|
|
|
|
|
|
517
|
|
|
} |
|
518
|
|
|
// Normal Matrix |
|
519
|
|
|
else if (parts.length === 3) { |
|
520
|
|
|
var matrixFieldHandle = parts[parts.length-2]; |
|
|
|
|
|
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
if ( matrixFieldHandle != '' ) |
|
|
|
|
|
|
524
|
|
|
{ |
|
525
|
|
|
return matrixFieldHandle; |
|
526
|
|
|
} |
|
527
|
|
|
else |
|
528
|
|
|
{ |
|
|
|
|
|
|
529
|
|
|
return false; |
|
530
|
|
|
} |
|
531
|
|
|
}, |
|
532
|
|
|
|
|
533
|
|
|
/** |
|
534
|
|
|
* Returns the block type handle for a given $matrixBlock |
|
535
|
|
|
*/ |
|
536
|
|
|
_getMatrixBlockTypeHandle: function($matrixBlock) |
|
537
|
|
|
{ |
|
538
|
|
|
var blockTypeHandle = $matrixBlock.find('> input[type="hidden"][name*="type"]').val(); |
|
539
|
|
|
|
|
540
|
|
|
if ( typeof blockTypeHandle == 'string' ) |
|
541
|
|
|
{ |
|
542
|
|
|
return blockTypeHandle; |
|
543
|
|
|
} |
|
544
|
|
|
else |
|
545
|
|
|
{ |
|
|
|
|
|
|
546
|
|
|
return false; |
|
547
|
|
|
} |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
}, |
|
551
|
|
|
{ |
|
552
|
|
|
defaults: { |
|
553
|
|
|
blockTypes: null, |
|
554
|
|
|
context: false, |
|
555
|
|
|
nestedSettingsHandles: [] |
|
556
|
|
|
} |
|
557
|
|
|
}); |
|
558
|
|
|
|
|
559
|
|
|
})(jQuery); |
|
560
|
|
|
|