fix first save
[angular-mojolicious.git] / templates / conference / Work.html.ep
1 <script>
2
3 if (typeof (console) === 'undefined') console = { debug: function() {} }; // mock console.debug
4
5 function Work($resource){
6         this.master = {
7                 type: '',
8                 title: '',
9                 abstract: '',
10                 authors:[ { name:'', surname:'', inst:'', email:'' } ],
11                 symposium: { organizer: [ {name:'', surname:'', inst:'', email:'' } ], work_nr: 1, },
12         };
13         this.last_saved_work = {};
14         this.Work = $resource( '/data/conference/Work/:_id', { _id:'' } );
15         this.Symposium = $resource( '/data/conference/Symposium/:_id', { _id:'' } );
16         this.reset();
17         this.$watch('$location.hashPath', this.hash_change);
18 }
19 Work.$inject=['$resource'];
20
21 Work.prototype = {
22         hash_change: function() {
23                 var id = this.$location.hashPath;
24 console.debug( 'hash_change', id, this.work._id );
25                 if ( id != this.work._id ) {
26                         if (id) {
27                                 var self = this;
28                                 this.work = this.Work.get({ _id: id }, function(work) {
29                                         self.last_saved_work = angular.copy(work);
30                                         if ( work.type == 'symposium' ) {
31                                                 var s_id = work.symposium._id || work._id;
32                                                 // first work doesn't have symposium._id, but we used same _id
33 console.debug( 'load symposium ', s_id );
34                                                 self.symposium = self.Symposium.get({ _id: s_id });
35                                         }
36                                 });
37                         }
38                         else this.reset();
39                 }
40         },
41         reset: function() {
42                 console.debug( this.Work );
43                 var current_symposium = null;
44                 if ( this.work && this.work.type == 'symposium' ) {
45                         current_symposium = this.work.symposium;
46                         if ( this.work._id ) current_symposium.work_nr++; // only if saved
47 console.debug( 'current_symposium', current_symposium, this.work )
48                 }
49                 this.work = new this.Work( this.master );
50                 if ( current_symposium ) {
51                         this.work.symposium = current_symposium;
52                         this.work.type = 'symposium';
53                 }
54                 this.last_saved_work = {};
55 console.debug( 'reset', current_symposium, this.work, this.$location.hashPath );
56         },
57         save: function(){
58                 var self = this;
59                 this.work.$save(function(work){
60                         self.$location.hashPath = work._id;
61
62                         // save symposium to separate resource
63                         if ( work.type == 'symposium' ) {
64                                 if ( ! self.symposium ) { 
65                                         self.work.symposium._id = work._id; // reuse _id of first work for symposium
66                                         self.symposium = new self.Symposium( work.symposium );
67                                         self.symposium.works = [];
68                                 }
69                                 self.symposium.works[ work.symposium.work_nr - 1 ] = work;
70                 console.debug('save_symposium', self.symposium );
71                                 self.symposium.$save();
72                         }
73
74                         self.last_saved_work = angular.copy(work);
75                 });
76         },
77         get_symposium: function() { this.symposium },
78 };
79
80
81 </script>
82
83 <h1>Conference work submission</h1>
84
85 <div ng:controller="Work" ng:init="$window.$root = this;">
86
87 <h2>Type of work:</h2>
88 <label><input type="radio" name="work.type" value="symposium"> Symposium</label>
89
90 <div ng:show="work.type == 'symposium'" style="background:#f0f0f0">
91 <!--
92 <select name="work.symposium_id" ng:show="work.type == 'symposium'" ng:controller="Symposium">
93 <option ng:repeat="s in symposiums" value="{{s._id}}">{{s.title}}</option>
94 </select>
95 -->
96
97 <label>Topic of symposium: <input name="work.symposium.title" size="60" ng:required></label><br/>
98    
99 <label>Summary: <br/>
100 <textarea name="work.symposium.abstract" cols="50" rows="5"></textarea>
101 </label>
102 <br/>
103
104 Organizer:
105 <div ng:repeat="author in work.symposium.organizer">
106 [<a href="" ng:click="work.symposium.organizer.$remove(author)">X</a>]
107 <input name="author.name" ng:required>
108 <input name="author.surname" ng:required>
109 <input name="author.inst" >
110 <input name="author.email" ng:required>
111 </div>
112 [<a href="" ng:click="work.symposium.organizer.$add()">Add another organizer</a>]
113
114 </div>
115
116 <br/>
117
118 <label><input type="radio" name="work.type" value="lecture"> Lecture</label><br/>
119 <label><input type="radio" name="work.type" value="poster"> Poster</label><br/>
120 <label><input type="radio" name="work.type" value="round"> Round table</label><br/>
121
122 <hr>
123
124 <div ng:show="symposium">
125 Works which are part of this symposium:
126 <ol>
127 <li ng:repeat="w in symposium.works"><a href="#{{w._id}}">{{w.title}}</a></li>
128 </ol>
129
130 </div>
131
132 <h2>Autors<span ng:show="work.type == 'symposium'"> of {{work.symposium.work_nr}}. work </span></h2>
133
134 <div ng:repeat="author in work.authors">
135 [<a href="" ng:click="work.authors.$remove(author)">X</a>]
136 <input name="author.name" ng:required>
137 <input name="author.surname" ng:required>
138 <input name="author.inst" >
139 <input name="author.email" ng:required>
140 </div>
141 [<a href="" ng:click="work.authors.$add()">Add another author</a>]
142
143 <hr>    
144
145
146 <label>Title: <input name="work.title" size="60" ng:required></label><br/>
147
148 <label>Summary:<br>
149 <textarea name="work.abstract" cols="50" rows="5"></textarea>
150 </label>
151 <br/>
152
153 <span ng:show="$invalidWidgets.visible() == 0">
154 <input type="submit" value="Save" ng:click="work.symposium_id=symposium._id; save();" ng:show="! last_saved_work.$equals(work)">
155 <input type="reset" value="Add another work" ng:click="reset()" ng:show="work && work._id">
156 </span>
157
158 <b ng:show="$invalidWidgets.visible() &gt; 0" style="color:#800">{{$invalidWidgets.visible()}} errors to fix in submission form</b>
159
160 <div ng:show="work._id">
161 Permalink to <a href="#{{work._id}}">{{work.title}}</a> which you can bookmark
162 </div>
163
164 <hr>
165 Debug Information:
166 {{$window.location.href}}
167 <pre>
168 work = {{work}}
169
170 dirty={{! last_saved_work.$equals(work)}}
171
172 last_saved_work = {{last_saved_work}}
173
174 master = {{master}}
175
176 $id={{$id}}
177 work.$id={{work.$id}}
178 work._id={{work._id}}
179 </pre>
180
181 </div>