2 /* ***************************************************************************
\r
3 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a
\r
6 * copy of this software and associated documentation files (the "Software"),
\r
7 * to deal in the Software without restriction, including without limitation
\r
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
9 * and/or sell copies of the Software, and to permit persons to whom the
\r
10 * Software is furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
\r
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
21 * DEALINGS IN THE SOFTWARE.
\r
22 * ***************************************************************************
\r
24 * Authors: Hyunsook Park <hyunsook.park@samsung.com>
\r
25 * Wonseop Kim <wonseop.kim@samsung.com>
\r
28 ( function ( $, window, document, undefined ) {
\r
29 var _canvas, _context;
\r
31 function initCanvas() {
\r
35 _canvas = document.createElement( 'canvas' );
\r
36 _context = _canvas.getContext( '2d' );
\r
39 function fileSystemErrorMessage( e ) {
\r
40 var FileError = window.FileError,
\r
43 case FileError.QUOTA_EXCEEDED_ERR:
\r
44 msg = 'QUOTA_EXCEEDED_ERR';
\r
46 case FileError.NOT_FOUND_ERR:
\r
47 msg = 'NOT_FOUND_ERR';
\r
49 case FileError.SECURITY_ERR:
\r
50 msg = 'SECURITY_ERR';
\r
52 case FileError.INVALID_MODIFICATION_ERR:
\r
53 msg = 'INVALID_MODIFICATION_ERR';
\r
55 case FileError.INVALID_STATE_ERR:
\r
56 msg = 'INVALID_STATE_ERR';
\r
59 msg = 'Unknown Error';
\r
65 function getInternalURLFromURL( url ) {
\r
66 var internalURL = url.replace( /\//gi, "_" );
\r
70 function resize( imagewidth, imageheight, thumbwidth, thumbheight, fit ) {
\r
71 var w = 0, h = 0, x = 0, y = 0,
\r
72 widthratio = imagewidth / thumbwidth,
\r
73 heightratio = imageheight / thumbheight,
\r
74 maxratio = Math.max( widthratio, heightratio );
\r
80 if ( maxratio > 1 ) {
\r
81 w = imagewidth / maxratio;
\r
82 h = imageheight / maxratio;
\r
87 x = ( thumbwidth - w ) / 2;
\r
88 y = ( thumbheight - h ) / 2;
\r
91 return { w: w, h: h, x: x, y: y };
\r
94 function getThumbnail( img, thumbwidth, thumbheight, fit ) {
\r
95 var dimensions, url;
\r
97 _canvas.width = thumbwidth;
\r
98 _canvas.height = thumbheight;
\r
99 dimensions = resize( img.width, img.height, thumbwidth, thumbheight, fit );
\r
100 _context.fillStyle = "#000000";
\r
101 _context.fillRect ( 0, 0, thumbwidth, thumbheight );
\r
102 _context.drawImage( img, dimensions.x, dimensions.y, dimensions.w, dimensions.h );
\r
103 url = _canvas.toDataURL();
\r
108 _grantedBytes: 1024 * 1024,
\r
109 getThumbnail: function ( url, _callback ) {
\r
110 var internalURL, canvasDataURI;
\r
111 function errorHandler( e ) {
\r
112 var msg = fileSystemErrorMessage( e );
\r
114 _callback( ( msg === "NOT_FOUND_ERR" ) ? msg : null );
\r
118 internalURL = getInternalURLFromURL( url );
\r
120 canvasDataURI = localStorage.getItem( internalURL );
\r
122 _callback( ( canvasDataURI === null ) ? "NOT_FOUND_ERR" : canvasDataURI );
\r
126 _callback( ( e.type === "non_object_property_load" ) ? "NOT_FOUND_ERR" : null );
\r
131 setThumbnail: function ( url, _callback, thumbWidth, thumbHeight, fit ) {
\r
132 var image, internalURL, canvasDataURI;
\r
133 function errorHandler( e ) {
\r
134 var msg = fileSystemErrorMessage( e );
\r
136 _callback( ( msg === "NOT_FOUND_ERR" ) ? msg : null );
\r
140 thumbWidth = thumbWidth || 128;
\r
141 thumbHeight = thumbHeight || 128;
\r
143 image = new Image();
\r
144 image.onload = function () {
\r
145 internalURL = getInternalURLFromURL( url );
\r
146 canvasDataURI = getThumbnail( this, thumbWidth, thumbHeight, fit );
\r
148 localStorage.setItem( internalURL, canvasDataURI );
\r
150 _callback( canvasDataURI );
\r
154 _callback( ( e.type === "non_object_property_load" ) ? "NOT_FOUND_ERR" : null );
\r
161 removeThumbnail: function ( url ) {
\r
163 function errorHandler( e ) {
\r
164 fileSystemErrorMessage( e );
\r
167 internalURL = getInternalURLFromURL( url );
\r
169 localStorage.removeItem( internalURL );
\r
176 } ( jQuery, window, document ) );
\r