1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-26 23:40:57 +01:00
phorge-phorge/webroot/rsrc/js/phui/behavior-phui-file-upload.js
epriestley f2c36a934e Provide an <input type="file"> control in Remarkup for mobile and users with esoteric windowing systems
Summary:
Ref T5187. This definitely feels a bit flimsy and I'm going to hold it until I cut the release since it changes a couple of things about Workflow in general, but it seems to work OK and most of it is fine.

The intent is described in T5187#176236.

In practice, most of that works like I describe, then the `phui-file-upload` behavior gets some weird glue to figure out if the input is part of the form. Not the most elegant system, but I think it'll hold until we come up with many reasons to write a lot more Javascript.

Test Plan:
Used both drag-and-drop and the upload dialog to upload files in Safari, Firefox and Chrome.

{F1653716}

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T5187

Differential Revision: https://secure.phabricator.com/D15953
2016-05-20 16:24:22 -07:00

80 lines
1.5 KiB
JavaScript

/**
* @provides javelin-behavior-phui-file-upload
* @requires javelin-behavior
* javelin-stratcom
* javelin-dom
* phuix-dropdown-menu
*/
JX.behavior('phui-file-upload', function(config) {
function startUpload(workflow, input) {
var files = input.files;
if (!files || !files.length) {
return;
}
var state = {
workflow: workflow,
input: input,
waiting: 0,
phids: []
};
var callback = JX.bind(null, didUpload, state);
var dummy = input;
var uploader = new JX.PhabricatorDragAndDropFileUpload(dummy)
.setURI(config.uploadURI)
.setChunkThreshold(config.chunkThreshold);
uploader.listen('didUpload', callback);
uploader.start();
workflow.pause();
for (var ii = 0; ii < files.length; ii++) {
state.waiting++;
uploader.sendRequest(files[ii]);
}
}
function didUpload(state, file) {
state.phids.push(file.getPHID());
state.waiting--;
if (state.waiting) {
return;
}
state.workflow
.addData(config.inputName, state.phids.join(', '))
.resume();
}
JX.Workflow.listen('start', function(workflow) {
var form = workflow.getSourceForm();
if (!form) {
return;
}
var input;
try {
input = JX.$(config.fileInputID);
} catch (ex) {
return;
}
var local_form = JX.DOM.findAbove(input, 'form');
if (!local_form) {
return;
}
if (local_form !== form) {
return;
}
startUpload(workflow, input);
});
});