blob: c0f4146bfff6fb9ac3644277b3503f38432910e0 [file] [log] [blame]
Scott Bakerfdaee922014-11-03 09:43:23 -08001TestApp = new XOSApplication();
Scott Bakere41c9082014-10-27 23:02:48 -07002
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
21TestApp.on("start", function() {
Scott Baker586878e2014-10-31 16:43:07 -070022 var objs = ['deployment', 'image', 'networkTemplate', 'network', 'networkSliver', 'networkDeployment', 'node', 'service', 'site', 'slice', 'sliceDeployment', 'slicePrivilege', 'sliver', 'user', 'sliceRole', 'userDeployment'];
Scott Bakere41c9082014-10-27 23:02:48 -070023
24 for (var index in objs) {
25 name = objs[index];
Scott Baker013025a2014-10-30 23:50:20 -070026 tr_template = '#xosAdmin-' + name + '-listitem-template';
27 table_template = '#xosAdmin-' + name + '-list-template';
28 detail_template = '#xosAdmin-' + name + '-detail-template';
Scott Bakere41c9082014-10-27 23:02:48 -070029 collection_name = name + "s";
30 region_name = name + "List";
31
Scott Baker3df41942014-10-28 12:44:13 -070032 detailClass = Marionette.ItemView.extend({
33 template: detail_template,
34 tagName: 'div',
35
36 events: {"click button.js-submit": "submitClicked",
37 "change input": "inputChanged"},
38
39 /* inputChanged is watching the onChange events of the input controls. We
40 do this to track when this view is 'dirty', so we can throw up a warning
41 if the user tries to change his slices without saving first.
42 */
43
44 inputChanged: function(e) {
45 this.dirty = true;
46 },
47
Scott Bakerd044c8f2014-10-28 14:46:13 -070048 saveError: function(model, result, xhr) {
49 TestApp.showError(result);
50 },
51
Scott Baker33c84ac2014-10-28 21:40:20 -070052 saveSuccess: function(model, result, xhr) {
53 TestApp.showSuccess({status: xhr.xhr.status, statusText: xhr.xhr.statusText});
54 },
55
Scott Baker3df41942014-10-28 12:44:13 -070056 submitClicked: function(e) {
Scott Bakerd044c8f2014-10-28 14:46:13 -070057 TestApp.hideError();
Scott Baker3df41942014-10-28 12:44:13 -070058 e.preventDefault();
59 var data = Backbone.Syphon.serialize(this);
Scott Bakerd044c8f2014-10-28 14:46:13 -070060 var thisView = this;
Scott Baker33c84ac2014-10-28 21:40:20 -070061 this.model.save(data, {error: function(model, result, xhr) { thisView.saveError(model, result, xhr); },
62 success: function(model, result, xhr) { thisView.saveSuccess(model, result, xhr); }});
Scott Baker3df41942014-10-28 12:44:13 -070063 this.dirty = false;
64 },
65 });
66
Scott Bakere41c9082014-10-27 23:02:48 -070067 itemViewClass = Marionette.ItemView.extend({
Scott Baker3df41942014-10-28 12:44:13 -070068 detailClass: detailClass,
Scott Bakere41c9082014-10-27 23:02:48 -070069 template: tr_template,
70 tagName: 'tr',
71 className: 'test-tablerow',
Scott Baker3df41942014-10-28 12:44:13 -070072
73 events: {"click": "changeItem"},
74
75 changeItem: function(e) {
Scott Bakerd044c8f2014-10-28 14:46:13 -070076 TestApp.hideError();
Scott Baker3df41942014-10-28 12:44:13 -070077 e.preventDefault();
78 e.stopPropagation();
79
Scott Baker4aee9a12014-10-30 00:06:16 -070080 index=0;
81 for (relatedName in this.model.collection.relatedCollections) {
82 relatedField = this.model.collection.relatedCollections[relatedName];
83
Scott Baker586878e2014-10-31 16:43:07 -070084 relatedListViewClassName = relatedName + "ListView";
85 if (TestApp[relatedListViewClassName] == undefined) {
86 console.log("warning: " + relatedListViewClassName + " not found");
87 }
88 relatedListViewClass = TestApp[relatedListViewClassName].extend({collection: xos[relatedName].filterBy(relatedField,this.model.id)});
Scott Baker4aee9a12014-10-30 00:06:16 -070089 TestApp["linkedObjs" + (index+1)].show(new relatedListViewClass());
90 index = index + 1;
91 }
92
93 while (index<4) {
94 TestApp["linkedObjs" + (index+1)].empty();
95 index = index + 1;
96 }
97
Scott Baker3df41942014-10-28 12:44:13 -070098 var detailView = new this.detailClass({
99 model: this.model,
100 });
101 $('#detailBox').show();
102 TestApp.detail.show(detailView);
103 },
Scott Bakere41c9082014-10-27 23:02:48 -0700104 });
105
106 listViewClass = Marionette.CompositeView.extend({
107 childView: itemViewClass,
108 childViewContainer: 'tbody',
109 template: table_template,
110 collection: xos[collection_name],
Scott Baker4aee9a12014-10-30 00:06:16 -0700111 title: name + "s",
Scott Bakere41c9082014-10-27 23:02:48 -0700112
113 initialize: function() {
114 this.listenTo(this.collection, 'change', this._renderChildren)
Scott Baker4aee9a12014-10-30 00:06:16 -0700115
116 // Because many of the templates use idToName(), we need to
117 // listen to the collections that hold the names for the ids
118 // that we want to display.
119 for (i in this.collection.foreignCollections) {
120 foreignName = this.collection.foreignCollections[i];
Scott Baker013025a2014-10-30 23:50:20 -0700121 if (xos[foreignName] == undefined) {
122 console.log("Failed to find xos class " + foreignName);
123 }
Scott Baker4aee9a12014-10-30 00:06:16 -0700124 this.listenTo(xos[foreignName], 'change', this._renderChildren);
125 this.listenTo(xos[foreignName], 'sort', this._renderChildren);
126 }
127 },
128
129 templateHelpers: function() {
130 return { title: this.title };
Scott Bakere41c9082014-10-27 23:02:48 -0700131 },
132 });
Scott Baker4aee9a12014-10-30 00:06:16 -0700133 TestApp[collection_name + "ListView"] = listViewClass;
Scott Bakere41c9082014-10-27 23:02:48 -0700134
135 var listView = new listViewClass();
136
Scott Baker4aee9a12014-10-30 00:06:16 -0700137 if (region_name in TestApp.getRegions()) {
138 TestApp[region_name].show(listView);
139 }
Scott Baker33c84ac2014-10-28 21:40:20 -0700140 xos[collection_name].fetch(); //startPolling();
Scott Bakere41c9082014-10-27 23:02:48 -0700141 }
Scott Baker3df41942014-10-28 12:44:13 -0700142
Scott Bakerd044c8f2014-10-28 14:46:13 -0700143 $('#close-detail-view').unbind().bind('click', function() {
Scott Baker3df41942014-10-28 12:44:13 -0700144 $('#detailBox').hide();
145 });
Scott Bakere41c9082014-10-27 23:02:48 -0700146});
147
148$(document).ready(function(){
Scott Baker013025a2014-10-30 23:50:20 -0700149 TestApp.start();
Scott Bakere41c9082014-10-27 23:02:48 -0700150});
151