54d583525b5e11d8531d740517d9b55e1af92641
[plstackapi.git] / planetstack / core / xoslib / static / js / test.js
1 TestApp = new Marionette.Application();
2
3 TestApp.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",
13     userList: "#userList",
14     detail: "#detail",
15     linkedObjs1: "#linkedObjs1",
16     linkedObjs2: "#linkedObjs2",
17     linkedObjs3: "#linkedObjs3",
18     linkedObjs4: "#linkedObjs4"
19 });
20
21 TestApp.hideError = function(result) {
22     $("#errorBox").hide();
23     $("#successBox").hide();
24 };
25
26 TestApp.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      });
32 };
33
34 TestApp.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
42 idToName = 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
51 TestApp.on("start", function() {
52      var objs = ['deployment', 'image', 'networkTemplate', 'network', 'networkSliver', 'node', 'service', 'site', 'slice', 'sliceDeployment', 'slicePrivilege', 'sliver', 'user', 'sliceRole', 'userDeployment'];
53
54      for (var index in objs) {
55          name = objs[index];
56          tr_template = '#xosAdmin-' + name + '-listitem-template';
57          table_template = '#xosAdmin-' + name + '-list-template';
58          detail_template = '#xosAdmin-' + name + '-detail-template';
59          collection_name = name + "s";
60          region_name = name + "List";
61
62          detailClass = Marionette.ItemView.extend({
63             template: detail_template,\r
64             tagName: 'div',\r
65 \r
66             events: {"click button.js-submit": "submitClicked",\r
67                      "change input": "inputChanged"},\r
68 \r
69             /* inputChanged is watching the onChange events of the input controls. We\r
70                do this to track when this view is 'dirty', so we can throw up a warning\r
71                if the user tries to change his slices without saving first.\r
72             */\r
73 \r
74             inputChanged: function(e) {\r
75                 this.dirty = true;\r
76             },\r
77 \r
78             saveError: function(model, result, xhr) {\r
79                 TestApp.showError(result);\r
80             },\r
81 \r
82             saveSuccess: function(model, result, xhr) {\r
83                 TestApp.showSuccess({status: xhr.xhr.status, statusText: xhr.xhr.statusText});\r
84             },\r
85 \r
86             submitClicked: function(e) {\r
87                 TestApp.hideError();\r
88                 e.preventDefault();\r
89                 var data = Backbone.Syphon.serialize(this);\r
90                 var thisView = this;\r
91                 this.model.save(data, {error: function(model, result, xhr) { thisView.saveError(model, result, xhr); },\r
92                                        success: function(model, result, xhr) { thisView.saveSuccess(model, result, xhr); }});\r
93                 this.dirty = false;\r
94             },\r
95          });
96
97          itemViewClass = Marionette.ItemView.extend({
98              detailClass: detailClass,
99              template: tr_template,
100              tagName: 'tr',
101              className: 'test-tablerow',
102
103              events: {"click": "changeItem"},
104 \r
105              changeItem: function(e) {\r
106                     TestApp.hideError();\r
107                     e.preventDefault();\r
108                     e.stopPropagation();\r
109 \r
110                     index=0;\r
111                     for (relatedName in this.model.collection.relatedCollections) {\r
112                         relatedField = this.model.collection.relatedCollections[relatedName];\r
113 \r
114                         relatedListViewClass = TestApp[relatedName + "ListView"].extend({collection: xos[relatedName].filterBy(relatedField,this.model.id)});\r
115                         TestApp["linkedObjs" + (index+1)].show(new relatedListViewClass());\r
116                         index = index + 1;\r
117                     }\r
118 \r
119                     while (index<4) {\r
120                         TestApp["linkedObjs" + (index+1)].empty();\r
121                         index = index + 1;\r
122                     }\r
123 \r
124                     var detailView = new this.detailClass({\r
125                         model: this.model,\r
126                     });\r
127                     $('#detailBox').show();\r
128                     TestApp.detail.show(detailView);\r
129               },
130          });
131
132          listViewClass = Marionette.CompositeView.extend({
133              childView: itemViewClass,
134              childViewContainer: 'tbody',
135              template: table_template,
136              collection: xos[collection_name],
137              title: name + "s",
138
139              initialize: function() {
140                  this.listenTo(this.collection, 'change', this._renderChildren)
141
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];
147                      if (xos[foreignName] == undefined) {
148                          console.log("Failed to find xos class " + foreignName);
149                      }
150                      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 };
157              },
158          });
159          TestApp[collection_name + "ListView"] = listViewClass;
160
161          var listView = new listViewClass();
162
163          if (region_name in TestApp.getRegions()) {
164              TestApp[region_name].show(listView);
165          }
166          xos[collection_name].fetch(); //startPolling();
167      }
168
169      $('#close-detail-view').unbind().bind('click', function() {
170          $('#detailBox').hide();
171      });
172 });
173
174 $(document).ready(function(){
175     TestApp.start();
176 });
177