Skip to content

Commit

Permalink
merging 0.7-dev branch into master
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisben committed Mar 27, 2014
2 parents 44d5dd1 + 3329cb2 commit f7ac5f0
Show file tree
Hide file tree
Showing 12 changed files with 1,127 additions and 643 deletions.
25 changes: 23 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
Release Notes
-------------
# Release Notes #

## 0.7.0 ##

### NEW ###
* New method: ImgCache.removeFile (#33)
* New method: ImgCache.getCurrentSize + added ability to clear cache on init when used space is higher than a given value (#28)
* New method: ImgCache.useBackgroundOnlineFile to revert a call to useCachedBackground (#17)
* jQuery/Zepto is not a required dependency anymore, if not available it will be using only the DOM API (#29)
* Added API coverage check in unit test

### IMPROVED ###
* Unit tests are moved to index.html
* README has been improved and split into multiple files: CORDOVA.md contains all documentation related to Cordova/Phonegap
* Reorganised code to be clearer + added checks throughout the code where missing
* Updated unit tests for 100% API coverage and multiple options configurations
* config.xml has been updated for newest Cordova releases requirements

### FIXED ###
* Fixed issues with Cordova 3.3/3.4 (#35, #40 -- thanks to Lukáš Marek)

## Older releases ##

* 0.6.2 Added extra checks to make sure ImgCache is properly initialised first (#15) + added note in README about Phonegap plugins (#25)
* 0.6.1 Added useCachedFileWithSource (#21) + added Bower package definition
* 0.6 Updated deprecated Chrome storage API + Refactored code + improved automated test suites + fixes
Expand Down
78 changes: 78 additions & 0 deletions CORDOVA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Using imgcache.js with Cordova/Phonegap

## javascript

You will need to include the cordova.js script in the html files where you want to use imgcache.js - see the `examples` folder.

imgcache.js should be initialised and used after the [deviceready](http://cordova.apache.org/docs/en/edge/cordova_events_events.md.html#deviceready) event has been fired.
For instance if the code calling imgcache.js is within a function called `yourCallbackFunction` :

```
document.addEventListener("deviceready", yourCallbackFunction, false);
```

## Configuration

This section lists the configuration needed to make imgcache.js work with Cordova/Phonegap. This configuration takes place in the config.xml file of your Cordova project.

See [config.xml](config.xml) at the root of this project as an example.

### Features

#### Cordova 3.x

imgcache.js requires the following Cordova features/plugins:
* [File](http://docs.phonegap.com/en/edge/cordova_file_file.md.html#File_accessing_the_feature)
* [Device](http://docs.phonegap.com/en/edge/cordova_device_device.md.html#Device_accessing_the_feature)
* [FileTransfer](https://github.com/apache/cordova-plugin-file-transfer/blob/dev/doc/index.md)

For each of these plugins you will be required to define the corresponding package for the OS you target. Here is a default configuration for both iOS and Android :

```xml
<feature name="Device">
<param name="ios-package" value="CDVDevice" />
<param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<feature name="File">
<param name="ios-package" value="CDVFile" />
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="ios-package" value="CDVFileTransfer" />
<param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>
```

#### Cordova 2.x

Features need to be added this way:

```xml
<feature name="http://api.phonegap.com/1.0/file"/>
<feature name="http://api.phonegap.com/1.0/device"/>
<feature name="http://api.phonegap.com/1.0/network"/>
```

### Phonegap Build

For Phonegap Build you also need to list all core plugins used (= each feature):

```
<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.file-transfer" />
```

### Access origin

In order to cache remote images via http it is important to allow access to those domains. This can be setup via a whitelisting `<access>` element in your config.xml file.

If you only want to do tests you can allow access to all domains to avoid this kind of issues:

```xml
<access origin="*" />
```

In order to avoid possible security issues, always limit the allowed domains list whenever possible.

For more information about this access origin configuration see [Whitelist Guide](http://docs.phonegap.com/en/edge/guide_appdev_whitelist_index.md.html#Whitelist%20Guide).
7 changes: 7 additions & 0 deletions KNOWN_ISSUES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Known issues

## Cordova

* getCurrentSize() always returns 0 using Cordova Plugin File v1.0.0.
The problem is that the Metadata interface of a File entry does not have a *size* property thus it does not match the html5 specifications.
Looking at the code for this plugin, this should have been [fixed](https://github.com/apache/cordova-plugin-file/commit/9ac8e477c0fda6aed3878a4cf165257f00e1bf83) in v1.0.1 but unit test still fail on iOS.
5 changes: 2 additions & 3 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Apache License
==============
# Apache License

Copyright 2012-2013 (c) Christophe BENOIT - [Wobis](http://www.wobis.fr)
Copyright 2012-2014 (c) Christophe BENOIT - [Wobis](http://www.wobis.fr)

*Version 2.0, January 2004*

Expand Down
56 changes: 25 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
imgcache.js
===========
# imgcache.js

***Note: this version has known issues with newer Cordova libraries, in the meantime please use the 0.7-dev branch that fixes those issues and should be merged to master soon.***

The purpose of this JS library is to provide a nice interface for locally storing images for offline apps using
PhoneGap/Cordova or [browsers supporting the new html5 File API](http://caniuse.com/filesystem)
(e.g. Chrome).
The purpose of this JS library is to provide a nice interface for locally storing images for offline apps using PhoneGap/Cordova or [browsers supporting the new html5 File API](http://caniuse.com/filesystem) (e.g. Chrome).

This library is especially useful for mobile web applications using Phonegap/Cordova where the normal browser cache cannot be relied upon and where offline navigation is quite common.

Expand All @@ -18,36 +13,29 @@ This is the best solution I have found so far to provide easy caching of images
This library works with Phonegap/Cordova (v >= 1.7) so the supported platforms should be:
* Android [TESTED]
* iOS [TESTED]
* BlackBerry WebWorks (OS 5.0 and higher)
* Windows Phone 7 ( Mango )
* Windows 8

All methods are asynchronous : use callbacks if required.
Most methods are *asynchronous* : use callbacks if required.

Using imgcache.js
=================

Requirements
------------
* jQuery (any version from 1.6 should do) or Zepto
Optional Dependencies
---------------------
* jQuery (any version from 1.6 should do) or Zepto *optional*
* Phonegap/Cordova *optional* (v >= v1.7)
* [imagesloaded] (http://desandro.github.com/imagesloaded/) *optional*

Installation
------------
To use `imgcache.js`, you need to copy `js/imgcache.js` and add it to your
application's Javascript. You can then load it like so:
To use this library, you need to copy `js/imgcache.js` into your project and import that script within your html file:

```html
<script src="imgcache.js" type="application/javascript"></script>
<script src="js/imgcache.js"></script>
```

Using with PhoneGap/Cordova:
* Requires the [File API](http://docs.phonegap.com/en/edge/cordova_file_file.md.html#File_accessing_the_feature) and [Device](http://docs.phonegap.com/en/edge/cordova_device_device.md.html#Device_accessing_the_feature) plugins in `config.xml`
* Remember to allow access to remote files by adding your domain in config.xml - or all domains using a wildcard: `<access origin="*" />`

Using with PhoneGap/Cordova (v >= 3.0):
* Check [Issue #15](https://github.com/chrisben/imgcache.js/issues/15) for more information

Using with PhoneGap/Cordova: see [CORDOVA.md](CORDOVA.md).

Using with Chrome or other browsers that support the [html5 filesystem API]:
* Beware of cross domain ajax issue! retrieve image from the same domain or set CORS solutions with the server...
* If the page is opened locally (file:// ..), Chrome needs to be loaded with the following flags: `--allow-file-access-from-files --allow-file-access` otherwise the local filesystem will not be accessible (security error)
Expand Down Expand Up @@ -77,7 +65,7 @@ ImgCache.init(function(){
});
````

If the cache successfully initializes, `ImgCache.ready` will be set to `true`.
If the cache successfully initializes, `ImgCache.ready` will be set to `true`. You can also watch for the triggered `ImgCacheReady` event.

If you're using imgcache.js with PhoneGap/Cordova, `ImgCache.init()` must be called in `onDeviceReady`, not before!
Expand Down Expand Up @@ -156,30 +144,36 @@ High level API
* ImgCache.clearCache() *clears the local cache folder*
* ImgCache.cacheBackground() *caches the background image of an element*
* ImgCache.useCachedBackground() *replaces the background image source of the given element with the cached version*
* ImgCache.useBackgroundOnlineFile() *replaces back a background image with the original (online) version*
* ImgCache.removeFile() *removes a given file from the cache*
* ImgCache.getCurrentSize() *returns the current size of the ImgCache cache in bytes -- this is not an asynchronous method *

Options
-------
See ImgCache.options at the top of the source file for the list of options.
Options can be overriden from your own script, no need to modify the library!

Unit tests
----------
Open index.html and click 'Start unit tests' to launch unit tests.

Code samples
------------
See html files in the `examples/` folder.

Release Notes
-------------
See `CHANGELOG.md` for recent updates.
See [CHANGELOG](CHANGELOG.md) for the complete release notes.

Known issues
------------
See [KNOWN_ISSUES](KNOWN_ISSUES.md) for a list of known issues.

License
-------
Copyright 2012-2013 (c) Christophe BENOIT - [Wobis](http://www.wobis.fr)
Copyright 2012-2014 (c) Christophe BENOIT - [Wobis](http://www.wobis.fr)

Apache License - see LICENSE.md

Code from http://code.google.com/p/tiny-sha1/ is being used which is under the MIT License.
The copyright for this part belongs to the creator of this work.

Todo
----
* Find a solution for cache invalidation in case an image changes
* When Chrome finally supports canvas.toBlob(), possibly replace download method with new one that draws an Image into a canvas and then retrieves its content using the toBlob() method -- or use [canvas-toBlob.js] (https://github.com/eligrey/canvas-toBlob.js)
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# TODO

* Find a solution for cache invalidation when online in case an image has changed since last cached version
* When Chrome finally supports canvas.toBlob(), possibly replace download method with new one that draws an Image into a canvas and then retrieves its content using the toBlob() method -- or use [canvas-toBlob.js] (https://github.com/eligrey/canvas-toBlob.js)
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imgcache.js",
"version": "0.6.2",
"version": "0.7.0",
"homepage": "https://github.com/chrisben/imgcache.js",
"authors": [
{
Expand Down
29 changes: 24 additions & 5 deletions config.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "imgcache.js"
versionCode = "1"
version = "1.0.0">
<widget xmlns="http://www.w3.org/ns/widgets"
xmlns:gap="http://phonegap.com/ns/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="imgcache.js"
versionCode="1"
version="1.0.0">

<name>imgcache.js</name>

Expand All @@ -18,9 +19,27 @@
<feature name="http://api.phonegap.com/1.0/device"/>
<feature name="http://api.phonegap.com/1.0/network"/>

<gap:plugin name="org.apache.cordova.file" />
<gap:plugin name="org.apache.cordova.device" />
<gap:plugin name="org.apache.cordova.file-transfer" />

<feature name="Device">
<param name="ios-package" value="CDVDevice" />
<param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<feature name="File">
<param name="ios-package" value="CDVFile" />
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
</feature>
<feature name="FileTransfer">
<param name="ios-package" value="CDVFileTransfer" />
<param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>

<preference name="orientation" value="default" />
<preference name="target-device" value="universal" />
<preference name="fullscreen" value="true" />
<preference name="phonegap-version" value="3.3.0" />

<access origin="*" />
</widget>
71 changes: 71 additions & 0 deletions examples/nojquery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Examples</title>
<script src="../js/imgcache.js"></script>
<!-- only for cordova (replace by phonegap.js for phonegap) - ignore on desktop browser -->
<script src="../cordova.js"></script>
<script>
var startTest = function() {
// see console output for debug info
ImgCache.options.debug = true;

ImgCache.init(function() {
console.log('ImgCache initialised');
});
};

if (typeof(cordova) !== 'undefined') {
// cordova test
document.addEventListener('deviceready', startTest, false);
} else {
// normal browser test
window.onload = startTest;
}
</script>
<style>
/* just to make the page look a little nicer.. no use for the example in itself.. */
.note {
background-color: #bbb;
margin: 1em;
padding: 1em;
display: inline-block;
float: right;
clear: right;
}
code {
display: block;
margin: 0.5em;
}
ul#actions li {
margin: 0.8em;
}
body { background-color: #eef; }
h1 { text-align: center; }
</style>
</head>
<body>

<h1>imgcache.js - examples</h1>

<div class="note">
<p>Don't forget to accept your browser request to store data on the local computer!</p>
</div>

<div class="note">
<p>If seen in Chrome run chrome with the following flags: <code>--allow-file-access-from-files --allow-file-access</code> in order to <a href="http://stackoverflow.com/questions/6427870/html5-file-api-security-error-while-reading-a-file">avoid a security error</a>.</p>
</div>

<p>1. Open page locally (file:// in Chrome) or in Phonegap/Cordova</p>
<p>2. Work offline</p>
<p>3. Reload the page (or re-start the web app)</p>
<p>4. You should see the images reloaded from the cache</p>

<div>
<h2>This page runs ImgCache without jQuery</h2>
<p>See console to check for any error.</p>
</div>

</body>
</html>
Loading

0 comments on commit f7ac5f0

Please sign in to comment.