Created
September 11, 2012 23:44
-
-
Save ferclaverino/3703065 to your computer and use it in GitHub Desktop.
bookmarkInLocalStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
"use strict"; | |
var localstorage; | |
var bookmarks = []; | |
function saveBookmarksToStorage() { | |
localstorage["bookmarks"] = JSON.stringify(bookmarks); | |
} | |
function init() { | |
localstorage = window.localStorage; | |
if (localstorage["bookmarks"]) { | |
try { | |
bookmarks = JSON.parse(localstorage["bookmarks"]); | |
} | |
catch (err) { | |
saveBookmarksToStorage(); | |
} | |
} | |
} | |
function saveImageInLocalFolder(imageUrl) { | |
if (imageUrl) { | |
WinJS.xhr({ | |
responseType: "blob", | |
url: imageUrl | |
}).then(function (result) { | |
writeBlobToFile(getImageName(imageUrl), result.response); | |
}); | |
} | |
} | |
function writeBlobToFile(filename, blob) { | |
var applicationData = Windows.Storage.ApplicationData.current; | |
var localFolder = applicationData.localFolder; | |
localFolder.createFolderAsync("bookmark", Windows.Storage.CreationCollisionOption.openIfExists).then(function (bookmarkFolder) { | |
// Open the picker to create a file to save the blob | |
return bookmarkFolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.replaceExisting); | |
}).then(function (file) { | |
// Open the returned file in order to copy the data | |
file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (output) { | |
// Get the IInputStream stream from the blob object | |
var input = blob.msDetachStream(); | |
// Copy the stream from the blob to the File stream | |
Windows.Storage.Streams.RandomAccessStream.copyAsync(input, output).then(function () { | |
output.flushAsync().done(function () { | |
input.close(); | |
output.close(); | |
}); | |
}); | |
}); | |
}); | |
} | |
function deleteImageInLocalFolder(imageUrl) { | |
deleteFile(getImageName(imageUrl)); | |
} | |
function deleteFile(filename) { | |
var applicationData = Windows.Storage.ApplicationData.current; | |
var localFolder = applicationData.localFolder; | |
localFolder.createFolderAsync("bookmark", Windows.Storage.CreationCollisionOption.openIfExists).then(function (bookmarkFolder) { | |
return bookmarkFolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.openIfExists); | |
}).then(function (file) { | |
file.deleteAsync(); | |
}); | |
} | |
function getImageName(imageUrl) { | |
var fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1, imageUrl.length); | |
return fileName; | |
} | |
function getImageLocalUrl(imageUrl) { | |
return "ms-appdata:///local/bookmark/" + getImageName(imageUrl); | |
} | |
var instanceMembers = { | |
addNews: function (news) { | |
saveImageInLocalFolder(news.image); | |
news.image = getImageLocalUrl(news.image); | |
bookmarks.push(news); | |
saveBookmarksToStorage(); | |
}, | |
hasNews: function (idArticulo) { | |
var result = bookmarks.filter(function (item) { | |
return (item.IdArticulo == idArticulo); | |
}); | |
return (result.length > 0); | |
}, | |
removeNews: function (idArticulo) { | |
for (var i = 0; i < bookmarks.length; i++) { | |
if (bookmarks[i].IdArticulo == idArticulo) { | |
deleteImageInLocalFolder(bookmarks[i].image) | |
bookmarks.splice(i, 1); | |
saveBookmarksToStorage(); | |
return; | |
} | |
} | |
}, | |
getAllNews: function () { | |
return bookmarks; | |
}, | |
}; | |
var staticMembers = {}; | |
var BookmarkInLocalStorage = WinJS.Class.define(init, instanceMembers, staticMembers); | |
// add to namespace | |
WinJS.Namespace.define("Repositories", | |
{ | |
BookmarkInLocalStorage: BookmarkInLocalStorage | |
} | |
); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment