-
Notifications
You must be signed in to change notification settings - Fork 1
/
params.js
133 lines (121 loc) · 4.48 KB
/
params.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
;(function($) {
$.paramsForm = {
defaults : {
formContainer: '#params-form-container',
button: '#params-form-edit-button',
showForm: true,
showButton: true,
useShowFormParam: false,
onFormToggle: function(settings){},
onFormReady: function(settings){}
},
/** Creates the params form and attaches it to the button toggle. */
create : function(formData, options) {
var settings = $.extend({}, $.paramsForm.defaults, options);
function _toggleForm() {
$(settings.formContainer).toggle();
settings.onFormToggle(settings);
}
if (!settings.showForm) {
_toggleForm();
}
// uses the dform jquery plugin to create the form
$(settings.formContainer).buildForm(formData);
// shows button to display form
// creates action for 'submit' button (not a true submit button)
$('#page-reload-button').on('click', function() {
var formValues = $(settings.formContainer).children().serializeArray();
// ensures it does not send data about unfilled fields
var formValuesFiltered = $.grep(formValues, function(e) {return e.name && e.value;});
// uses location.assign since that forces a page reload
window.location.assign(window.location.pathname + '?' + $.param(formValuesFiltered));
});
$(settings.button).on('click', _toggleForm);
settings.onFormReady(settings);
}
};
// A new select subscriber for dform that takes in a list instead of a hash
// (so that we can maintain the given order of the options)
$.dform.subscribe('options-ordered',
function(options, type) {
var scoper = $(this);
if (type == 'select') {
$.each(options, function(i, name) {
var option = { type : 'option', value: name, html: name };
$(scoper).formElement(option);
});
}
}
);
})(jQuery);
;(function($) {
// Transform formData for use with bootstrap subscribers
var _bootstrapify = function(formData) {
var elts = formData.elements;
for (var i = 0; i < elts.length; i++) {
if(elts[i].type == 'submit')
elts[i].type += '-bootstrap';
if(elts[i].caption) {
elts[i]['caption-bootstrap'] = elts[i].caption;
delete elts[i]['caption'];
}
}
};
$.bootstrapParamsForm = {
defaults: {
onFormReady: function(settings) {
// makes the button show up slowly so it's obvious
$($.paramsForm.defaults.button).toggleClass('no-vertical-padding').animate({height: '20'}, 200);
},
onFormToggle: function(settings) {
// makes the button more obvious when the form is hidden
$(settings.button).toggleClass('primary');
},
formContainerHtml: '<div id="params-form-container" class="well" style="padding:15px"></div>',
buttonHtml: '<input id="params-form-edit-button" type="submit" class="btn" value="Edit page parameters"/>',
prependTo: 'body'
},
/** Creates the params form and button with reasonable bootstrap defaults. */
create: function(formData, options) {
var settings = $.extend({}, $.bootstrapParamsForm.defaults, options);
$(settings.prependTo).prepend(settings.formContainerHtml, settings.buttonHtml);
_bootstrapify(formData);
$.paramsForm.create(formData, settings);
}
};
// Bootstrap friendly versions of form subscribers for dform
$.dform.addType('submit-bootstrap',
function(options) {
return $('<div class="clearfix"><div class="input"><input id="page-reload-button" type="button" class="btn primary no-vertical-padding" value="Reload"/></div></div>');
}
);
$.dform.subscribe('caption-bootstrap',
function(options, type) {
var ops = {};
if (typeof (options) == 'string')
ops['html'] = options;
else
$.extend(ops, options);
if (type == 'fieldset') {
// Labels for fieldsets are legend
ops.type = 'legend';
var legend = $.dform.createElement(ops);
this.prepend(legend);
$(legend).runAll(ops);
} else {
this.wrap('<div class="clearfix">');
ops.type = 'label';
if (this.attr('id'))
ops['for'] = this.attr('id');
var label = $.dform.createElement(ops);
if (type == 'radio') {
this.parent().append($(label));
} else {
$(label).insertBefore($(this));
}
$(label).runAll(ops);
this.wrap('<div class="input">');
}
}
);
})(jQuery);