-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabletop-edit.js
90 lines (74 loc) · 3.07 KB
/
tabletop-edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
(function ($) {
Drupal.tabletop = [];
Drupal.behaviors.tabletopEdit = {
attach: function(context, settings) {
$('.edit-tabletop').each(function(i, field) {
// Replace the 'sheet' text field with a select widget.
var select = $('<select class="form-select"></select>')
.change({ 'field': field }, Drupal.behaviors.tabletopEdit.changeSelect);
$('.tabletop-sheet', field).after(select);
// Instantiate Tabletop objects.
$('input.tabletop-key', field).change(function(e) {
// Empty the sheet select widget.
$(select).empty();
// Empty the dynamic template help.
$('.tabletop-model-columns', field).empty();
if (e.target.value) {
var table = Tabletop.init({
key: e.target.value,
callback: function() {
var table = $(field).data('table');
if (table) {
// Build new options with sheets from the updated table.
$.each(table.model_names, function(i, name) {
$(select).append('<option value="' + name + '">' + name + '</option>');
});
// If there's a sheet that matches the current sheet name, select it.
var sheetName = $('.tabletop-sheet', field).val();
$('option[value="' + sheetName + '"]').attr('selected', 'selected');
// Trigger the change event on the select widget so that column names update
$(select).change();
}
}
});
// Store the Tabletop object on the field element.
$(field).data('table', table);
}
})
// Trigger the change event on the key field so that the select widget and template hints are populated on load.
.change();
});
},
changeSelect: function(e) {
var field = $(e.data.field);
var selected = e.target.value;
if (selected) {
$('.tabletop-sheet', field).val(selected);
}
var table = $(field).data('table');
if (table && selected) {
var sheet = table.sheets(selected);
if (sheet) {
// Set help text.
$('.tabletop-model-columns', field).html('Your column names are: <code></code>');
$.each(sheet.column_names, function(i, col) {
$('.tabletop-model-columns code', field).append('{{' + col + '}} ');
});
// Build default text field value.
var newDefaultValue = '{{#each data}}';
$.each(sheet.column_names, function(i, col) {
newDefaultValue += "\n {{" + col + "}}";
});
newDefaultValue += "\n{{/each}}";
// Set default.
var $template = $('.tabletop-template', field);
var defaultValue = $template.data('defaultValue');
if (!$template.val() || $template.val() == defaultValue) {
$template.val(newDefaultValue);
}
$template.data('defaultValue', newDefaultValue);
}
}
}
}
})(jQuery);