2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-10-02 08:32:53 -04:00
|
|
|
import Flash from '../../flash';
|
2017-09-06 12:14:34 -04:00
|
|
|
import { handleLocationHash } from '../../lib/utils/common_utils';
|
2018-01-26 04:19:03 -05:00
|
|
|
import axios from '../../lib/utils/axios_utils';
|
2017-09-06 12:14:34 -04:00
|
|
|
|
2017-04-13 13:05:09 -04:00
|
|
|
export default class BlobViewer {
|
|
|
|
constructor() {
|
2017-05-08 19:50:23 -04:00
|
|
|
BlobViewer.initAuxiliaryViewer();
|
2018-03-01 02:33:06 -05:00
|
|
|
BlobViewer.initRichViewer();
|
2017-05-08 19:50:23 -04:00
|
|
|
|
|
|
|
this.initMainViewers();
|
|
|
|
}
|
|
|
|
|
|
|
|
static initAuxiliaryViewer() {
|
|
|
|
const auxiliaryViewer = document.querySelector('.blob-viewer[data-type="auxiliary"]');
|
|
|
|
if (!auxiliaryViewer) return;
|
|
|
|
|
|
|
|
BlobViewer.loadViewer(auxiliaryViewer);
|
|
|
|
}
|
|
|
|
|
2018-03-01 02:33:06 -05:00
|
|
|
static initRichViewer() {
|
|
|
|
const viewer = document.querySelector('.blob-viewer[data-type="rich"]');
|
|
|
|
if (!viewer || !viewer.dataset.richType) return;
|
|
|
|
|
|
|
|
const initViewer = promise => promise
|
|
|
|
.then(module => module.default(viewer))
|
|
|
|
.catch((error) => {
|
|
|
|
Flash('Error loading file viewer.');
|
|
|
|
throw error;
|
|
|
|
});
|
|
|
|
|
|
|
|
switch (viewer.dataset.richType) {
|
|
|
|
case 'balsamiq':
|
|
|
|
initViewer(import(/* webpackChunkName: 'balsamiq_viewer' */ '../balsamiq_viewer'));
|
|
|
|
break;
|
2018-03-01 02:47:06 -05:00
|
|
|
case 'notebook':
|
|
|
|
initViewer(import(/* webpackChunkName: 'notebook_viewer' */ '../notebook_viewer'));
|
|
|
|
break;
|
2018-03-01 03:02:18 -05:00
|
|
|
case 'pdf':
|
|
|
|
initViewer(import(/* webpackChunkName: 'pdf_viewer' */ '../pdf_viewer'));
|
|
|
|
break;
|
2018-03-01 03:11:07 -05:00
|
|
|
case 'sketch':
|
|
|
|
initViewer(import(/* webpackChunkName: 'sketch_viewer' */ '../sketch_viewer'));
|
|
|
|
break;
|
2018-03-01 03:21:08 -05:00
|
|
|
case 'stl':
|
|
|
|
initViewer(import(/* webpackChunkName: 'stl_viewer' */ '../stl_viewer'));
|
|
|
|
break;
|
2018-03-01 02:33:06 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
initMainViewers() {
|
|
|
|
this.$fileHolder = $('.file-holder');
|
|
|
|
if (!this.$fileHolder.length) return;
|
|
|
|
|
2017-04-21 14:33:48 -04:00
|
|
|
this.switcher = document.querySelector('.js-blob-viewer-switcher');
|
|
|
|
this.switcherBtns = document.querySelectorAll('.js-blob-viewer-switch-btn');
|
2017-04-13 13:05:09 -04:00
|
|
|
this.copySourceBtn = document.querySelector('.js-copy-blob-source-btn');
|
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
this.simpleViewer = this.$fileHolder[0].querySelector('.blob-viewer[data-type="simple"]');
|
|
|
|
this.richViewer = this.$fileHolder[0].querySelector('.blob-viewer[data-type="rich"]');
|
2017-04-13 13:05:09 -04:00
|
|
|
|
2017-04-21 14:33:48 -04:00
|
|
|
this.initBindings();
|
2017-04-13 13:05:09 -04:00
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
this.switchToInitialViewer();
|
|
|
|
}
|
|
|
|
|
|
|
|
switchToInitialViewer() {
|
|
|
|
const initialViewer = this.$fileHolder[0].querySelector('.blob-viewer:not(.hidden)');
|
|
|
|
let initialViewerName = initialViewer.getAttribute('data-type');
|
|
|
|
|
2017-04-21 14:33:48 -04:00
|
|
|
if (this.switcher && location.hash.indexOf('#L') === 0) {
|
|
|
|
initialViewerName = 'simple';
|
2017-04-13 13:05:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.switchToViewer(initialViewerName);
|
|
|
|
}
|
|
|
|
|
|
|
|
initBindings() {
|
2017-04-21 14:33:48 -04:00
|
|
|
if (this.switcherBtns.length) {
|
|
|
|
Array.from(this.switcherBtns)
|
|
|
|
.forEach((el) => {
|
|
|
|
el.addEventListener('click', this.switchViewHandler.bind(this));
|
|
|
|
});
|
|
|
|
}
|
2017-04-13 13:05:09 -04:00
|
|
|
|
|
|
|
if (this.copySourceBtn) {
|
|
|
|
this.copySourceBtn.addEventListener('click', () => {
|
2017-05-18 16:53:15 -04:00
|
|
|
if (this.copySourceBtn.classList.contains('disabled')) return this.copySourceBtn.blur();
|
2017-04-13 13:05:09 -04:00
|
|
|
|
2017-05-18 16:53:15 -04:00
|
|
|
return this.switchToViewer('simple');
|
2017-04-13 13:05:09 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switchViewHandler(e) {
|
|
|
|
const target = e.currentTarget;
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
this.switchToViewer(target.getAttribute('data-viewer'));
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleCopyButtonState() {
|
|
|
|
if (!this.copySourceBtn) return;
|
|
|
|
|
|
|
|
if (this.simpleViewer.getAttribute('data-loaded')) {
|
|
|
|
this.copySourceBtn.setAttribute('title', 'Copy source to clipboard');
|
|
|
|
this.copySourceBtn.classList.remove('disabled');
|
2017-04-20 18:56:24 -04:00
|
|
|
} else if (this.activeViewer === this.simpleViewer) {
|
2017-04-13 13:05:09 -04:00
|
|
|
this.copySourceBtn.setAttribute('title', 'Wait for the source to load to copy it to the clipboard');
|
|
|
|
this.copySourceBtn.classList.add('disabled');
|
|
|
|
} else {
|
|
|
|
this.copySourceBtn.setAttribute('title', 'Switch to the source to copy it to the clipboard');
|
|
|
|
this.copySourceBtn.classList.add('disabled');
|
|
|
|
}
|
|
|
|
|
2018-04-19 11:40:23 -04:00
|
|
|
$(this.copySourceBtn).tooltip('_fixTitle');
|
2017-04-13 13:05:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
switchToViewer(name) {
|
2017-05-08 19:50:23 -04:00
|
|
|
const newViewer = this.$fileHolder[0].querySelector(`.blob-viewer[data-type='${name}']`);
|
2017-04-20 18:56:24 -04:00
|
|
|
if (this.activeViewer === newViewer) return;
|
2017-04-13 13:05:09 -04:00
|
|
|
|
2017-04-21 14:33:48 -04:00
|
|
|
const oldButton = document.querySelector('.js-blob-viewer-switch-btn.active');
|
|
|
|
const newButton = document.querySelector(`.js-blob-viewer-switch-btn[data-viewer='${name}']`);
|
2017-05-08 19:50:23 -04:00
|
|
|
const oldViewer = this.$fileHolder[0].querySelector(`.blob-viewer:not([data-type='${name}'])`);
|
2017-04-13 13:05:09 -04:00
|
|
|
|
|
|
|
if (oldButton) {
|
|
|
|
oldButton.classList.remove('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newButton) {
|
|
|
|
newButton.classList.add('active');
|
|
|
|
newButton.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldViewer) {
|
|
|
|
oldViewer.classList.add('hidden');
|
|
|
|
}
|
2017-04-20 18:56:24 -04:00
|
|
|
|
2017-04-13 13:05:09 -04:00
|
|
|
newViewer.classList.remove('hidden');
|
|
|
|
|
|
|
|
this.activeViewer = newViewer;
|
|
|
|
|
|
|
|
this.toggleCopyButtonState();
|
|
|
|
|
2017-05-08 19:50:23 -04:00
|
|
|
BlobViewer.loadViewer(newViewer)
|
|
|
|
.then((viewer) => {
|
2017-05-30 08:25:47 -04:00
|
|
|
$(viewer).renderGFM();
|
2017-05-08 19:50:23 -04:00
|
|
|
|
|
|
|
this.$fileHolder.trigger('highlight:line');
|
2017-09-06 12:14:34 -04:00
|
|
|
handleLocationHash();
|
2017-05-08 19:50:23 -04:00
|
|
|
|
|
|
|
this.toggleCopyButtonState();
|
|
|
|
})
|
|
|
|
.catch(() => new Flash('Error loading viewer'));
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadViewer(viewerParam) {
|
|
|
|
const viewer = viewerParam;
|
|
|
|
const url = viewer.getAttribute('data-url');
|
|
|
|
|
2018-01-26 04:19:03 -05:00
|
|
|
if (!url || viewer.getAttribute('data-loaded') || viewer.getAttribute('data-loading')) {
|
|
|
|
return Promise.resolve(viewer);
|
|
|
|
}
|
2017-05-08 19:50:23 -04:00
|
|
|
|
2018-01-26 04:19:03 -05:00
|
|
|
viewer.setAttribute('data-loading', 'true');
|
2017-05-08 19:50:23 -04:00
|
|
|
|
2018-01-26 04:19:03 -05:00
|
|
|
return axios.get(url)
|
|
|
|
.then(({ data }) => {
|
2017-05-08 19:50:23 -04:00
|
|
|
viewer.innerHTML = data.html;
|
|
|
|
viewer.setAttribute('data-loaded', 'true');
|
|
|
|
|
2018-01-26 04:19:03 -05:00
|
|
|
return viewer;
|
2017-05-08 19:50:23 -04:00
|
|
|
});
|
2017-04-13 13:05:09 -04:00
|
|
|
}
|
|
|
|
}
|