/
ProjectEditorView.js
258 lines (211 loc) · 7.56 KB
/
ProjectEditorView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
define(['underscore',
'jquery',
'backbone',
'models/project/ProjectModel',
'views/EditorView',
"views/project/editor/ProjEditorSectionsView",
"text!templates/loading.html",
"text!templates/project/editor/projectEditor.html"
],
function(_, $, Backbone, Project, EditorView, ProjEditorSectionsView, LoadingTemplate, Template){
/**
* @class ProjectEditorView
*/
var ProjectEditorView = EditorView.extend({
/**
* The type of View this is
* @type {string}
*/
type: "ProjectEditor",
/**
* The short name OR pid for the project
* @type {string}
*/
projectIdentifier: "",
/**
* The ProjectModel that is being edited
* @type {Project}
*/
model: undefined,
/**
* The currently active editor section. e.g. Data, Metrics, Settings, etc.
* @type {string}
*/
activeSection: "",
/**
* References to templates for this view. HTML files are converted to Underscore.js templates
*/
template: _.template(Template),
loadingTemplate: _.template(LoadingTemplate),
/**
* The events this view will listen to and the associated function to call.
* This view will inherit events from the parent class, EditorView.
* @type {Object}
*/
events: _.extend(EditorView.prototype.events, {
"focusout .title-container input" : "updateBasicText",
}),
/**
* Creates a new ProjectEditorView
* @constructs ProjectEditorView
* @param {Object} options - A literal object with options to pass to the view
*/
initialize: function(options){
if(typeof options == "object"){
// initializing the ProjectEditorView properties
this.projectIdentifier = options.projectIdentifier ? options.projectIdentifier : undefined;
this.activeSection = options.activeSection || "";
}
},
/**
* Renders the ProjectEditorView
*/
render: function(){
// Display a spinner to indicate loading until model is created.
this.$el.html(this.loadingTemplate({
msg: "Retrieving project details..."
}));
//Create the model
this.createModel();
// When the model has been synced, render the results
this.stopListening();
this.listenTo(this.model, "sync", this.renderProjectEditor);
if ( this.model.get("seriesId") || this.model.get("label") ){
// If the project model already exists - fetch it.
this.model.fetch();
}
else{
// handling the default case where a new project model is created
this.hideLoading();
this.renderProjectEditor();
}
return this;
},
/**
* Renders the project editor view once the project view is created
*/
renderProjectEditor: function() {
// Add the template to the view and give the body the "Editor" class
var name = this.model.get("name");
this.$el.html(this.template({
name: name
}));
$("body").addClass("Editor");
// Get the project identifier
// or set it to a default value in the case that it's a new project
var projectIdentifier = this.projectIdentifier;
if(!projectIdentifier){
projectIdentifier = "new-project"
}
var sectionsView = new ProjEditorSectionsView({
model: this.model,
projectIdentifier: projectIdentifier,
activeSection: this.activeSection
});
sectionsView.render();
this.$(".proj-editor-sections-container").html(sectionsView.el);
},
/**
* Create a ProjectModel object
*/
createModel: function(){
// Look up the project document seriesId by its registered name if given
if ( this.projectIdentifier) {
// Create a new project model with the identifier
this.model = new Project({
label: this.projectIdentifier
});
} else {
// Create a new, default project model
this.model = new Project();
}
},
/**
* The authorizeUser function checks if the current user is authorized
* to edit the given ProjectModel. If not, a message is displayed and
* the view doesn't render anything else.
*
* If the user isn't logged in at all, don't check for authorization and
* display a message and login button.
*/
authorizeUser: function() {
//Remove the loading message
this.hideLoading();
//Only proceed if the user is logged in
if ( MetacatUI.appUserModel.get("checked") && MetacatUI.appUserModel.get("loggedIn") ){
if ( this.model.checkAuthority() ) {
// Display the project editor
this.renderProjectEditor();
// Listens to the focus event on the window to detect when a user
// switches back to this browser tab from somewhere else
// When a user checks back, we want to check for log-in status
MetacatUI.appView.listenForActivity();
// Determine the length of time until the user's current token expires
// Asks to sign in in case of time out
MetacatUI.appView.listenForTimeout()
}
else {
// generate error message
var msg = "This is a private project. You're not authorized to access this project.";
//Show the not authorized error message
MetacatUI.appView.showAlert(msg, "alert-error", ".proj-editor-sections-container")
}
}
else if ( !MetacatUI.appUserModel.get("loggedIn") ){
// generate error message
var msg = 'This is a private project. If you believe you have permission ' +
'to access this project, then <a href="' + MetacatUI.root +
'/signin">sign in</a>.';
//Show the not logged in error
MetacatUI.appView.showAlert(msg, "alert-error", ".proj-editor-sections-container")
}
},
/**
* Hides the loading
*/
hideLoading: function() {
// Find the loading object and remove it.
if (this.$el.find(".loading")) {
this.$el.find(".loading").remove();
}
},
/**
* When a simple text input field loses focus, the corresponding model
* attribute is updated with the value from the input field
*
* @param {Event} [e] - The focusout event
*/
updateBasicText: function(e){
if(!e) return false;
//Get the category, new value, and model
var category = $(e.target).attr("data-category"),
value = $(e.target).val(),
model = $(e.target).data("model") || this.model;
//We can't update anything without a category
if(!category) return false;
//Get the current value
var currentValue = model.get(category);
//Insert the new value into the array
if( Array.isArray(currentValue) ){
//Find the position this text input is in
var position = $(e.target).parents("div.text-container").first().children("div").index($(e.target).parent());
//Set the value in that position in the array
currentValue[position] = value;
//Set the changed array on this model
model.set(category, currentValue);
model.trigger("change:" + category);
}
//Update the model if the current value is a string
else if(typeof currentValue == "string" || !currentValue){
model.set(category, value);
model.trigger("change:" + category);
}
//Add another blank text input
if($(e.target).is(".new") && value != '' && category != "title"){
$(e.target).removeClass("new");
this.addBasicText(e);
}
},
});
return ProjectEditorView;
});