blob: 76d131c82d487499054a0b2623a267b6f3a94a86 [file] [log] [blame]
Scott Bakere41c9082014-10-27 23:02:48 -07001TestApp = new Marionette.Application();
2
3TestApp.addRegions({
4 deploymentList: "#deploymentList",
5 imageList: "#imageList",
6 networkTemplateList: "#networkTemplateList",
7 networkList: "#networkList",
8 nodeList: "#nodeList",
9 serviceList: "#serviceList",
10 siteList: "#siteList",
11 sliceList: "#sliceList",
12 sliverList: "#sliverList",
Scott Baker3df41942014-10-28 12:44:13 -070013 userList: "#userList",
Scott Baker4aee9a12014-10-30 00:06:16 -070014 detail: "#detail",
15 linkedObjs1: "#linkedObjs1",
16 linkedObjs2: "#linkedObjs2",
17 linkedObjs3: "#linkedObjs3",
18 linkedObjs4: "#linkedObjs4"
Scott Bakere41c9082014-10-27 23:02:48 -070019});
20
Scott Bakerd044c8f2014-10-28 14:46:13 -070021TestApp.hideError = function(result) {
22 $("#errorBox").hide();
Scott Baker33c84ac2014-10-28 21:40:20 -070023 $("#successBox").hide();
24};
25
26TestApp.showSuccess = function(result) {
27 $("#successBox").show();
28 $("#successBox").html(_.template($("#test-success-template").html())(result));
29 $('#close-success-box').unbind().bind('click', function() {
30 $('#successBox').hide();
31 });
Scott Bakerd044c8f2014-10-28 14:46:13 -070032};
33
34TestApp.showError = function(result) {
35 $("#errorBox").show();
36 $("#errorBox").html(_.template($("#test-error-template").html())(result));
37 $('#close-error-box').unbind().bind('click', function() {
38 $('#errorBox').hide();
39 });
40};
41
Scott Baker4aee9a12014-10-30 00:06:16 -070042idToName = function(id, collectionName, fieldName) {
43 linkedObject = xos[collectionName].get(id);
44 if (linkedObject == undefined) {
45 return "#" + id;
46 } else {
47 return linkedObject.attributes[fieldName];
48 }
49};
50
Scott Bakere41c9082014-10-27 23:02:48 -070051TestApp.on("start", function() {
Scott Baker4aee9a12014-10-30 00:06:16 -070052 var objs = ['deployment', 'image', 'networkTemplate', 'network', 'networkSliver', 'node', 'service', 'site', 'slice', 'sliceDeployment', 'slicePrivilege', 'sliver', 'user', 'sliceRole'];
Scott Bakere41c9082014-10-27 23:02:48 -070053
54 for (var index in objs) {
55 name = objs[index];
Scott Baker013025a2014-10-30 23:50:20 -070056 tr_template = '#xosAdmin-' + name + '-listitem-template';
57 table_template = '#xosAdmin-' + name + '-list-template';
58 detail_template = '#xosAdmin-' + name + '-detail-template';
Scott Bakere41c9082014-10-27 23:02:48 -070059 collection_name = name + "s";
60 region_name = name + "List";
61
Scott Baker3df41942014-10-28 12:44:13 -070062 detailClass = Marionette.ItemView.extend({
63 template: detail_template,
64 tagName: 'div',
65
66 events: {"click button.js-submit": "submitClicked",
67 "change input": "inputChanged"},
68
69 /* inputChanged is watching the onChange events of the input controls. We
70 do this to track when this view is 'dirty', so we can throw up a warning
71 if the user tries to change his slices without saving first.
72 */
73
74 inputChanged: function(e) {
75 this.dirty = true;
76 },
77
Scott Bakerd044c8f2014-10-28 14:46:13 -070078 saveError: function(model, result, xhr) {
79 TestApp.showError(result);
80 },
81
Scott Baker33c84ac2014-10-28 21:40:20 -070082 saveSuccess: function(model, result, xhr) {
83 TestApp.showSuccess({status: xhr.xhr.status, statusText: xhr.xhr.statusText});
84 },
85
Scott Baker3df41942014-10-28 12:44:13 -070086 submitClicked: function(e) {
Scott Bakerd044c8f2014-10-28 14:46:13 -070087 TestApp.hideError();
Scott Baker3df41942014-10-28 12:44:13 -070088 e.preventDefault();
89 var data = Backbone.Syphon.serialize(this);
Scott Bakerd044c8f2014-10-28 14:46:13 -070090 var thisView = this;
Scott Baker33c84ac2014-10-28 21:40:20 -070091 this.model.save(data, {error: function(model, result, xhr) { thisView.saveError(model, result, xhr); },
92 success: function(model, result, xhr) { thisView.saveSuccess(model, result, xhr); }});
Scott Baker3df41942014-10-28 12:44:13 -070093 this.dirty = false;
94 },
95 });
96
Scott Bakere41c9082014-10-27 23:02:48 -070097 itemViewClass = Marionette.ItemView.extend({
Scott Baker3df41942014-10-28 12:44:13 -070098 detailClass: detailClass,
Scott Bakere41c9082014-10-27 23:02:48 -070099 template: tr_template,
100 tagName: 'tr',
101 className: 'test-tablerow',
Scott Baker3df41942014-10-28 12:44:13 -0700102
103 events: {"click": "changeItem"},
104
105 changeItem: function(e) {
Scott Bakerd044c8f2014-10-28 14:46:13 -0700106 TestApp.hideError();
Scott Baker3df41942014-10-28 12:44:13 -0700107 e.preventDefault();
108 e.stopPropagation();
109
Scott Baker4aee9a12014-10-30 00:06:16 -0700110 index=0;
111 for (relatedName in this.model.collection.relatedCollections) {
112 relatedField = this.model.collection.relatedCollections[relatedName];
113
114 relatedListViewClass = TestApp[relatedName + "ListView"].extend({collection: xos[relatedName].filterBy(relatedField,this.model.id)});
115 TestApp["linkedObjs" + (index+1)].show(new relatedListViewClass());
116 index = index + 1;
117 }
118
119 while (index<4) {
120 TestApp["linkedObjs" + (index+1)].empty();
121 index = index + 1;
122 }
123
Scott Baker3df41942014-10-28 12:44:13 -0700124 var detailView = new this.detailClass({
125 model: this.model,
126 });
127 $('#detailBox').show();
128 TestApp.detail.show(detailView);
129 },
Scott Bakere41c9082014-10-27 23:02:48 -0700130 });
131
132 listViewClass = Marionette.CompositeView.extend({
133 childView: itemViewClass,
134 childViewContainer: 'tbody',
135 template: table_template,
136 collection: xos[collection_name],
Scott Baker4aee9a12014-10-30 00:06:16 -0700137 title: name + "s",
Scott Bakere41c9082014-10-27 23:02:48 -0700138
139 initialize: function() {
140 this.listenTo(this.collection, 'change', this._renderChildren)
Scott Baker4aee9a12014-10-30 00:06:16 -0700141
142 // Because many of the templates use idToName(), we need to
143 // listen to the collections that hold the names for the ids
144 // that we want to display.
145 for (i in this.collection.foreignCollections) {
146 foreignName = this.collection.foreignCollections[i];
Scott Baker013025a2014-10-30 23:50:20 -0700147 if (xos[foreignName] == undefined) {
148 console.log("Failed to find xos class " + foreignName);
149 }
Scott Baker4aee9a12014-10-30 00:06:16 -0700150 this.listenTo(xos[foreignName], 'change', this._renderChildren);
151 this.listenTo(xos[foreignName], 'sort', this._renderChildren);
152 }
153 },
154
155 templateHelpers: function() {
156 return { title: this.title };
Scott Bakere41c9082014-10-27 23:02:48 -0700157 },
158 });
Scott Baker4aee9a12014-10-30 00:06:16 -0700159 TestApp[collection_name + "ListView"] = listViewClass;
Scott Bakere41c9082014-10-27 23:02:48 -0700160
161 var listView = new listViewClass();
162
Scott Baker4aee9a12014-10-30 00:06:16 -0700163 if (region_name in TestApp.getRegions()) {
164 TestApp[region_name].show(listView);
165 }
Scott Baker33c84ac2014-10-28 21:40:20 -0700166 xos[collection_name].fetch(); //startPolling();
Scott Bakere41c9082014-10-27 23:02:48 -0700167 }
Scott Baker3df41942014-10-28 12:44:13 -0700168
Scott Bakerd044c8f2014-10-28 14:46:13 -0700169 $('#close-detail-view').unbind().bind('click', function() {
Scott Baker3df41942014-10-28 12:44:13 -0700170 $('#detailBox').hide();
171 });
Scott Bakere41c9082014-10-27 23:02:48 -0700172});
173
174$(document).ready(function(){
Scott Baker013025a2014-10-30 23:50:20 -0700175 TestApp.start();
Scott Bakere41c9082014-10-27 23:02:48 -0700176});
177