uploader

class webix.ui.uploader(data)
Arguments:
  • data (object) – A configuration object

Uploader view.

References

views
button().
components
DataCollection().
helpers
_event(), assert(), bind(), delay(), extend(), isUndefined(), protoUI(), toNode(), uid().

External references

Official documentation page.

Code

  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
214
215
216
217
218
webix.protoUI({
    name:"uploader",
    defaults:{
        autosend:true,
        multiple:true,
        inputName:"upload"
    },
    $cssName:"button",
    _allowsClear:true,

    //will be redefined by upload driver
    send:function(){},
    fileDialog:function(){},
    stopUpload:function(){},

    $init:function(config){
        var driver = webix.UploadDriver.html5;
        this.files = new webix.DataCollection();

        // browser doesn't support XMLHttpRequest2
        if (webix.isUndefined(XMLHttpRequest) || webix.isUndefined((new XMLHttpRequest()).upload))
            driver = webix.UploadDriver.flash;

        webix.assert(driver,"incorrect driver");
        webix.extend(this, driver, true);
    },
    $setSize:function(x,y){
        if (webix.ui.view.prototype.$setSize.call(this,x,y)){
            this.render();
        }
    },
    apiOnly_setter:function(value){
        webix.delay(this.render, this);
        return (this.$apiOnly=value);
    },
    _add_files: function(files){
        for (var i = 0; i < files.length; i++)
            this.addFile(files[i]);

    },
    link_setter:function(value){
        if (value)
            webix.delay(function(){
                var view = webix.$$(this._settings.link);
                if (!view){
                    var top = this.getTopParentView();
                    if (top.$$)
                        view = top.$$(this._settings.link);
                }

                if (view.sync && view.filter)
                    view.sync(this.files);
                else if (view.setValues)
                    this.files.data.attachEvent("onStoreUpdated", function(){
                        view.setValues(this);
                    });
                view._settings.uploader = this._settings.id;
            }, this);
        return value;
    },
    addFile:function(name, size, type, extra){
        var file = null;
        if (typeof name == "object"){
            file = name;
            name = file.name;
            size = file.size;
        }

        var format = this._format_size(size);
        type = type || name.split(".").pop();

        var file_struct = {
            file: file,
            name: name,
            id: webix.uid(),
            size: size,
            sizetext: format,
            type: type,
            context: this._last_file_context,
            status: "client"
        };

        if (this._settings.directory && file.webkitRelativePath)
            file_struct.name = file.webkitRelativePath;

        if (extra)
            webix.extend(file_struct, extra, true);

        if (this.callEvent("onBeforeFileAdd", [file_struct])){
            if (!this._settings.multiple)
                this.files.clearAll();

            var id = this.files.add(file_struct);
            this.callEvent("onAfterFileAdd", [file_struct]);
            if (id && this._settings.autosend)
                this.send(id);
        }

        return file_struct;
    },

    _get_active_url:function(item){
        var url = this._settings.upload;
        var urldata = webix.extend(item.urlData||{},this._settings.urlData||{});
        if (url && urldata){
            var subline = [];
            for (var key in urldata)
                subline.push(encodeURIComponent(key)+"="+encodeURIComponent(urldata[key]));

            if (subline.length)
                url += ((url.indexOf("?") ==-1) ? "?" : "&") + subline.join("&");
        }
        return url;
    },

    addDropZone:function(id, hover_text){
        var node = webix.toNode(id);
        var extra_css = "";
        if (hover_text)
            extra_css = " "+webix.html.createCss({ content:'"'+hover_text+'"' }, ":before");

        var fullcss = "webix_drop_file"+extra_css;
        var timer = null;

        //web
        webix._event(node,"dragover", webix.html.preventEvent);
        webix._event(node,"dragover", function(e){
            webix.html.addCss(node, fullcss, true);
            if (timer){
                clearTimeout(timer);
                timer = null;
            }
        });
        webix._event(node,"dragleave", function(e){
            //when moving over html child elements
            //browser will issue dragleave and dragover events
            //ignore first one
            timer = setTimeout(function(){
                webix.html.removeCss(node, fullcss);
            }, 150);
        });

        webix._event(node,"drop", webix.bind(function(e){
            webix.html.removeCss(node, fullcss);
            this._drop(e);
            return webix.html.preventEvent(e);
        }, this));
    },

    _format_size: function(size) {
        var index = 0;
        while (size > 1024){
            index++;
            size = size/1024;
        }
        return Math.round(size*100)/100+" "+webix.i18n.fileSize[index];
    },

    _complete: function(id, response) {
        if (response.status != 'error') {
            var item = this.files.getItem(id);

            item.status = "server";
            item.progress = 100;
            webix.extend(item, response, true);

            this.callEvent("onFileUpload", [item, response]);
            this.callEvent("onChange", []);
            this.files.updateItem(id);
        }

        if (this.isUploaded())
            this._upload_complete(response);
    },
    _upload_complete:function(response){
        this.callEvent("onUploadComplete", [response]);
        if (this._last_assigned_upload_callback){
            this._last_assigned_upload_callback.call(this, response);
            this._last_assigned_upload_callback = 0;
        }
    },
    isUploaded:function(){
        var order = this.files.data.order;
        for (var i=0; i<order.length; i++)
            if (this.files.getItem(order[i]).status != "server")
                return false;

        return true;
    },
    $onUploadComplete: function(){

    },
    $updateProgress: function(id, percent) {
        var item = this.files.getItem(id);
        item.percent = Math.round(percent);
        this.files.updateItem(id);
    },
    setValue:function(value){
        if (typeof value == "string")
            value = { value:value, status:"server" };

        this.files.clearAll();
        if (value)
            this.files.parse(value);

        this.callEvent("onChange", []);
    },
    getValue:function(){
        var data = [];
        this.files.data.each(function(obj){
            if (obj.status == "server")
                data.push(obj.value||obj.name);
        });

        return data.join(",");
    }

}, webix.ui.button);