|
473 | 473 | if (field.defaultValue) { |
474 | 474 | $input.val(strOrFunc(field.defaultValue)); |
475 | 475 | } |
| 476 | + } else if (field.isFileUpload) { |
| 477 | + $input = $('<input>').attr({ |
| 478 | + type: 'file', |
| 479 | + name: 'files[]' |
| 480 | + }).appendTo($value); |
| 481 | + |
| 482 | + // Add events |
| 483 | + $input.change(function(event) { |
| 484 | + $form.data('files', event.target.files); |
| 485 | + }); |
476 | 486 | } else if (field.isTokenInput) { // jquery.tokeninput.js |
477 | 487 | isAsync = true; |
478 | 488 |
|
|
672 | 682 | } |
673 | 683 | } |
674 | 684 |
|
675 | | - args.after({ |
676 | | - data: data, |
677 | | - ref: args.ref, // For backwards compatibility; use context |
678 | | - context: args.context, |
679 | | - $form: $form |
680 | | - }); |
| 685 | + var uploadFiles = function() { |
| 686 | + $form.prepend($('<div>').addClass('loading-overlay')); |
| 687 | + args.form.fileUpload.getURL({ |
| 688 | + formData: data, |
| 689 | + context: args.context, |
| 690 | + response: { |
| 691 | + success: function(successArgs) { |
| 692 | + // |
| 693 | + // Move file field into iframe; keep visible for consistency |
| 694 | + // |
| 695 | + var $uploadFrame = $('<iframe>'); |
| 696 | + var $frameForm = $('<form>').attr({ |
| 697 | + method: 'POST', |
| 698 | + action: successArgs.url, |
| 699 | + enctype: 'multipart/form-data' |
| 700 | + }); |
| 701 | + var $file = $form.find('input[type=file]'); |
| 702 | + var $field = $file.closest('.form-item .value'); |
| 703 | + |
| 704 | + $uploadFrame.css({ width: $field.outerWidth(), height: $field.height() }).show(); |
| 705 | + $frameForm.append($file); |
| 706 | + $field.append($uploadFrame); |
| 707 | + $uploadFrame.contents().find('html body').append($frameForm); |
| 708 | + $frameForm.submit(function() { |
| 709 | + $uploadFrame.load(function() { |
| 710 | + args.form.fileUpload.postUpload({ |
| 711 | + formData: data, |
| 712 | + context: args.context, |
| 713 | + response: { |
| 714 | + success: function() { |
| 715 | + args.after({ |
| 716 | + data: data, |
| 717 | + ref: args.ref, // For backwards compatibility; use context |
| 718 | + context: args.context, |
| 719 | + $form: $form |
| 720 | + }); |
| 721 | + }, |
| 722 | + error: function(msg) { |
| 723 | + $form.find('.loading-overlay').remove(); |
| 724 | + cloudStack.dialog.error({ message: msg }); |
| 725 | + } |
| 726 | + } |
| 727 | + }); |
| 728 | + }); |
| 729 | + return true; |
| 730 | + }); |
| 731 | + $frameForm.submit(); |
| 732 | + }, |
| 733 | + error: function(msg) { |
| 734 | + cloudStack.dialog.error({ message: msg }); |
| 735 | + } |
| 736 | + } |
| 737 | + }); |
| 738 | + }; |
| 739 | + |
| 740 | + if ($form.data('files')) { |
| 741 | + uploadFiles(); |
| 742 | + |
| 743 | + return false; |
| 744 | + } else { |
| 745 | + args.after({ |
| 746 | + data: data, |
| 747 | + ref: args.ref, // For backwards compatibility; use context |
| 748 | + context: args.context, |
| 749 | + $form: $form |
| 750 | + }); |
| 751 | + } |
681 | 752 |
|
682 | 753 | return true; |
683 | 754 | }; |
|
0 commit comments