Skip to content

Commit

Permalink
Merge pull request #43 from CulturalMe/improved_lc
Browse files Browse the repository at this point in the history
Prevented Flickering
  • Loading branch information
gsuess committed Jan 8, 2015
2 parents 688face + dc4d3d4 commit 4eee2ca
Showing 1 changed file with 70 additions and 27 deletions.
97 changes: 70 additions & 27 deletions lib/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Slingshot.Upload = function (directive, metaData) {
loaded = new ReactiveVar(),
total = new ReactiveVar(),
status = new ReactiveVar("idle"),
dataUri = new ReactiveVar(),
preloaded= new ReactiveVar();
dataUri,
preloaded;

function buildFormData() {
var formData = new window.FormData();
Expand Down Expand Up @@ -212,42 +212,85 @@ Slingshot.Upload = function (directive, metaData) {
*/

url: function (preload) {
var status = self.status();

if (preload && !self.isImage())
throw new Error("Cannot pre-load anything other than images");

if (self.instructions && status === "done") {
var download = self.instructions.download;
if (!dataUri) {
var localUrl = new ReactiveVar(),
URL = (window.URL || window.webkitURL);

dataUri = new ReactiveVar();

Tracker.nonreactive(function () {

/*
It is important that we generate the local url not more than once
throughout the entire lifecycle of `self` to prevent flickering.
*/

var previewRequirement = new Tracker.Dependency();

Tracker.autorun(function (computation) {
if (self.file) {
if (URL) {
localUrl.set(URL.createObjectURL(self.file));
computation.stop();
}
else if (Tracker.active && window.FileReader) {
readDataUrl(self.file, function (result) {
localUrl.set(result);
computation.stop();
});
}
}
else {
previewRequirement.depend();
}
});

if (preload && preloaded.get() !== download) {
preloadImage(download, function () {
preloaded.set(download);
Tracker.autorun(function (computation) {
var status = self.status();

if (self.instructions && status === "done") {
computation.stop();
dataUri.set(self.instructions.download);
}
else if (status === "failed" || status === "aborted") {
computation.stop();
}
else if (self.file && !dataUri.curValue) {
previewRequirement.changed();
dataUri.set(localUrl.get());
}
});
}
else {
dataUri.set();
return download;
}
});
}

if (status !== "failed" && status !== "aborted" && self.file) {
var URL = (window.URL || window.webkitURL);
if (preload) {

if (URL)
return URL.createObjectURL(self.file);
if (self.file && !self.isImage())
throw new Error("Cannot pre-load anything other than images");

if (Tracker.active && window.FileReader) {
var url = dataUri.get();
if (!preloaded) {
Tracker.nonreactive(function () {
preloaded = new ReactiveVar();

if (url)
return url;
Tracker.autorun(function (computation) {
var url = dataUri.get();

readDataUrl(self.file, function (result) {
dataUri.set(result);
if (self.instructions) {
preloadImage(url, function () {
computation.stop();
preloaded.set(url);
});
}
else
preloaded.set(url);
});
});
}

return preloaded.get();
}
else
return dataUri.get();
},

/** Gets an upload parameter for the directive.
Expand Down

0 comments on commit 4eee2ca

Please sign in to comment.