Updated application sources
[apps/web/sample/FileManager.git] / project / js / app.helpers.js
1 /*
2  *      Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license/
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 /*jslint devel: true*/
18 /*global $, jQuery */
19
20 /**
21  * @class Helpers
22  */
23 function Helpers() {
24     'use strict';
25 }
26
27 (function () { // strict mode wrapper
28     'use strict';
29     Helpers.prototype = {
30
31         extensions: {
32             '.bmp':       'img.png',
33             '.gif':       'img.png',
34             '.jpeg':      'img.png',
35             '.jpg':       'img.png',
36             '.png':       'img.png',
37             '.tiff':      'img.png',
38             '.3ga':       'music.png',
39             '.aac':       'music.png',
40             '.ac3':       'music.png',
41             '.amr':       'music.png',
42             '.awb':       'music.png',
43             '.m4a':       'music.png',
44             '.m4p':       'music.png',
45             '.m4r':       'music.png',
46             '.mp3':       'music.png',
47             '.ogg':       'music.png',
48             '.wav':       'music.png',
49             '.wma':       'music.png',
50             '.3gp':       'video.png',
51             '.avc':       'video.png',
52             '.avi':       'video.png',
53             '.m4v':       'video.png',
54             '.mkv':       'video.png',
55             '.mov':       'video.png',
56             '.mp4':       'video.png',
57             '.mpeg':      'video.png',
58             '.mpg':       'video.png',
59             '.ogv':       'video.png',
60             '.vc1':       'video.png',
61             '.wmv':       'video.png',
62             '.doc':       'text.png',
63             '.docx':      'text.png',
64             '.odt':       'text.png',
65             '.ods':       'text.png',
66             '.txt':       'text.png',
67             '.xls':       'text.png',
68             '.xlsx':      'text.png',
69             '.vcard':     'text.png',
70             '.vcf':       'text.png',
71             '.icalendar': 'text.png',
72             '.ical':      'text.png',
73             '.ics':       'text.png',
74             '.ifb':       'text.png',
75             '.pdf':       'pdf.png',
76             '.odp':       'ppt.png',
77             '.ppt':       'ppt.png',
78             '.wgt':       'etc.png'
79         },
80
81         /**
82          * Capitalise the first letter
83          *
84          * @param {string} text
85          * @returns {string}
86          */
87         UCFirst: function Helpers_UCFirst(text) {
88             return text.charAt(0).toUpperCase() + text.slice(1);
89         },
90
91         /**
92          * @param {string} fileName
93          * @returns {string} file name without extension
94          */
95         getFileName: function Helpers_getFileName(fileName) {
96             var fileNameLen = fileName.lastIndexOf('.');
97             if (fileNameLen !== -1) {
98                 fileName = fileName.slice(0, fileNameLen);
99             }
100             return fileName;
101         },
102
103         /**
104          * @param {string} fileName
105          * @returns {string} extension for specified file name
106          */
107         getFileExtension: function Helpers_getFileExtension(fileName) {
108             var splittedFileName = fileName.split('.'),
109                 ext = '';
110
111             if (splittedFileName.length > 1) {
112                 ext = '.' + splittedFileName.pop();
113             }
114             return ext;
115         },
116
117         /**
118          * Return icon filename for the given extension.
119          * For example, for '.mp3' returns 'music.png'
120          *
121          * @param {string} ext
122          * @return {string}
123          */
124         resolveFileIcon: function Helpers_resolveFileIcon(ext) {
125             ext = ext.toLowerCase();
126             return this.extensions[ext] || 'etc.png';
127         },
128
129         /**
130          * Returns thumbnail URI for specified file
131          * @param {string} fileName
132          * @param {File} node
133          * @returns {string}
134          */
135         getThumbnailURI: function Helpers_getThumbnailURI(fileName, node) {
136             var ext = this.getFileExtension(fileName);
137
138             if (!node.thumbnailURIs) {
139                 return 'images/' + this.resolveFileIcon(ext);
140             }
141
142             if (
143                 node.thumbnailURIs[0] &&
144                     $.inArray(ext, ['.mp4', '.jpg', '.png', '.gif'])
145             ) {
146                 return node.thumbnailURIs[0];
147             }
148
149             return 'images/etc.png';
150         },
151
152         /**
153          * File name automatic number increase for copy files
154          */
155         getCopyFileName: function Helpers_getCopyFileName(
156             sourceName,
157             filesList
158         ) {
159             var i = 1, copyFileName = sourceName,
160                 filesNames = filesList.map(function (element) {
161                     return element.name;
162                 }),
163                 index = filesNames.indexOf(copyFileName),
164                 ext = this.getFileExtension(sourceName);
165
166             while (index !== -1) {
167                 if (filesList[index].isDirectory) {
168                     copyFileName = sourceName + '(' + i + ')';
169                 } else {
170                     copyFileName = this.getFileName(
171                         sourceName
172                     ) + '(' + i + ')' + ext;
173                 }
174                 i += 1;
175                 index = filesNames.indexOf(copyFileName);
176             }
177
178             return copyFileName;
179         },
180
181         /**
182          * Fixes invalid URI returned by API's File::toURI() method.
183          * See issue https://bugs.tizendev.org/jira/browse/N_SE-54639
184          */
185         fixURI: function Helpers_fixURI(invaliduri) {
186             var scheme, address, k;
187             invaliduri = invaliduri.split('://');
188             scheme = invaliduri[0];
189             invaliduri.shift();
190             address = invaliduri.join('://').split('/');
191             for (k = address.length - 1; k >= 0; k -= 1) {
192                 address[k] = encodeURIComponent(address[k]);
193             }
194             return scheme + '://' + address.join('/');
195         }
196
197     };
198 }());