-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmain.js
More file actions
187 lines (160 loc) · 5.41 KB
/
main.js
File metadata and controls
187 lines (160 loc) · 5.41 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
// Import our custom CSS based on bootstrap
import "../scss/styles.scss";
import "../css/figure.css";
// Import all of Bootstrap's JS
import * as bootstrap from "bootstrap";
import Backbone from "backbone";
import $ from "jquery";
import FigureModel from "./models/figure_model";
import createFigureView from "./views/figure_view";
import SvgView from "./views/svg_model_view";
import RightPanelView from "./views/right_panel_view";
import { UndoManager, UndoView } from "./models/undo";
import { ajaxSetup } from "./views/util.csrf";
import { hideModals, showModal } from "./views/util";
const figureModel = new FigureModel();
// make this global so we can access it from the browser console
window.figureModel = figureModel;
// This will be '/' unless deployed from gh-pages, when it will be '/omero-figure/'
console.log("import.meta.env", import.meta.env)
window.BASE_URL = import.meta.env.BASE_URL.slice(1); // remove leading slash
console.log("BASE_URL", BASE_URL);
const RELEASE_VERSION = import.meta.env.VITE_VERSION;
console.log("RELEASE_VERSION", RELEASE_VERSION);
document.getElementById("release_version").innerHTML = RELEASE_VERSION;
// Override 'Backbone.sync'...
Backbone.ajaxSync = Backbone.sync;
// TODO: - Use the undo/redo queue instead of sync to trigger figureModel.set("unsaved", true);
// If syncOverride, then instead of actually trying to Save via ajax on model.save(attr, value)
// We simply set the 'unsaved' flag on the figureModel.
// This works for FigureModel and also for Panels collection.
Backbone.getSyncMethod = function (model) {
if (
model.syncOverride ||
(model.collection && model.collection.syncOverride)
) {
return function (method, model, options, error) {
figureModel.set("unsaved", true);
};
}
return Backbone.ajaxSync;
};
// Override 'Backbone.sync' to default to localSync,
// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
Backbone.sync = function (method, model, options, error) {
return Backbone.getSyncMethod(model).apply(this, [
method,
model,
options,
error,
]);
};
new SvgView({ model: figureModel });
new RightPanelView({ model: figureModel });
if (PING_URL) {
// keep-alive ping every minute, so that OMERO session doesn't die
setInterval(function () {
fetch(PING_URL);
}, 60000);
}
// Undo Model and View
var undoManager = new UndoManager({ figureModel: figureModel }),
undoView = new UndoView({ model: undoManager });
// Finally, start listening for changes to panels
undoManager.listenToCollection(figureModel.panels);
// All routes based on BASE_URL, which is either '/' or '/omero-figure/' depending on deployment
let routes = {};
routes[`${BASE_URL}(/)`] = "index";
routes[`${BASE_URL}new(/)`] = "newFigure";
routes[`${BASE_URL}recover(/)`] = "recoverFigure";
routes[`${BASE_URL}open(/)`] = "openFigure";
routes[`${BASE_URL}file/:id(/)`] = "loadFigure";
var FigureRouter = Backbone.Router.extend({
routes: routes,
index: function () {
console.log("index");
// Check for ?file=http://...json
// TODO: do we ONLY want to do this on index?
if (window.location.search.length > 1) {
const searchParams = new URLSearchParams(window.location.search.substring(1));
if (searchParams.has("file")) {
const file = searchParams.get("file");
var cb = function () {
figureModel.load_from_url(file);
};
figureModel.checkSaveAndClear(cb);
return;
}
}
hideModals();
var cb = () => {
showModal("welcomeModal");
};
figureModel.checkSaveAndClear(cb);
},
openFigure: function () {
console.log("openFigure...");
hideModals();
var cb = function () {
showModal("openFigureModal");
};
figureModel.checkSaveAndClear(cb);
},
recoverFigure: function () {
hideModals();
figureModel.recoverFromLocalStorage();
},
newFigure: function () {
console.log("newFigure");
hideModals();
var cb = function () {
showModal("addImagesModal");
};
// Check for ?image=1&image=2
if (window.location.search.length > 1) {
var params = window.location.search.substring(1).split("&");
var iids = params.reduce(function (prev, param) {
if (param.split("=")[0] === "image") {
prev.push(param.split("=")[1]);
}
return prev;
}, []);
if (iids.length > 0) {
cb = function () {
figureModel.addImages(iids);
};
}
}
figureModel.checkSaveAndClear(cb);
},
loadFigure: function (id) {
console.log("LoadFigure", id);
hideModals();
var fileId = parseInt(id, 10);
var cb = function () {
figureModel.load_from_OMERO(fileId);
};
figureModel.checkSaveAndClear(cb);
},
});
let config = { pushState: true };
// when deployed from omero-web...
if (APP_ROOT_URL) {
config.root = APP_ROOT_URL;
}
// jQuery ajaxSetup for COORs
ajaxSetup();
const app = new FigureRouter();
createFigureView({ model: figureModel, app })
Backbone.history.start(config);
// We want 'a' links (E.g. to open_figure) to use app.navigate
$(document).on("click", "a", function (ev) {
var href = $(this).attr("href");
// check that links are 'internal' to this app
if (href.substring(0, BASE_WEBFIGURE_URL.length) === BASE_WEBFIGURE_URL) {
ev.preventDefault();
let baseUrl = APP_SERVED_BY_OMERO ? "/" : "/omero-figure/";
href = href.replace(BASE_WEBFIGURE_URL, baseUrl);
app.navigate(href, { trigger: true });
}
});