1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
\r
2 //>>description: Tizen image loader component for gallery3d
\r
3 //>>label: Image loader
\r
4 //>>group: Tizen:Widgets:Components
\r
6 define( [ ], function ( ) {
\r
7 //>>excludeEnd("jqmBuildExclude");
\r
9 /* ***************************************************************************
\r
10 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
\r
12 * Permission is hereby granted, free of charge, to any person obtaining a
\r
13 * copy of this software and associated documentation files (the "Software"),
\r
14 * to deal in the Software without restriction, including without limitation
\r
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
16 * and/or sell copies of the Software, and to permit persons to whom the
\r
17 * Software is furnished to do so, subject to the following conditions:
\r
19 * The above copyright notice and this permission notice shall be included in
\r
20 * all copies or substantial portions of the Software.
\r
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
\r
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
28 * DEALINGS IN THE SOFTWARE.
\r
29 * ***************************************************************************
\r
31 * Authors: Hyunsook Park <hyunsook.park@samsung.com>
\r
32 * Wonseop Kim <wonseop.kim@samsung.com>
\r
35 ( function ( $, window, document, undefined ) {
\r
36 var _canvas = document.createElement( 'canvas' ),
\r
37 _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
96 _canvas.width = thumbwidth;
\r
97 _canvas.height = thumbheight;
\r
98 dimensions = resize( img.width, img.height, thumbwidth, thumbheight, fit );
\r
99 _context.fillStyle = "#000000";
\r
100 _context.fillRect ( 0, 0, thumbwidth, thumbheight );
\r
101 _context.drawImage( img, dimensions.x, dimensions.y, dimensions.w, dimensions.h );
\r
102 url = _canvas.toDataURL();
\r
107 _grantedBytes: 1024 * 1024,
\r
108 getThumbnail: function ( url, _callback ) {
\r
109 var internalURL, canvasDataURI;
\r
110 function errorHandler( e ) {
\r
111 var msg = fileSystemErrorMessage( e );
\r
113 _callback( ( msg === "NOT_FOUND_ERR" ) ? msg : null );
\r
117 internalURL = getInternalURLFromURL( url );
\r
119 canvasDataURI = localStorage.getItem( internalURL );
\r
121 _callback( ( canvasDataURI === null ) ? "NOT_FOUND_ERR" : canvasDataURI );
\r
125 _callback( ( e.type === "non_object_property_load" ) ? "NOT_FOUND_ERR" : null );
\r
130 setThumbnail: function ( url, _callback, thumbWidth, thumbHeight, fit ) {
\r
131 var image, internalURL, canvasDataURI;
\r
132 function errorHandler( e ) {
\r
133 var msg = fileSystemErrorMessage( e );
\r
135 _callback( ( msg === "NOT_FOUND_ERR" ) ? msg : null );
\r
139 thumbWidth = thumbWidth || 128;
\r
140 thumbHeight = thumbHeight || 128;
\r
142 image = new Image();
\r
143 image.onload = function () {
\r
144 internalURL = getInternalURLFromURL( url );
\r
145 canvasDataURI = getThumbnail( this, thumbWidth, thumbHeight, fit );
\r
147 localStorage.setItem( internalURL, canvasDataURI );
\r
149 _callback( canvasDataURI );
\r
153 _callback( ( e.type === "non_object_property_load" ) ? "NOT_FOUND_ERR" : null );
\r
160 removeThumbnail: function ( url ) {
\r
162 function errorHandler( e ) {
\r
163 fileSystemErrorMessage( e );
\r
166 internalURL = getInternalURLFromURL( url );
\r
168 localStorage.removeItem( internalURL );
\r
175 } ( jQuery, window, document ) );
\r
177 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);
\r
179 //>>excludeEnd("jqmBuildExclude");
\r