1
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
2
// use this file except in compliance with the License. You may obtain a copy of
5
// http://www.apache.org/licenses/LICENSE-2.0
7
// Unless required by applicable law or agreed to in writing, software
8
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
// License for the specific language governing permissions and limitations under
18
function (app, FauxtonAPI) {
20
var Config = FauxtonAPI.addon();
22
Config.Model = Backbone.Model.extend({});
23
Config.OptionModel = Backbone.Model.extend({
26
return app.host + '/_config/' + this.get("section") + '/' + this.get("name");
29
isNew: function () { return false; },
31
sync: function (method, model, options) {
35
contentType: 'application/json',
37
data: JSON.stringify(model.get('value'))
40
if (method === 'delete') {
41
params.type = 'DELETE';
46
return $.ajax(params);
50
Config.Collection = Backbone.Collection.extend({
54
return app.host + '/_config';
57
parse: function (resp) {
58
return _.map(resp, function (section, section_name) {
60
section: section_name,
61
options: _.map(section, function (option, option_name) {
72
Config.ViewItem = FauxtonAPI.View.extend({
74
className: "config-item",
75
template: "addons/config/templates/item",
78
"click .edit-button": "editValue",
79
"click #delete-value": "deleteValue",
80
"click #cancel-value": "cancelEdit",
81
"click #save-value": "saveValue"
84
deleteValue: function (event) {
85
var result = confirm("Are you sure you want to delete this configuration value?");
87
if (!result) { return; }
93
editValue: function (event) {
94
this.$("#show-value").hide();
95
this.$("#edit-value-form").show();
98
saveValue: function (event) {
99
this.model.save({value: this.$(".value-input").val()});
103
cancelEdit: function (event) {
104
this.$("#edit-value-form").hide();
105
this.$("#show-value").show();
108
serialize: function () {
109
return {option: this.model.toJSON()};
114
Config.View = FauxtonAPI.View.extend({
115
template: "addons/config/templates/dashboard",
118
"click #add-section": "addSection",
119
"submit #add-section-form": "submitForm"
122
submitForm: function (event) {
123
event.preventDefault();
124
var option = new Config.OptionModel({
125
section: this.$('input[name="section"]').val(),
126
name: this.$('input[name="name"]').val(),
127
value: this.$('input[name="value"]').val()
132
var section = this.collection.find(function (section) {
133
return section.get("section") === option.get("section");
137
section.get("options").push(option.attributes);
139
this.collection.add({
140
section: option.get("section"),
141
options: [option.attributes]
145
this.$("#add-section-modal").modal('hide');
149
addSection: function (event) {
150
event.preventDefault();
151
this.$("#add-section-modal").modal({show:true});
154
beforeRender: function() {
155
this.collection.each(function(config) {
156
_.each(config.get("options"), function (option, index) {
157
this.insertView("table.config tbody", new Config.ViewItem({
158
model: new Config.OptionModel({
159
section: config.get("section"),
169
establish: function() {
170
return [this.collection.fetch()];