2017-11-23 05:43:00 +09:00
|
|
|
declare const _API_URL_: string;
|
|
|
|
|
2017-03-18 20:05:11 +09:00
|
|
|
import * as riot from 'riot';
|
|
|
|
import dialog from './dialog';
|
|
|
|
import api from '../../common/scripts/api';
|
2017-02-18 17:56:27 +09:00
|
|
|
|
2017-03-18 20:05:11 +09:00
|
|
|
export default (I, cb, file = null) => {
|
2017-02-18 17:56:27 +09:00
|
|
|
const fileSelected = file => {
|
2017-11-13 18:05:35 +09:00
|
|
|
const cropper = (riot as any).mount(document.body.appendChild(document.createElement('mk-crop-window')), {
|
2017-02-18 17:56:27 +09:00
|
|
|
file: file,
|
|
|
|
title: 'バナーとして表示する部分を選択',
|
|
|
|
aspectRatio: 16 / 9
|
|
|
|
})[0];
|
2017-02-27 17:08:27 +09:00
|
|
|
|
2017-02-18 17:56:27 +09:00
|
|
|
cropper.on('cropped', blob => {
|
|
|
|
const data = new FormData();
|
|
|
|
data.append('i', I.token);
|
|
|
|
data.append('file', blob, file.name + '.cropped.png');
|
|
|
|
|
|
|
|
api(I, 'drive/folders/find', {
|
|
|
|
name: 'バナー'
|
|
|
|
}).then(iconFolder => {
|
|
|
|
if (iconFolder.length === 0) {
|
2017-02-19 07:15:18 +09:00
|
|
|
api(I, 'drive/folders/create', {
|
2017-02-18 17:56:27 +09:00
|
|
|
name: 'バナー'
|
|
|
|
}).then(iconFolder => {
|
2017-02-27 17:08:59 +09:00
|
|
|
upload(data, iconFolder);
|
2017-02-18 17:56:27 +09:00
|
|
|
});
|
|
|
|
} else {
|
2017-02-27 17:08:59 +09:00
|
|
|
upload(data, iconFolder[0]);
|
2017-02-18 17:56:27 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-02-27 17:08:27 +09:00
|
|
|
|
2017-02-27 17:17:40 +09:00
|
|
|
cropper.on('skipped', () => {
|
2017-02-18 17:56:27 +09:00
|
|
|
set(file);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-02-27 17:08:59 +09:00
|
|
|
const upload = (data, folder) => {
|
2017-11-13 18:05:35 +09:00
|
|
|
const progress = (riot as any).mount(document.body.appendChild(document.createElement('mk-progress-dialog')), {
|
2017-02-18 17:56:27 +09:00
|
|
|
title: '新しいバナーをアップロードしています'
|
|
|
|
})[0];
|
2017-02-27 17:08:27 +09:00
|
|
|
|
2017-02-18 17:56:27 +09:00
|
|
|
if (folder) data.append('folder_id', folder.id);
|
2017-02-27 17:08:27 +09:00
|
|
|
|
2017-02-18 17:56:27 +09:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-11-23 05:43:00 +09:00
|
|
|
xhr.open('POST', _API_URL_ + '/drive/files/create', true);
|
2017-02-18 17:56:27 +09:00
|
|
|
xhr.onload = e => {
|
2017-11-13 18:05:35 +09:00
|
|
|
const file = JSON.parse((e.target as any).response);
|
2017-02-18 17:56:27 +09:00
|
|
|
progress.close();
|
|
|
|
set(file);
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.upload.onprogress = e => {
|
|
|
|
if (e.lengthComputable) progress.updateProgress(e.loaded, e.total);
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const set = file => {
|
|
|
|
api(I, 'i/update', {
|
|
|
|
banner_id: file.id
|
|
|
|
}).then(i => {
|
2017-12-08 02:44:50 +09:00
|
|
|
dialog('%fa:info-circle%バナーを更新しました',
|
2017-02-18 17:56:27 +09:00
|
|
|
'新しいバナーが反映されるまで時間がかかる場合があります。',
|
|
|
|
[{
|
|
|
|
text: 'わかりました。'
|
|
|
|
}]);
|
|
|
|
|
|
|
|
if (cb) cb(i);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
fileSelected(file);
|
|
|
|
} else {
|
2017-11-13 18:05:35 +09:00
|
|
|
const browser = (riot as any).mount(document.body.appendChild(document.createElement('mk-select-file-from-drive-window')), {
|
2017-02-18 17:56:27 +09:00
|
|
|
multiple: false,
|
2017-12-08 02:44:50 +09:00
|
|
|
title: '%fa:image%バナーにする画像を選択'
|
2017-02-18 17:56:27 +09:00
|
|
|
})[0];
|
|
|
|
|
|
|
|
browser.one('selected', file => {
|
|
|
|
fileSelected(file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|