[FileManager]update FileManager(tizen_2.1)
[samples/web/FileManager.git] / js / app.helpers.js
1 /*jslint devel: true*/
2 /*global $ */
3
4 /**
5  * @class Helpers
6  */
7 function Helpers() {
8         'use strict';
9 }
10
11 (function () { // strict mode wrapper
12         'use strict';
13         Helpers.prototype = {
14
15                 /**
16                  * Capitalise the first letter
17                  *
18                  * @param {string} text
19                  * @returns {string}
20                  */
21                 UCFirst: function Helpers_UCFirst(text) {
22                         return text.charAt(0).toUpperCase() + text.slice(1);
23                 },
24
25                 /**
26                  * @param {string} fileName
27                  * @returns {string} file name without extension
28                  */
29                 getFileName: function Helpers_getFileName(fileName) {
30                         var fileNameLen = fileName.indexOf('.');
31                         if (fileNameLen !== -1) {
32                                 fileName = fileName.slice(0, fileNameLen);
33                         }
34                         return fileName;
35                 },
36
37                 /**
38                  * @param {string} fileName
39                  * @returns {string} extension for specified file name
40                  */
41                 getFileExtension: function Helpers_getFileExtension(fileName) {
42                         var splittedFileName = fileName.split('.'),
43                                 ext = '';
44
45                         if (splittedFileName.length > 1) {
46                                 ext = '.' + splittedFileName.pop();
47                         }
48                         return ext;
49                 },
50
51                 /**
52                  * Return icon filename for the given extension.
53                  * For example, for '.mp3' returns 'music.png'
54                  *
55                  * @param {string} ext
56                  * @return {string}
57                  */
58                 resolveFileIcon: function Helpers_resolveFileIcon(ext) {
59
60                         ext = ext.toLowerCase();
61
62                         switch (ext) {
63                         case '.jpg':
64                                 return 'img.png';
65                         case '.png':
66                                 return 'img.png';
67                         case '.gif':
68                                 return 'img.png';
69                         case '.pdf':
70                                 return 'pdf.png';
71                         case '.mp3':
72                                 return 'music.png';
73                         case '.avi':
74                                 return 'video.png';
75                         case '.mp4':
76                                 return 'video.png';
77                         case '.ppt':
78                                 return 'ppt.png';
79                         case '.txt':
80                                 return 'text.png';
81                         case '.doc':
82                                 return 'text.png';
83                         case '.xls':
84                                 return 'text.png';
85                         case '.directory':
86                                 return 'folder.png';
87                         default:
88                                 return 'etc.png';
89                         }
90                 },
91
92                 /**
93                  * Resolve file extension to MIME type
94                  *
95                  * @param {string} ext File extension
96                  * @returns {string}
97                  */
98                 resolveMimeType: function Helpers_resolveMimeType(ext) {
99                         var mime = '';
100
101                         ext = ext.toLowerCase();
102
103                         if (ext === '.jpg' || ext === '.png' || ext === '.gif') {
104                                 mime = 'image/*';
105                         } else if (ext === '.mp4' || ext === '.ogv' || ext === '.avi') {
106                                 mime = 'video/*';
107                         } else if (ext === '.mp3') {
108                                 mime = 'audio/mp3';
109                         } else if (ext === '.txt' || ext === '.doc' || ext === '.html' || ext === '.ppt' || ext === '.xls' || ext === '.pdf') {
110                                 //mime = 'text/*';
111                         }
112
113                         return mime;
114                 },
115
116                 /**
117                  * Returns thumbnail URI for specified file
118                  * @param {string} fileName
119                  * @param {File} node
120                  * @returns {string}
121                  */
122                 getThumbnailURI: function Helpers_getThumbnailURI(fileName, node) {
123                         var ext = this.getFileExtension(fileName),
124                                 thumbnailURI = '';
125
126                         if (!node.thumbnailURIs) {
127                                 thumbnailURI = 'images/' + this.resolveFileIcon(ext);
128                         } else if (node.thumbnailURIs[0] && $.inArray(ext, ['.mp4', '.jpg', '.png', '.gif'])) {
129                                 thumbnailURI = node.thumbnailURIs[0];
130                         }
131
132                         return thumbnailURI;
133                 },
134
135                 /**
136                  * File name automatic number increase for copy files
137                  */
138                 getCopyFileName: function (sourceName, filesList) {
139                         var ext = this.getFileExtension(sourceName),
140                                 fileName = this.getFileName(sourceName),
141                                 copyFileName = sourceName, i = 1;
142                         while ($.inArray(copyFileName, filesList) !== -1) {
143                                 i+=1;
144                                 copyFileName = fileName+'('+i+')'+ext;
145                         }
146                         return copyFileName;
147                 }
148         };
149 }());
150
151 (function($) {
152         'use strict';
153
154         function height(t, el) {
155                 return t.height() > el.height();
156         }
157         function width(t, el) {
158                 return t.width() > el.width();
159         }
160
161         $.fn.ellipsis = function ()
162         {
163                 return this.each(function () {
164                         var el, text, multiline, t, func;
165                         el = $(this);
166                         if(el.css("overflow") === "hidden") {
167                                 text = el.html();
168                                 multiline = el.hasClass('multiline');
169                                 t = $(this.cloneNode(true))
170                                         .hide()
171                                         .css('position', 'absolute')
172                                         .css('overflow', 'visible')
173                                         .width(multiline ? el.width() : 'auto')
174                                         .height(multiline ? 'auto' : el.height())
175                                         ;
176
177                                 el.after(t);
178
179                                 func = multiline ? height : width;
180
181                                 while (text.length > 0 && func(t, el)) {
182                                         text = text.substr(0, text.length - 1);
183                                         t.html(text + "...");
184                                 }
185
186                                 el.html(t.html());
187                                 t.remove();
188                         }
189                 });
190         };
191 }(jQuery));