OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 |
| 7 <!-- |
| 8 /** |
| 9 * @module Polymer Elements |
| 10 */ |
| 11 |
| 12 /** |
| 13 * polymer-file provides File API operations. |
| 14 * |
| 15 * Examples: |
| 16 * |
| 17 * <polymer-file id="file" readas="dataurl" |
| 18 * on-polymer-result="{{handleResult}}"></polymer-file> |
| 19 * ... |
| 20 * this.$.file.blob = new Blob(['abc'], {type: 'text/plain'}); |
| 21 * this.$.file.read(); |
| 22 * |
| 23 * --- |
| 24 * |
| 25 * <polymer-file id="file" readas="arraybuffer" auto |
| 26 * result="{{result}}"></polymer-file> |
| 27 * ... |
| 28 * this.$.file.blob = new Blob(['abc'], {type: 'text/plain'}); |
| 29 * |
| 30 * @class polymer-file |
| 31 */ |
| 32 /** |
| 33 * Fired when a file read has complete. |
| 34 * |
| 35 * @event polymer-result |
| 36 * @param {Object} detail |
| 37 * @param {Object} detail.result The result of the read. |
| 38 */ |
| 39 /** |
| 40 * Fired if there is an error in the file read. |
| 41 * |
| 42 * @event polymer-error |
| 43 * @param {Object} detail |
| 44 * @param {Object} detail.error Information on the error. |
| 45 * |
| 46 */ |
| 47 --> |
| 48 <link rel="import" href="../polymer/polymer.html"> |
| 49 |
| 50 <polymer-element name="polymer-file" attributes="blob result auto readas"> |
| 51 <template> |
| 52 <style> |
| 53 :host { |
| 54 display: none; |
| 55 } |
| 56 </style> |
| 57 </template> |
| 58 <script> |
| 59 Polymer('polymer-file', { |
| 60 /** |
| 61 * Contains the result of a read operation. |
| 62 * |
| 63 * @attribute result |
| 64 * @type Blob|File |
| 65 * @default null |
| 66 */ |
| 67 result: null, |
| 68 /** |
| 69 * The Blob-like object to read. |
| 70 * |
| 71 * @attribute blob |
| 72 * @type Blob|File |
| 73 * @default null |
| 74 */ |
| 75 blob: null, |
| 76 /** |
| 77 * If true, automatically performs the file read (if a blob has been set). |
| 78 * |
| 79 * @attribute auto |
| 80 * @type boolean |
| 81 * @default false |
| 82 */ |
| 83 auto: false, |
| 84 /** |
| 85 * The format the result should be returned as. One of 'arraybuffer', 'text', |
| 86 * 'dataurl', 'binarystring'. |
| 87 * |
| 88 * @attribute readas |
| 89 * @type string |
| 90 * @default 'text' |
| 91 */ |
| 92 readas: 'text', |
| 93 blobChanged: function() { |
| 94 // result is set at end of microtask in read. This won't call resultChanged. |
| 95 this.result = null; |
| 96 if (this.auto) { |
| 97 this.read(); |
| 98 } |
| 99 }, |
| 100 resultChanged: function() { |
| 101 this.fire('polymer-result', {result: this.result}); |
| 102 }, |
| 103 read: function() { |
| 104 // Send back same result if blob hasn't changed. |
| 105 if (this.result || !this.blob) { |
| 106 // Wrap in asyncMethod for situations where read() is called immediately. |
| 107 this.asyncMethod(function() { |
| 108 this.fire('polymer-result', {result: this.result}); |
| 109 }); |
| 110 return; |
| 111 } |
| 112 |
| 113 // TODO: reader.abort() a in-flight read. |
| 114 var reader = new FileReader(); |
| 115 reader.onload = function(e) { |
| 116 this.result = e.target.result; |
| 117 }.bind(this); |
| 118 reader.onerror = function(e) { |
| 119 this.fire('polymer-error', {error: e.target.error}); |
| 120 }.bind(this); |
| 121 |
| 122 switch(this.readas) { |
| 123 case 'dataurl': |
| 124 reader.readAsDataURL(this.blob); |
| 125 break; |
| 126 case 'arraybuffer': |
| 127 reader.readAsArrayBuffer(this.blob); |
| 128 break; |
| 129 case 'binarystring': |
| 130 reader.readAsBinaryString(this.blob); |
| 131 break; |
| 132 case 'text': |
| 133 // Default to text. |
| 134 default: |
| 135 reader.readAsText(this.blob); |
| 136 break; |
| 137 } |
| 138 } |
| 139 }); |
| 140 </script> |
| 141 </polymer-element> |
OLD | NEW |