-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectView.js
More file actions
374 lines (304 loc) · 10.9 KB
/
ProjectView.js
File metadata and controls
374 lines (304 loc) · 10.9 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"use strict";
var ProjectView = JakeKit.HBox.extend({
initialize: function(project) {
JakeKit.HBox.prototype.initialize.call(this);
this.project = project;
_.bindAll(this, "openTab", "schemaNameChanged", "selectTab", "deleteSelection");
this.sidebar = new Sidebar(project, this);
this.addChild(this.sidebar);
this.tabstack = new JakeKit.w2tabstack();
this.addChild(this.tabstack);
this.views = {};
this.listenTo(this.project, "schemaNameChanged", this.schemaNameChanged);
this.listenTo(this.project, "schemaOpened", this.openTab);
if (this.project.noSchemas()) {
// If there is no schema, then we'll create one.
this.project.dispatchAction(new Action({type: "ADD_SCHEMA"}));
} else {
_.each(this.project.listOpenTabs(), function(tab_id) {
this.openTab(tab_id, true);
}, this);
var selected_tab = this.project.selectedTab();
if (selected_tab) {
this.selectTab(selected_tab);
}
}
this.listenTo(this.tabstack, "viewSelected", this.viewSelected);
this.interval_id = setInterval(function(project_view) {
var schema_model = project_view.project.getSchema(project_view.project.selectedTab());
schema_model.checkAndRebuild();
schema_model.logic_system.run(100);
project_view.activeView().doDraw();
}, 100, this);
},
openTab: function(schema_id, dont_select) {
var schema = this.project.getSchema(schema_id);
var new_view = new SchemaView(schema, this.project);
this.views[schema.id] = new_view;
this.tabstack.addChild(new_view, this.project.getSchemaName(schema_id));
if (!dont_select) {
this.selectTab(schema_id);
}
},
selectTab: function(schema_id) {
this.tabstack.makeActive(this.views[schema_id]);
},
viewSelected: function(view) {
this.project.dispatchAction(new Action({type: "SELECT_SCHEMA", schema: view.id}));
this.sidebar.viewChanged();
},
activeSchema: function() {
return this.activeView().id;
},
schemaNameChanged: function(schema_id) {
this.tabstack.setCaption(this.views[schema_id], this.project.getSchemaName(schema_id));
},
deleteSelection: function() {
this.activeView().deleteSelection();
},
activeView: function() {
return this.tabstack.activeView();
},
undo: function() {
this.project.dispatchAction(new Action({ type: "UNDO" }));
},
redo: function() {
this.project.dispatchAction(new Action({ type: "REDO" }));
},
});
var ComponentView = Backbone.View.extend({
attributes: { draggable: "true" },
initialize: function(args) {
this.schema_id = args.schema_id;
this.name = args.name;
},
render: function() {
this.$el.html(this.name);
this.el.addEventListener("dragstart", makeEventListener("COMPONENT:" + this.schema_id), true);
return this;
}
});
var ComponentList = Backbone.View.extend({
className: "components",
initialize: function(project) {
this.project = project;
},
render: function() {
this.$el.empty();
_.each(this.project.listSchemas(), function(schema_id) {
if (this.project.isComponent(schema_id)) {
var component_view = new ComponentView({ schema_id: schema_id, name: this.project.getSchemaName(schema_id) });
this.$el.append(component_view.render().el);
}
}, this);
}
});
var SchemaDetailsView = Backbone.View.extend({
initialize: function(none, project) {
this.project = project;
},
render: function() {
var schema_id = this.project.selectedTab();
var html = '<table><tr><td>';
html += '<span class="button" id="new-schema">New Schematic</span><hr>';
html += 'Schematic Name<br>';
html += '<input id="schema-name" type="text" value="' + _.escape(this.project.getSchemaName(schema_id)) + '">';
html += '</td></tr><tr><td id="component-editor-panel"></td></tr></table>'
this.$el.empty();
this.$el.html(html)
var that = this;
this.$("#schema-name").on("change", function() {
that.project.dispatchAction(new Action({
type: "RENAME_SCHEMA",
schema: schema_id,
new_name: that.$("input")[0].value,
old_name: that.project.getSchemaName(schema_id)
}));
});
if (this.project.isComponent(schema_id)) {
var component_editor = new ComponentEditor({}, schema_id, this.project);
this.$("#component-editor-panel").append(component_editor.$el);
}
this.$("#new-schema").click(function() {
that.project.dispatchAction(new Action({type: "ADD_SCHEMA"}));
});
}
});
var picker_menu = [
{ text: "Project", name: "projectPicked"},
{ text: "Pallet", name: "palletPicked"},
{ text: "Components", name: "componentsPicked"},
{ text: "Edit", name: "editPicked"},
{ text: "Schematic", name: "schemaDetailsPicked" },
{ text: "About Digital Schematic", name: "aboutPicked" }
];
var Picker = Backbone.View.extend({
className: "picker",
initialize: function(none, sidebar) {
this.sidebar = sidebar;
},
render: function() {
var html = '';
_.each(picker_menu, function(menu_item) {
html += '<div id="' + menu_item.name + '">' + menu_item.text + '</div>';
}, this);
this.$el.html(html);
_.each(picker_menu, function(menu_item) {
this.$("#" + menu_item.name).click(this.sidebar[menu_item.name]);
}, this);
},
showSelection: function(name) {
_.each(picker_menu, function(menu_item) {
var item_name = menu_item.name;
if (item_name == name) {
this.$("#" + item_name).addClass("selected");
} else {
this.$("#" + item_name).removeClass("selected");
}
}, this);
}
});
var Sidebar = Backbone.View.extend({
className: "sidebar",
initialize: function(project, project_view) {
this.project = project;
this.project_view = project_view;
_.bindAll(this, "palletPicked", "componentsPicked", "schemaDetailsPicked", "aboutPicked", "projectPicked", "editPicked");
this.listenTo(this.project, "newSchema", this.newSchema);
this.listenTo(this.project, "schemaNameChanged", this.schemaNameChanged);
this.picker = new Picker({}, this);
},
editPicked: function() {
if (this.current_selection != 'edit') {
this.view = new EditBox({}, this.project_view);
this.setContent(this.view);
this.current_selection = "edit";
this.picker.showSelection("editPicked");
}
},
projectPicked: function() {
if (this.current_selection != 'project') {
this.view = new ProjectBox(this.project);
this.setContent(this.view);
this.current_selection = "project";
this.picker.showSelection("projectPicked");
}
},
palletPicked: function() {
if (this.current_selection != "pallet") {
this.view = new Pallet();
this.setContent(this.view);
this.current_selection = "pallet";
this.picker.showSelection("palletPicked");
}
},
componentsPicked: function() {
if (this.current_selection != "components") {
this.view = new ComponentList(this.project);
this.setContent(this.view);
this.current_selection = "components";
this.picker.showSelection("componentsPicked");
}
},
schemaDetailsPicked: function() {
if (this.current_selection != "schema_details") {
this.view = new SchemaDetailsView({}, this.project);
this.setContent(this.view);
this.current_selection = "schema_details";
this.picker.showSelection("schemaDetailsPicked");
}
},
aboutPicked: function() {
if (this.current_selection != "about") {
this.view = new AboutBox();
this.setContent(this.view);
this.current_selection = "about";
this.picker.showSelection("aboutPicked");
}
},
setContent: function(view) {
var content_panel = this.$("#content-panel").empty();
view.render();
content_panel.append(view.$el);
},
viewChanged: function() {
if (this.current_selection == "schema_details") {
this.view.render();
}
},
render: function() {
this.$el.html('<table><tr><td id="picker-panel"></td></tr><tr><td id="content-panel"></td></tr></table>');
this.picker.render();
this.$("#picker-panel").append(this.picker.$el);
this.palletPicked();
this.picker.showSelection("palletPicked");
}
});
var ProjectBox = Backbone.View.extend({
initialize: function(project) {
this.project = project;
this.mode = "project";
},
render: function() {
this.$el.empty();
if (this.mode == "project") {
var html = '<table><tr><td><span class="button" id="new-project">New Project</span>'
html += '<span class="button" id="open-project">Open Project</span><hr>';
// get the project name
var main_view = this.project.main_view;
var database = this.project.main_view.database;
var project_list = database.getProjectList();
var project_id = database.getConfig("active_project");
var project_record;
project_list.each(function(pr) {
if (pr.get("project_id") == project_id) {
project_record = pr;
}
});
var database_name = project_record.get("name");
html += 'Project Name<br><input id="project-name" type="text" value="' + _.escape(project_record.get("name")) + '"></input>'
this.$el.html(html);
this.$("#new-project").click(main_view.newProject);
var project_box = this;
this.$("#open-project").click(function() {
project_box.mode = "open-project";
project_box.render();
});
this.$("#project-name").change(function() {
project_record.set("name", project_box.$("#project-name")[0].value);
project_record.save();
})
} else {
var main_view = this.project.main_view;
var database = this.project.main_view.database;
var project_list = database.getProjectList();
var records = project_list.toJSON();
this.$el.html("<ul></ul>");
_.each(records, function(project_record) {
var id = _.uniqueId();
this.$("ul").append('<li id="' + id + '">' + _.escape(project_record.name) + '</li');
this.$("#" + id).click(function() {
main_view.openProject(project_record.project_id);
});
}, this);
}
}
});
var EditBox = Backbone.View.extend({
initialize: function(none, project_view) {
this.project_view = project_view;
},
render: function() {
this.$el.html('<table><tr><td><span class="button" id="delete">Delete</span></td></tr></table>');
this.$("#delete").click(this.project_view.deleteSelection);
}
});
var AboutBox = Backbone.View.extend({
render: function() {
var html = '<p>This is program for the <a href="http://en.wikipedia.org/wiki/Schematic_capture">schematic capture</a> and <a href="http://en.wikipedia.org/wiki/Electronic_circuit_simulation">simulation<a> of digital electronic circuits built out of <a href="http://en.wikipedia.org/wiki/Logic_gate">logic gates<\a>.</p>';
html += '<p>This software is a toy. It may be useful for educational purposes but it is certainly not a tool suitable for professional engineers and probably not engineering students either. You will hopefully have a lot of fun playing with it though.</p>';
html += '<p>In it's current state this software is experimental, data loss is almost guaranteed. The author is not going to help you recover lost schematics.</p>';
html += '<p>This software was created by Joe Jones, the source code is available on git hub <a href="https://github.com/Joe-Jones/logic">https://github.com/Joe-Jones/logic</a> and can be copied under the terms of the MIT license.</p>';
this.$el.html(html);
}
})