2017-04-10 10:48:23 -04:00
|
|
|
import sqljs from 'sql.js';
|
2017-04-11 08:06:32 -04:00
|
|
|
import { template as _template } from 'underscore';
|
2017-04-10 10:48:23 -04:00
|
|
|
|
2017-04-28 12:32:50 -04:00
|
|
|
const PREVIEW_TEMPLATE = _template(`
|
|
|
|
<div class="panel panel-default">
|
|
|
|
<div class="panel-heading"><%- name %></div>
|
|
|
|
<div class="panel-body">
|
|
|
|
<img class="img-thumbnail" src="data:image/png;base64,<%- image %>"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
|
2017-04-10 10:48:23 -04:00
|
|
|
class BalsamiqViewer {
|
|
|
|
constructor(viewer) {
|
|
|
|
this.viewer = viewer;
|
|
|
|
}
|
|
|
|
|
2017-05-15 03:48:54 -04:00
|
|
|
loadFile(endpoint) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open('GET', endpoint, true);
|
|
|
|
xhr.responseType = 'arraybuffer';
|
|
|
|
xhr.onload = loadEvent => this.fileLoaded(loadEvent, resolve, reject);
|
|
|
|
xhr.onerror = reject;
|
|
|
|
|
|
|
|
xhr.send();
|
|
|
|
});
|
|
|
|
}
|
2017-04-10 10:48:23 -04:00
|
|
|
|
2017-05-15 03:48:54 -04:00
|
|
|
fileLoaded(loadEvent, resolve, reject) {
|
|
|
|
if (loadEvent.target.status !== 200) return reject();
|
2017-04-10 10:48:23 -04:00
|
|
|
|
2017-05-15 03:48:54 -04:00
|
|
|
this.renderFile(loadEvent);
|
2017-04-10 10:48:23 -04:00
|
|
|
|
2017-05-15 03:48:54 -04:00
|
|
|
return resolve();
|
2017-04-10 10:48:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
renderFile(loadEvent) {
|
|
|
|
const container = document.createElement('ul');
|
|
|
|
|
|
|
|
this.initDatabase(loadEvent.target.response);
|
|
|
|
|
|
|
|
const previews = this.getPreviews();
|
2017-04-28 12:32:50 -04:00
|
|
|
previews.forEach((preview) => {
|
|
|
|
const renderedPreview = this.renderPreview(preview);
|
2017-04-10 10:48:23 -04:00
|
|
|
|
2017-04-28 12:32:50 -04:00
|
|
|
container.appendChild(renderedPreview);
|
|
|
|
});
|
|
|
|
|
|
|
|
container.classList.add('list-inline');
|
|
|
|
container.classList.add('previews');
|
2017-04-10 10:48:23 -04:00
|
|
|
|
|
|
|
this.viewer.appendChild(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
initDatabase(data) {
|
|
|
|
const previewBinary = new Uint8Array(data);
|
|
|
|
|
|
|
|
this.database = new sqljs.Database(previewBinary);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPreviews() {
|
2017-04-10 11:44:49 -04:00
|
|
|
const thumbnails = this.database.exec('SELECT * FROM thumbnails');
|
2017-04-10 10:48:23 -04:00
|
|
|
|
2017-04-10 11:44:49 -04:00
|
|
|
return thumbnails[0].values.map(BalsamiqViewer.parsePreview);
|
2017-04-10 10:48:23 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 12:32:50 -04:00
|
|
|
getResource(resourceID) {
|
|
|
|
const resources = this.database.exec(`SELECT * FROM resources WHERE id = '${resourceID}'`);
|
|
|
|
|
|
|
|
return resources[0];
|
2017-04-11 06:28:17 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 10:48:23 -04:00
|
|
|
renderPreview(preview) {
|
|
|
|
const previewElement = document.createElement('li');
|
|
|
|
|
2017-04-10 11:44:49 -04:00
|
|
|
previewElement.classList.add('preview');
|
2017-04-10 10:48:23 -04:00
|
|
|
previewElement.innerHTML = this.renderTemplate(preview);
|
|
|
|
|
2017-04-28 12:32:50 -04:00
|
|
|
return previewElement;
|
2017-04-10 10:48:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(preview) {
|
2017-04-28 12:32:50 -04:00
|
|
|
const resource = this.getResource(preview.resourceID);
|
|
|
|
const name = BalsamiqViewer.parseTitle(resource);
|
2017-04-10 10:48:23 -04:00
|
|
|
const image = preview.image;
|
|
|
|
|
2017-04-28 12:32:50 -04:00
|
|
|
const template = PREVIEW_TEMPLATE({
|
2017-04-11 06:28:17 -04:00
|
|
|
name,
|
|
|
|
image,
|
|
|
|
});
|
2017-04-10 10:48:23 -04:00
|
|
|
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
|
|
|
static parsePreview(preview) {
|
|
|
|
return JSON.parse(preview[1]);
|
|
|
|
}
|
|
|
|
|
2017-04-28 12:32:50 -04:00
|
|
|
/*
|
|
|
|
* resource = {
|
|
|
|
* columns: ['ID', 'BRANCHID', 'ATTRIBUTES', 'DATA'],
|
|
|
|
* values: [['id', 'branchId', 'attributes', 'data']],
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* 'attributes' being a JSON string containing the `name` property.
|
|
|
|
*/
|
|
|
|
static parseTitle(resource) {
|
|
|
|
return JSON.parse(resource.values[0][2]).name;
|
2017-04-11 06:28:17 -04:00
|
|
|
}
|
2017-04-10 10:48:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default BalsamiqViewer;
|