2018-02-20 11:53:34 -06:00
|
|
|
import OS from '../../common/mios';
|
|
|
|
import { apiUrl } from '../../config';
|
|
|
|
import CropWindow from '../views/components/crop-window.vue';
|
|
|
|
import ProgressDialog from '../views/components/progress-dialog.vue';
|
|
|
|
|
2018-04-08 10:33:56 -05:00
|
|
|
export default (os: OS) => {
|
2018-02-20 11:53:34 -06:00
|
|
|
|
2018-04-08 10:33:56 -05:00
|
|
|
const cropImage = file => new Promise((resolve, reject) => {
|
2018-02-20 11:53:34 -06:00
|
|
|
const w = new CropWindow({
|
|
|
|
propsData: {
|
2018-02-20 14:55:19 -06:00
|
|
|
image: file,
|
2018-02-20 11:53:34 -06:00
|
|
|
title: 'バナーとして表示する部分を選択',
|
|
|
|
aspectRatio: 16 / 9
|
|
|
|
}
|
|
|
|
}).$mount();
|
|
|
|
|
|
|
|
w.$once('cropped', blob => {
|
|
|
|
const data = new FormData();
|
2018-04-07 13:58:11 -05:00
|
|
|
data.append('i', os.i.token);
|
2018-02-20 11:53:34 -06:00
|
|
|
data.append('file', blob, file.name + '.cropped.png');
|
|
|
|
|
|
|
|
os.api('drive/folders/find', {
|
|
|
|
name: 'バナー'
|
|
|
|
}).then(bannerFolder => {
|
|
|
|
if (bannerFolder.length === 0) {
|
|
|
|
os.api('drive/folders/create', {
|
|
|
|
name: 'バナー'
|
|
|
|
}).then(iconFolder => {
|
2018-04-08 10:33:56 -05:00
|
|
|
resolve(upload(data, iconFolder));
|
2018-02-20 11:53:34 -06:00
|
|
|
});
|
|
|
|
} else {
|
2018-04-08 10:33:56 -05:00
|
|
|
resolve(upload(data, bannerFolder[0]));
|
2018-02-20 11:53:34 -06:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
w.$once('skipped', () => {
|
2018-04-08 10:33:56 -05:00
|
|
|
resolve(file);
|
2018-02-20 11:53:34 -06:00
|
|
|
});
|
|
|
|
|
2018-04-08 10:33:56 -05:00
|
|
|
w.$once('cancelled', reject);
|
|
|
|
|
2018-02-20 11:53:34 -06:00
|
|
|
document.body.appendChild(w.$el);
|
2018-04-08 10:33:56 -05:00
|
|
|
});
|
2018-02-20 11:53:34 -06:00
|
|
|
|
2018-04-08 10:33:56 -05:00
|
|
|
const upload = (data, folder) => new Promise((resolve, reject) => {
|
2018-02-20 11:53:34 -06:00
|
|
|
const dialog = new ProgressDialog({
|
|
|
|
propsData: {
|
|
|
|
title: '新しいバナーをアップロードしています'
|
|
|
|
}
|
|
|
|
}).$mount();
|
|
|
|
document.body.appendChild(dialog.$el);
|
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
if (folder) data.append('folderId', folder.id);
|
2018-02-20 11:53:34 -06:00
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open('POST', apiUrl + '/drive/files/create', true);
|
|
|
|
xhr.onload = e => {
|
|
|
|
const file = JSON.parse((e.target as any).response);
|
|
|
|
(dialog as any).close();
|
2018-04-08 10:33:56 -05:00
|
|
|
resolve(file);
|
2018-02-20 11:53:34 -06:00
|
|
|
};
|
2018-04-08 10:33:56 -05:00
|
|
|
xhr.onerror = reject;
|
2018-02-20 11:53:34 -06:00
|
|
|
|
|
|
|
xhr.upload.onprogress = e => {
|
2018-02-20 14:55:19 -06:00
|
|
|
if (e.lengthComputable) (dialog as any).update(e.loaded, e.total);
|
2018-02-20 11:53:34 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(data);
|
2018-04-08 10:33:56 -05:00
|
|
|
});
|
2018-02-20 11:53:34 -06:00
|
|
|
|
2018-04-08 10:33:56 -05:00
|
|
|
const setBanner = file => {
|
|
|
|
return os.api('i/update', {
|
2018-03-29 00:48:47 -05:00
|
|
|
bannerId: file.id
|
2018-02-20 11:53:34 -06:00
|
|
|
}).then(i => {
|
2018-03-29 00:48:47 -05:00
|
|
|
os.i.bannerId = i.bannerId;
|
|
|
|
os.i.bannerUrl = i.bannerUrl;
|
2018-02-20 14:55:19 -06:00
|
|
|
|
2018-02-20 11:53:34 -06:00
|
|
|
os.apis.dialog({
|
|
|
|
title: '%fa:info-circle%バナーを更新しました',
|
|
|
|
text: '新しいバナーが反映されるまで時間がかかる場合があります。',
|
|
|
|
actions: [{
|
|
|
|
text: 'わかった'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
2018-04-08 10:33:56 -05:00
|
|
|
return i;
|
2018-02-20 11:53:34 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-04-08 10:33:56 -05:00
|
|
|
return (file = null) => {
|
|
|
|
const selectedFile = file
|
|
|
|
? Promise.resolve(file)
|
|
|
|
: os.apis.chooseDriveFile({
|
|
|
|
multiple: false,
|
|
|
|
title: '%fa:image%バナーにする画像を選択'
|
|
|
|
});
|
|
|
|
|
|
|
|
return selectedFile
|
|
|
|
.then(cropImage)
|
|
|
|
.then(setBanner)
|
|
|
|
.catch(err => err && console.warn(err));
|
|
|
|
};
|
2018-02-20 11:53:34 -06:00
|
|
|
};
|