TaskAttachment.js 8.4 KB
Newer Older
R
roo00 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
MWF.require("MWF.widget.AttachmentController", null,false);

MWF.xApplication.TeamWork.TaskAttachment = new Class({
    Implements: [Options, Events],
    options: {
        "documentId" : "",
        "isNew": false,
        "isEdited" : true,
        "size" : "max",
        "isSizeChange" : true
    },

    initialize: function (node, app, actions, lp, options) {
        this.setOptions(options);
        this.app = app;
        this.node = $(node);
        this.actions = actions;
        this.lp = lp;
    },

    load: function () {
        this.loadAttachmentController();
        if(this.options.size=="min"){
            if(this.attachmentController.minActionAreaNode){
                this.attachmentController.minActionAreaNode.setStyles({
                    "float":"left",
                    //"margin-top":"10px"
                });
            }
        }
    },
    loadAttachmentController: function () {
        var options = {
            "style": "default",
            "title": "",
            "listStyle": "list",
            "size": this.options.size ,
            "resize": true,
            //"attachmentCount": this.json.attachmentCount || 0,
            "isUpload": (this.options.isNew || this.options.isEdited) ? true : false,
            "isDelete": (this.options.isNew || this.options.isEdited) ? true : false,
            "isReplace": false,
            "isDownload": true,
            "isSizeChange": false,
            "readonly": (!this.options.isNew && !this.options.isEdited ) ? true : false
        };
        this.attachmentController = new MWF.widget.ATTER(this.node, this, options);
        this.attachmentController.load();

        this.listAttachment( function( json ){
            json.data.each(function (att) {
                this.attachmentController.addAttachment(att);
            }.bind(this));
        }.bind(this))
    },
    transportData : function( json ){
        if( typeOf(json.data) == "array" ){
            json.data.each(function(d){
                d.person = d.creatorUid;
                d.lastUpdateTime = d.updateTime;
            })
        }else if( typeOf(json.data) == "object" ){
            var d = json.data;
            d.person = d.creatorUid;
            d.lastUpdateTime = d.updateTime;
        }else{
            json.each(function(d){
                d.person = d.creatorUid;
                d.lastUpdateTime = d.updateTime;
            })
        }
        return json;
    },
    listAttachment: function( callback ){
        this.actions.attachmentListByTaskId(this.options.documentId, function(json){
            if(callback)callback(this.transportData(json));
        }.bind(this))
    },

    createUploadFileNode: function () {
        this.uploadFileAreaNode = new Element("div");
        var html = "<input name=\"file\" type=\"file\" multiple/>";
        this.uploadFileAreaNode.set("html", html);

        this.fileUploadNode = this.uploadFileAreaNode.getFirst();
        this.fileUploadNode.addEvent("change", function () {
            this.isQueryUploadSuccess = true;
            this.fireEvent( "queryUploadAttachment" );
            if( this.isQueryUploadSuccess ){
                var files = this.fileUploadNode.files;
                if (files.length) {
                    for (var i = 0; i < files.length; i++) {
                        var file = files.item(i);
                        var formData = new FormData();
                        formData.append('file', file);

                        formData.append('site', this.options.documentId);
                        this.actions.attachmentTaskUpload(this.options.documentId, function (json) {
                            if(json.type=="success"){
                                if(json.id){
                                    this.actions.attachmentGet(json.id,  function (json) {
                                        json = this.transportData(json);
                                        if (json.data) {
                                            this.attachmentController.addAttachment(json.data);
                                            //this.attachmentList.push(json.data);
                                        }
                                        this.attachmentController.checkActions();

                                        this.fireEvent("upload", [json.data]);
                                    }.bind(this))
                                }
                            }

                            this.attachmentController.checkActions();
                        }.bind(this), null, formData, file,this.options.documentId);

                    }
                }
            }else{
                this.uploadFileAreaNode.destroy();
                this.uploadFileAreaNode = false;
            }
        }.bind(this));
    },
    uploadAttachment: function (e, node) {
        if (!this.uploadFileAreaNode) {
            this.createUploadFileNode();
        }
        this.fileUploadNode.click();
    },
    deleteAttachments: function (e, node, attachments) {
        var names = [];
        attachments.each(function (attachment) {
            names.push(attachment.data.name);
        }.bind(this));

        var _self = this;
        this.app.confirm("warn", e, this.lp.common.confirm.removeTitle, this.lp.common.confirm.removeContent + "( " + names.join(", ") + " )", 300, 120, function () {
            while (attachments.length) {
                attachment = attachments.shift();
                _self.deleteAttachment(attachment);
            }
            this.close();
        }, function () {
            this.close();
        }, null);
    },
    deleteAttachment: function (attachment) {
        this.fireEvent("delete", [attachment.data]);
        this.actions.attachmentRemove(attachment.data.id, function (json) {
            this.attachmentController.removeAttachment(attachment);
            //this.form.businessData.attachmentList.erase( attachment.data )
            this.attachmentController.checkActions();
        }.bind(this));
    },
    replaceAttachment: function (e, node, attachment) {
        var _self = this;
        this.form.confirm("warn", e, this.lp.common.confirm.replaceTitle, this.lp.common.confirm.replaceContent + "( " + attachment.data.name + " )", 300, 120, function () {
            _self.replaceAttachmentFile(attachment);
            this.close();
        }, function () {
            this.close();
        }, null);
    },

    createReplaceFileNode: function (attachment) {
        this.replaceFileAreaNode = new Element("div");
        var html = "<input name=\"file\" type=\"file\" multiple/>";
        this.replaceFileAreaNode.set("html", html);

        this.fileReplaceNode = this.replaceFileAreaNode.getFirst();
        this.fileReplaceNode.addEvent("change", function () {

            var files = this.fileReplaceNode.files;
            if (files.length) {
                for (var i = 0; i < files.length; i++) {
                    var file = files.item(i);

                    var formData = new FormData();
                    formData.append('file', file);
                    //    formData.append('site', this.json.id);

                    this.actions.replaceAttachment(attachment.data.id, this.options.documentId, function (o, text) {
                        this.form.documentAction.getAttachment(attachment.data.id, this.options.documentId, function (json) {
                            attachment.data = json.data;
                            attachment.reload();
                            this.attachmentController.checkActions();
                        }.bind(this))
                    }.bind(this), null, formData, file);
                }
            }
        }.bind(this));
    },
    replaceAttachmentFile: function (attachment) {
        if (!this.replaceFileAreaNode) {
            this.createReplaceFileNode(attachment);
        }
        this.fileReplaceNode.click();
    },
    downloadAttachment: function (e, node, attachments) {
        attachments.each(function (att) {
            this.actions.attachmentDownloadStream(att.data.id, this.options.documentId);
        }.bind(this));
    },
    openAttachment: function (e, node, attachments) {
        attachments.each(function (att) {
            this.actions.attachmentDownloadStream(att.data.id, this.options.documentId);
        }.bind(this));
    },
    getAttachmentUrl: function (attachment, callback) {
        this.actions.attachmentDownloadUrl(attachment.data.id, this.options.documentId, callback);
    }
});