1 /** 2 * @fileOverview 3 * @author <a href="https://www.labkey.org">LabKey</a> (<a href="mailto:info@labkey.com">info@labkey.com</a>) 4 * @license Copyright (c) 2018 LabKey Corporation 5 * <p/> 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * <p/> 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * <p/> 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * <p/> 18 */ 19 LABKEY.Assay = new function(impl) { 20 21 /** 22 * Documentation specified in core/Assay.js -- search for "@name importRun" 23 */ 24 impl.importRun = function(config) { 25 if (!window.FormData) 26 throw new Error("modern browser required"); 27 28 if (!config.assayId) 29 throw new Error("assayId required"); 30 31 var files = []; 32 if (config.files) { 33 for (var i = 0; i < config.files.length; i++) { 34 var f = config.files[i]; 35 if (f instanceof window.File) { 36 files.push(f); 37 } 38 else if (f.tagName == "INPUT") { 39 for (var j = 0; j < f.files.length; j++) { 40 files.push(f.files[j]); 41 } 42 } 43 } 44 } 45 46 if (files.length == 0 && !config.runFilePath && !config.dataRows) 47 throw new Error("At least one of 'file', 'runFilePath', or 'dataRows' is required"); 48 49 if ((files.length > 0 ? 1 : 0) + (config.runFilePath ? 1 : 0) + (config.dataRows ? 1 : 0) > 1) 50 throw new Error("Only one of 'file', 'runFilePath', or 'dataRows' is allowed"); 51 52 var formData = new FormData(); 53 formData.append("assayId", config.assayId); 54 if (config.name) 55 formData.append("name", config.name); 56 if (config.comment) 57 formData.append("comment", config.comment); 58 if (config.batchId) 59 formData.append("batchId", config.batchId); 60 61 if (config.properties) { 62 for (var key in config.properties) { 63 if (LABKEY.Utils.isObject(config.properties[key])) 64 formData.append("properties['" + key + "']", JSON.stringify(config.properties[key])); 65 else 66 formData.append("properties['" + key + "']", config.properties[key]); 67 } 68 } 69 70 if (config.batchProperties) { 71 for (var key in config.batchProperties) { 72 if (LABKEY.Utils.isObject(config.batchProperties[key])) 73 formData.append("batchProperties['" + key + "']", JSON.stringify(config.batchProperties[key])); 74 else 75 formData.append("batchProperties['" + key + "']", config.batchProperties[key]); 76 } 77 } 78 79 if (config.dataRows) 80 formData.append("dataRows", JSON.stringify(config.dataRows)); 81 82 if (config.runFilePath) 83 formData.append("runFilePath", config.runFilePath); 84 85 if (files && files.length > 0) { 86 formData.append("file", files[0]); 87 for (var i = 1; i < files.length; i++) { 88 formData.append("file" + i, files[i]); 89 } 90 } 91 92 LABKEY.Ajax.request({ 93 method: 'POST', 94 url: LABKEY.ActionURL.buildURL("assay", "importRun.api", config.containerPath), 95 success: LABKEY.Utils.getCallbackWrapper(LABKEY.Utils.getOnSuccess(config), config.scope, false), 96 failure: LABKEY.Utils.getCallbackWrapper(LABKEY.Utils.getOnFailure(config), config.scope, true), 97 form: formData 98 }); 99 }; 100 101 return impl; 102 103 }(LABKEY.Assay || new function() { return {}; });