blob: c6299442b8c897143a7467e5716c2d84e4e00894 [file] [log] [blame]
Scott Bakerac868ac2014-07-15 10:29:17 -07001/* This is a demo of using xoslib with Marionette
2
3 The main window is split into two halves. The left half has a CollectionView
4 (SliceListView) that lists all slices the user has access to. The right half
5 has an ItemView (SliceDetailView) that allows the user to edit the
6 name and description of a slice, as well as a <Save> button to save it.
7*/
8
Scott Bakerbb4961d2014-07-14 18:10:53 -07009SliceEditorApp = new Marionette.Application();
Scott Bakerac868ac2014-07-15 10:29:17 -070010
11SliceEditorApp.addRegions({
12 sliceList: "#sliceEditorList",
13 sliceDetail: "#sliceEditorDetail",
Scott Bakerbb4961d2014-07-14 18:10:53 -070014});
15
Scott Bakerac868ac2014-07-15 10:29:17 -070016/* SliceListItemView: This is the item view that is used by SliceListView to
17 display slice names.
18*/
19
Scott Bakerbb4961d2014-07-14 18:10:53 -070020SliceEditorApp.SliceListItemView = Marionette.ItemView.extend({
21 template: "#sliceeditor-listitem-template",
22 tagName: 'li',
23 className: 'sliceeditor-listitem',
24
25 events: {"click": "changeSlice"},
26
27 changeSlice: function(e) {
28 e.preventDefault();
29 e.stopPropagation();
30
31 if (SliceEditorApp.sliceDetail.currentView && SliceEditorApp.sliceDetail.currentView.dirty) {
32 if (!confirm("discard current changes?")) {
33 return;
34 }
35 }
36
Scott Bakerac868ac2014-07-15 10:29:17 -070037 /* create a new SliceDetailView and set the sliceDetail region to
38 display it.
39 */
40
Scott Bakerbb4961d2014-07-14 18:10:53 -070041 var sliceDetailView = new SliceEditorApp.SliceDetailView({
42 model: this.model,
43 });
44 SliceEditorApp.sliceDetail.show(sliceDetailView);
45 },
46});
Scott Bakerac868ac2014-07-15 10:29:17 -070047
48/* SliceListView: This displays a list of slice names.
49*/
Scott Bakerbb4961d2014-07-14 18:10:53 -070050
51SliceEditorApp.SliceListView = Marionette.CollectionView.extend({
52 tagName: "ul",
53 childView: SliceEditorApp.SliceListItemView,
54
Scott Bakerbb4961d2014-07-14 18:10:53 -070055 initialize: function() {
Scott Bakerac868ac2014-07-15 10:29:17 -070056 /* CollectionViews don't automatically listen for change events, but we
57 want to, so we pick up changes from the DetailView, and we pick up
58 changes from the server.
59 */
Scott Bakerbb4961d2014-07-14 18:10:53 -070060 this.listenTo(this.collection, 'change', this._renderChildren);
61 },
62
63 attachHtml: function(compositeView, childView, index) {
64 // The REST API will let admin users see everything. For the developer
65 // view we still want to hide slices we are not members of.
66 if (childView.model.get("sliceInfo").roles.length == 0) {
67 return;
68 }
69 SliceEditorApp.SliceListView.__super__.attachHtml(compositeView, childView, index);
70 },
71});
72
Scott Bakerac868ac2014-07-15 10:29:17 -070073/* SliceDetailView: Display the slice and allow it to be edited */
74
Scott Bakerbb4961d2014-07-14 18:10:53 -070075SliceEditorApp.SliceDetailView = Marionette.ItemView.extend({
76 template: "#sliceeditor-sliceedit-template",
77 tagName: 'div',
78
79 events: {"click button.js-submit": "submitClicked",
80 "change input": "inputChanged"},
81
Scott Bakerac868ac2014-07-15 10:29:17 -070082 /* inputChanged is watching the onChange events of the input controls. We
83 do this to track when this view is 'dirty', so we can throw up a warning
84 if the user tries to change his slices without saving first.
85 */
86
Scott Bakerbb4961d2014-07-14 18:10:53 -070087 inputChanged: function(e) {
88 this.dirty = true;
89 },
90
91 submitClicked: function(e) {
92 e.preventDefault();
93 var data = Backbone.Syphon.serialize(this);
94 this.model.save(data);
95 this.dirty = false;
96 },
97});
98
99SliceEditorApp.on("start", function() {
100 var sliceListView = new SliceEditorApp.SliceListView({
101 collection: xos.slicesPlus
102 });
103 SliceEditorApp.sliceList.show(sliceListView);
104 xos.slicesPlus.startPolling();
105});
106
107$(document).ready(function(){
108 SliceEditorApp.start();
109});
110