2020-04-08 14:13:33 +05:30
|
|
|
import { template as _template } from 'lodash';
|
2021-03-11 19:13:27 +05:30
|
|
|
import sqljs from 'sql.js';
|
2019-12-21 20:55:43 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { successCodes } from '~/lib/utils/http_status';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
const PREVIEW_TEMPLATE = _template(`
|
2018-11-08 19:23:39 +05:30
|
|
|
<div class="card">
|
|
|
|
<div class="card-header"><%- name %></div>
|
|
|
|
<div class="card-body">
|
2017-08-17 22:00:37 +05:30
|
|
|
<img class="img-thumbnail" src="data:image/png;base64,<%- image %>"/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`);
|
|
|
|
|
|
|
|
class BalsamiqViewer {
|
|
|
|
constructor(viewer) {
|
|
|
|
this.viewer = viewer;
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
loadFile(endpoint) {
|
2019-12-21 20:55:43 +05:30
|
|
|
return axios
|
|
|
|
.get(endpoint, {
|
|
|
|
responseType: 'arraybuffer',
|
|
|
|
validateStatus(status) {
|
|
|
|
return status !== successCodes.OK;
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
this.renderFile(data);
|
|
|
|
})
|
2021-03-08 18:12:59 +05:30
|
|
|
.catch((e) => {
|
2019-12-21 20:55:43 +05:30
|
|
|
throw new Error(e);
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
renderFile(fileBuffer) {
|
2017-08-17 22:00:37 +05:30
|
|
|
const container = document.createElement('ul');
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
this.initDatabase(fileBuffer);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
const previews = this.getPreviews();
|
2021-03-08 18:12:59 +05:30
|
|
|
previews.forEach((preview) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
const renderedPreview = this.renderPreview(preview);
|
|
|
|
|
|
|
|
container.appendChild(renderedPreview);
|
|
|
|
});
|
|
|
|
|
|
|
|
container.classList.add('list-inline');
|
|
|
|
container.classList.add('previews');
|
|
|
|
|
|
|
|
this.viewer.appendChild(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
initDatabase(data) {
|
|
|
|
const previewBinary = new Uint8Array(data);
|
|
|
|
|
|
|
|
this.database = new sqljs.Database(previewBinary);
|
|
|
|
}
|
|
|
|
|
|
|
|
getPreviews() {
|
|
|
|
const thumbnails = this.database.exec('SELECT * FROM thumbnails');
|
|
|
|
|
|
|
|
return thumbnails[0].values.map(BalsamiqViewer.parsePreview);
|
|
|
|
}
|
|
|
|
|
|
|
|
getResource(resourceID) {
|
|
|
|
const resources = this.database.exec(`SELECT * FROM resources WHERE id = '${resourceID}'`);
|
|
|
|
|
|
|
|
return resources[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
renderPreview(preview) {
|
|
|
|
const previewElement = document.createElement('li');
|
|
|
|
|
|
|
|
previewElement.classList.add('preview');
|
|
|
|
previewElement.innerHTML = this.renderTemplate(preview);
|
|
|
|
|
|
|
|
return previewElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTemplate(preview) {
|
|
|
|
const resource = this.getResource(preview.resourceID);
|
|
|
|
const name = BalsamiqViewer.parseTitle(resource);
|
2018-11-08 19:23:39 +05:30
|
|
|
const { image } = preview;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
const template = PREVIEW_TEMPLATE({
|
|
|
|
name,
|
|
|
|
image,
|
|
|
|
});
|
|
|
|
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
|
|
|
static parsePreview(preview) {
|
|
|
|
return JSON.parse(preview[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BalsamiqViewer;
|