2016-07-07 05:03:30 +08:00
|
|
|
import ko from 'ko';
|
2020-10-03 20:02:33 +08:00
|
|
|
import { pInt } from 'Common/Utils';
|
2020-10-02 20:44:29 +08:00
|
|
|
import { File } from 'Common/File';
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { AbstractModel } from 'Knoin/AbstractModel';
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
class ComposeAttachmentModel extends AbstractModel {
|
2016-07-07 05:03:30 +08:00
|
|
|
/**
|
|
|
|
* @param {string} id
|
|
|
|
* @param {string} fileName
|
|
|
|
* @param {?number=} size = null
|
|
|
|
* @param {boolean=} isInline = false
|
|
|
|
* @param {boolean=} isLinked = false
|
|
|
|
* @param {string=} CID = ''
|
|
|
|
* @param {string=} contentLocation = ''
|
|
|
|
*/
|
2019-07-05 03:19:24 +08:00
|
|
|
constructor(id, fileName, size = null, isInline = false, isLinked = false, CID = '', contentLocation = '') {
|
2016-07-07 05:03:30 +08:00
|
|
|
super('ComposeAttachmentModel');
|
|
|
|
|
|
|
|
this.id = id;
|
|
|
|
this.isInline = !!isInline;
|
|
|
|
this.isLinked = !!isLinked;
|
|
|
|
this.CID = CID;
|
|
|
|
this.contentLocation = contentLocation;
|
|
|
|
this.fromMessage = false;
|
|
|
|
|
|
|
|
this.fileName = ko.observable(fileName);
|
|
|
|
this.size = ko.observable(size);
|
|
|
|
this.tempName = ko.observable('');
|
|
|
|
|
|
|
|
this.progress = ko.observable(0);
|
|
|
|
this.error = ko.observable('');
|
|
|
|
this.waiting = ko.observable(true);
|
|
|
|
this.uploading = ko.observable(false);
|
|
|
|
this.enabled = ko.observable(true);
|
|
|
|
this.complete = ko.observable(false);
|
|
|
|
|
|
|
|
this.progressText = ko.computed(() => {
|
|
|
|
const p = this.progress();
|
|
|
|
return 0 === p ? '' : '' + (98 < p ? 100 : p) + '%';
|
|
|
|
});
|
|
|
|
|
|
|
|
this.progressStyle = ko.computed(() => {
|
|
|
|
const p = this.progress();
|
|
|
|
return 0 === p ? '' : 'width:' + (98 < p ? 100 : p) + '%';
|
|
|
|
});
|
|
|
|
|
|
|
|
this.title = ko.computed(() => {
|
2020-07-28 23:20:14 +08:00
|
|
|
return this.error() || this.fileName();
|
2016-07-07 05:03:30 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.friendlySize = ko.computed(() => {
|
|
|
|
const localSize = this.size();
|
2020-10-03 20:02:33 +08:00
|
|
|
return null === localSize ? '' : File.friendlySize(localSize);
|
2016-07-07 05:03:30 +08:00
|
|
|
});
|
|
|
|
|
2020-10-02 20:44:29 +08:00
|
|
|
this.mimeType = ko.computed(() => File.getContentType(this.fileName()));
|
|
|
|
this.fileExt = ko.computed(() => File.getExtension(this.fileName()));
|
2016-07-07 05:03:30 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
this.regDisposables([
|
|
|
|
this.progressText,
|
|
|
|
this.progressStyle,
|
|
|
|
this.title,
|
|
|
|
this.friendlySize,
|
|
|
|
this.mimeType,
|
|
|
|
this.fileExt
|
|
|
|
]);
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
2020-09-19 19:53:14 +08:00
|
|
|
static fromAttachment(item)
|
|
|
|
{
|
|
|
|
const attachment = new ComposeAttachmentModel(
|
|
|
|
item.download,
|
|
|
|
item.fileName,
|
|
|
|
item.estimatedSize,
|
|
|
|
item.isInline,
|
|
|
|
item.isLinked,
|
|
|
|
item.cid,
|
|
|
|
item.contentLocation
|
|
|
|
);
|
|
|
|
attachment.fromMessage = true;
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
2016-07-07 05:03:30 +08:00
|
|
|
/**
|
|
|
|
* @param {AjaxJsonComposeAttachment} json
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
initByUploadJson(json) {
|
|
|
|
let bResult = false;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (json) {
|
2016-07-07 05:03:30 +08:00
|
|
|
this.fileName(json.Name);
|
2020-07-30 03:49:41 +08:00
|
|
|
this.size(undefined === json.Size ? 0 : pInt(json.Size));
|
|
|
|
this.tempName(undefined === json.TempName ? '' : json.TempName);
|
2016-07-07 05:03:30 +08:00
|
|
|
this.isInline = false;
|
|
|
|
|
|
|
|
bResult = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
iconClass() {
|
2020-10-02 20:44:29 +08:00
|
|
|
return File.getIconClass(this.fileExt(), this.mimeType())[0];
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
iconText() {
|
2020-10-02 20:44:29 +08:00
|
|
|
return File.getIconClass(this.fileExt(), this.mimeType())[1];
|
2016-07-07 05:03:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
export { ComposeAttachmentModel, ComposeAttachmentModel as default };
|