|
12 | 12 | /*global window, document, loadImage, HTMLCanvasElement, $ */ |
13 | 13 |
|
14 | 14 | $(function () { |
15 | | - 'use strict'; |
| 15 | + 'use strict' |
16 | 16 |
|
17 | | - var result = $('#result'), |
18 | | - exifNode = $('#exif'), |
19 | | - thumbNode = $('#thumbnail'), |
20 | | - actionsNode = $('#actions'), |
21 | | - currentFile, |
22 | | - replaceResults = function (img) { |
23 | | - var content; |
24 | | - if (!(img.src || img instanceof HTMLCanvasElement)) { |
25 | | - content = $('<span>Loading image file failed</span>'); |
26 | | - } else { |
27 | | - content = $('<a target="_blank">').append(img) |
28 | | - .attr('download', currentFile.name) |
29 | | - .attr('href', img.src || img.toDataURL()); |
30 | | - } |
31 | | - result.children().replaceWith(content); |
32 | | - if (img.getContext) { |
33 | | - actionsNode.show(); |
34 | | - } |
35 | | - }, |
36 | | - displayImage = function (file, options) { |
37 | | - currentFile = file; |
38 | | - if (!loadImage( |
39 | | - file, |
40 | | - replaceResults, |
41 | | - options |
42 | | - )) { |
43 | | - result.children().replaceWith( |
44 | | - $('<span>Your browser does not support the URL or FileReader API.</span>') |
45 | | - ); |
46 | | - } |
47 | | - }, |
48 | | - displayExifData = function (exif) { |
49 | | - var thumbnail = exif.get('Thumbnail'), |
50 | | - tags = exif.getAll(), |
51 | | - table = exifNode.find('table').empty(), |
52 | | - row = $('<tr></tr>'), |
53 | | - cell = $('<td></td>'), |
54 | | - prop; |
55 | | - if (thumbnail) { |
56 | | - thumbNode.empty(); |
57 | | - loadImage(thumbnail, function (img) { |
58 | | - thumbNode.append(img).show(); |
59 | | - }, {orientation: exif.get('Orientation')}); |
60 | | - } |
61 | | - for (prop in tags) { |
62 | | - if (tags.hasOwnProperty(prop)) { |
63 | | - table.append( |
64 | | - row.clone() |
65 | | - .append(cell.clone().text(prop)) |
66 | | - .append(cell.clone().text(tags[prop])) |
67 | | - ); |
68 | | - } |
69 | | - } |
70 | | - exifNode.show(); |
71 | | - }, |
72 | | - dropChangeHandler = function (e) { |
73 | | - e.preventDefault(); |
74 | | - e = e.originalEvent; |
75 | | - var target = e.dataTransfer || e.target, |
76 | | - file = target && target.files && target.files[0], |
77 | | - options = { |
78 | | - maxWidth: result.width(), |
79 | | - canvas: true |
80 | | - }; |
81 | | - if (!file) { |
82 | | - return; |
83 | | - } |
84 | | - exifNode.hide(); |
85 | | - thumbNode.hide(); |
86 | | - loadImage.parseMetaData(file, function (data) { |
87 | | - if (data.exif) { |
88 | | - options.orientation = data.exif.get('Orientation'); |
89 | | - displayExifData(data.exif); |
90 | | - } |
91 | | - displayImage(file, options); |
92 | | - }); |
93 | | - }, |
94 | | - coordinates; |
95 | | - // Hide URL/FileReader API requirement message in capable browsers: |
96 | | - if (window.createObjectURL || window.URL || window.webkitURL || window.FileReader) { |
97 | | - result.children().hide(); |
| 17 | + var result = $('#result') |
| 18 | + var exifNode = $('#exif') |
| 19 | + var thumbNode = $('#thumbnail') |
| 20 | + var actionsNode = $('#actions') |
| 21 | + var currentFile |
| 22 | + var coordinates |
| 23 | + function replaceResults (img) { |
| 24 | + var content |
| 25 | + if (!(img.src || img instanceof HTMLCanvasElement)) { |
| 26 | + content = $('<span>Loading image file failed</span>') |
| 27 | + } else { |
| 28 | + content = $('<a target="_blank">').append(img) |
| 29 | + .attr('download', currentFile.name) |
| 30 | + .attr('href', img.src || img.toDataURL()) |
98 | 31 | } |
99 | | - $(document) |
100 | | - .on('dragover', function (e) { |
101 | | - e.preventDefault(); |
102 | | - e = e.originalEvent; |
103 | | - e.dataTransfer.dropEffect = 'copy'; |
104 | | - }) |
105 | | - .on('drop', dropChangeHandler); |
106 | | - $('#file-input').on('change', dropChangeHandler); |
107 | | - $('#edit').on('click', function (event) { |
108 | | - event.preventDefault(); |
109 | | - var imgNode = result.find('img, canvas'), |
110 | | - img = imgNode[0]; |
111 | | - imgNode.Jcrop({ |
112 | | - setSelect: [40, 40, img.width - 40, img.height - 40], |
113 | | - onSelect: function (coords) { |
114 | | - coordinates = coords; |
115 | | - }, |
116 | | - onRelease: function () { |
117 | | - coordinates = null; |
118 | | - } |
119 | | - }).parent().on('click', function (event) { |
120 | | - event.preventDefault(); |
121 | | - }); |
122 | | - }); |
123 | | - $('#crop').on('click', function (event) { |
124 | | - event.preventDefault(); |
125 | | - var img = result.find('img, canvas')[0]; |
126 | | - if (img && coordinates) { |
127 | | - replaceResults(loadImage.scale(img, { |
128 | | - left: coordinates.x, |
129 | | - top: coordinates.y, |
130 | | - sourceWidth: coordinates.w, |
131 | | - sourceHeight: coordinates.h, |
132 | | - minWidth: result.width() |
133 | | - })); |
134 | | - coordinates = null; |
135 | | - } |
136 | | - }); |
137 | | - |
138 | | -}); |
| 32 | + result.children().replaceWith(content) |
| 33 | + if (img.getContext) { |
| 34 | + actionsNode.show() |
| 35 | + } |
| 36 | + } |
| 37 | + function displayImage (file, options) { |
| 38 | + currentFile = file |
| 39 | + if (!loadImage( |
| 40 | + file, |
| 41 | + replaceResults, |
| 42 | + options |
| 43 | + )) { |
| 44 | + result.children().replaceWith( |
| 45 | + $('<span>Your browser does not support the URL or FileReader API.</span>') |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + function displayExifData (exif) { |
| 50 | + var thumbnail = exif.get('Thumbnail') |
| 51 | + var tags = exif.getAll() |
| 52 | + var table = exifNode.find('table').empty() |
| 53 | + var row = $('<tr></tr>') |
| 54 | + var cell = $('<td></td>') |
| 55 | + var prop |
| 56 | + if (thumbnail) { |
| 57 | + thumbNode.empty() |
| 58 | + loadImage(thumbnail, function (img) { |
| 59 | + thumbNode.append(img).show() |
| 60 | + }, {orientation: exif.get('Orientation')}) |
| 61 | + } |
| 62 | + for (prop in tags) { |
| 63 | + if (tags.hasOwnProperty(prop)) { |
| 64 | + table.append( |
| 65 | + row.clone() |
| 66 | + .append(cell.clone().text(prop)) |
| 67 | + .append(cell.clone().text(tags[prop])) |
| 68 | + ) |
| 69 | + } |
| 70 | + } |
| 71 | + exifNode.show() |
| 72 | + } |
| 73 | + function dropChangeHandler (e) { |
| 74 | + e.preventDefault() |
| 75 | + e = e.originalEvent |
| 76 | + var target = e.dataTransfer || e.target |
| 77 | + var file = target && target.files && target.files[0] |
| 78 | + var options = { |
| 79 | + maxWidth: result.width(), |
| 80 | + canvas: true |
| 81 | + } |
| 82 | + if (!file) { |
| 83 | + return |
| 84 | + } |
| 85 | + exifNode.hide() |
| 86 | + thumbNode.hide() |
| 87 | + loadImage.parseMetaData(file, function (data) { |
| 88 | + if (data.exif) { |
| 89 | + options.orientation = data.exif.get('Orientation') |
| 90 | + displayExifData(data.exif) |
| 91 | + } |
| 92 | + displayImage(file, options) |
| 93 | + }) |
| 94 | + } |
| 95 | + // Hide URL/FileReader API requirement message in capable browsers: |
| 96 | + if (window.createObjectURL || window.URL || window.webkitURL || window.FileReader) { |
| 97 | + result.children().hide() |
| 98 | + } |
| 99 | + $(document) |
| 100 | + .on('dragover', function (e) { |
| 101 | + e.preventDefault() |
| 102 | + e = e.originalEvent |
| 103 | + e.dataTransfer.dropEffect = 'copy' |
| 104 | + }) |
| 105 | + .on('drop', dropChangeHandler) |
| 106 | + $('#file-input').on('change', dropChangeHandler) |
| 107 | + $('#edit').on('click', function (event) { |
| 108 | + event.preventDefault() |
| 109 | + var imgNode = result.find('img, canvas') |
| 110 | + var img = imgNode[0] |
| 111 | + imgNode.Jcrop({ |
| 112 | + setSelect: [40, 40, img.width - 40, img.height - 40], |
| 113 | + onSelect: function (coords) { |
| 114 | + coordinates = coords |
| 115 | + }, |
| 116 | + onRelease: function () { |
| 117 | + coordinates = null |
| 118 | + } |
| 119 | + }).parent().on('click', function (event) { |
| 120 | + event.preventDefault() |
| 121 | + }) |
| 122 | + }) |
| 123 | + $('#crop').on('click', function (event) { |
| 124 | + event.preventDefault() |
| 125 | + var img = result.find('img, canvas')[0] |
| 126 | + if (img && coordinates) { |
| 127 | + replaceResults(loadImage.scale(img, { |
| 128 | + left: coordinates.x, |
| 129 | + top: coordinates.y, |
| 130 | + sourceWidth: coordinates.w, |
| 131 | + sourceHeight: coordinates.h, |
| 132 | + minWidth: result.width() |
| 133 | + })) |
| 134 | + coordinates = null |
| 135 | + } |
| 136 | + }) |
| 137 | +}) |
0 commit comments