gallery3d: Add new widget
[platform/framework/web/web-ui-fw.git] / src / js / widgets / components / imageloader.js
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
5 \r
6 define( [ ], function ( ) {\r
7 //>>excludeEnd("jqmBuildExclude");\r
8 \r
9 /* ***************************************************************************\r
10  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.\r
11  *\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
18  *\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
21  *\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
30  *\r
31  * Authors: Hyunsook Park <hyunsook.park@samsung.com>\r
32  *                      Wonseop Kim <wonseop.kim@samsung.com>\r
33 */\r
34 \r
35 ( function ( $, window, document, undefined ) {\r
36         var _canvas = document.createElement( 'canvas' ),\r
37                 _context = _canvas.getContext( '2d' );\r
38 \r
39         function fileSystemErrorMessage( e ) {\r
40                 var FileError = window.FileError,\r
41                         msg = '';\r
42                 switch ( e.code ) {\r
43                 case FileError.QUOTA_EXCEEDED_ERR:\r
44                         msg = 'QUOTA_EXCEEDED_ERR';\r
45                         break;\r
46                 case FileError.NOT_FOUND_ERR:\r
47                         msg = 'NOT_FOUND_ERR';\r
48                         break;\r
49                 case FileError.SECURITY_ERR:\r
50                         msg = 'SECURITY_ERR';\r
51                         break;\r
52                 case FileError.INVALID_MODIFICATION_ERR:\r
53                         msg = 'INVALID_MODIFICATION_ERR';\r
54                         break;\r
55                 case FileError.INVALID_STATE_ERR:\r
56                         msg = 'INVALID_STATE_ERR';\r
57                         break;\r
58                 default:\r
59                         msg = 'Unknown Error';\r
60                         break;\r
61                 }\r
62                 return msg;\r
63         }\r
64 \r
65         function getInternalURLFromURL( url ) {\r
66                 var internalURL = url.replace( /\//gi, "_" );\r
67                 return internalURL;\r
68         }\r
69 \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
75 \r
76                 if ( fit ) {\r
77                         w = thumbwidth;\r
78                         h = thumbheight;\r
79                 } else {\r
80                         if ( maxratio > 1 ) {\r
81                                 w = imagewidth / maxratio;\r
82                                 h = imageheight / maxratio;\r
83                         } else {\r
84                                 w = imagewidth;\r
85                                 h = imageheight;\r
86                         }\r
87                         x = ( thumbwidth - w ) / 2;\r
88                         y = ( thumbheight - h ) / 2;\r
89                 }\r
90 \r
91                 return { w: w, h: h, x: x, y: y };\r
92         }\r
93 \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
103                 return url;\r
104         }\r
105 \r
106         $.imageloader = {\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
112                                 if ( _callback ) {\r
113                                         _callback( ( msg === "NOT_FOUND_ERR" ) ? msg : null );\r
114                                 }\r
115                         }\r
116 \r
117                         internalURL = getInternalURLFromURL( url );\r
118                         try {\r
119                                 canvasDataURI = localStorage.getItem( internalURL );\r
120                                 if ( _callback ) {\r
121                                         _callback( ( canvasDataURI === null ) ? "NOT_FOUND_ERR" : canvasDataURI );\r
122                                 }\r
123                         } catch ( e ) {\r
124                                 if ( _callback ) {\r
125                                         _callback( ( e.type === "non_object_property_load" ) ? "NOT_FOUND_ERR" : null );\r
126                                 }\r
127                         }\r
128                 },\r
129 \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
134                                 if ( _callback ) {\r
135                                         _callback( ( msg === "NOT_FOUND_ERR" ) ? msg : null );\r
136                                 }\r
137                         }\r
138 \r
139                         thumbWidth = thumbWidth || 128;\r
140                         thumbHeight = thumbHeight || 128;\r
141                         fit = fit || true;\r
142                         image = new Image();\r
143                         image.onload = function () {\r
144                                 internalURL = getInternalURLFromURL( url );\r
145                                 canvasDataURI = getThumbnail( this, thumbWidth, thumbHeight, fit );\r
146                                 try {\r
147                                         localStorage.setItem( internalURL, canvasDataURI );\r
148                                         if ( _callback ) {\r
149                                                 _callback( canvasDataURI );\r
150                                         }\r
151                                 } catch ( e ) {\r
152                                         if ( _callback ) {\r
153                                                 _callback( ( e.type === "non_object_property_load" ) ? "NOT_FOUND_ERR" : null );\r
154                                         }\r
155                                 }\r
156                         };\r
157                         image.src = url;\r
158                 },\r
159 \r
160                 removeThumbnail: function ( url ) {\r
161                         var internalURL;\r
162                         function errorHandler( e ) {\r
163                                 fileSystemErrorMessage( e );\r
164                         }\r
165 \r
166                         internalURL = getInternalURLFromURL( url );\r
167                         try {\r
168                                 localStorage.removeItem( internalURL );\r
169                         } catch ( e ) {\r
170                                 throw e;\r
171                         }\r
172                 }\r
173         };\r
174 \r
175 } ( jQuery, window, document ) );\r
176 \r
177 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);\r
178 } );\r
179 //>>excludeEnd("jqmBuildExclude");\r