7bb18693089d68530b461f8b36a265405c2a2a2f
[samples/web/CallLog.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                  * Returns date in format YYYY/MM/DD
17                  * @param dateObj
18                  * @returns {String} formatted
19                  */
20                 getShortDate: function Helpers_getShortDate(dateObj) {
21                         var dd, mm, yyyy;
22
23                         try {
24                                 yyyy = dateObj.getFullYear().toString();
25                                 mm = (dateObj.getMonth() + 1).toString(); // getMonth() is zero-based
26                                 dd  = dateObj.getDate().toString();
27                         } catch (e) {
28                                 console.error('error', e);
29                         }
30
31                         return yyyy + '/' + (mm[1] ? mm : '0' + mm[0]) + '/' + (dd[1] ? dd : '0' + dd[0]);
32                 },
33                 /**
34                  * Returns date in format DD short_month YYYY
35                  * @param dateObj
36                  * @returns {String} formatted
37                  */
38                 toNativeDate: function Helpers_toNativeDate(dateObj) {
39                         var date = dateObj.toDateString().split(" ");
40
41                         return  date[2] + ' ' + date[1] + ' ' + date[3];
42                 },
43                 /**
44                  * Returns time in format HH:mm or hh:mm ap
45                  * @param dateObj
46                  * @returns {String} formatted
47                  */
48                 toNativeTime: function Helpers_toNativeDate(dateObj) {
49                         var hours, apHours;
50
51                         if (tizen.time.getTimeFormat().indexOf('ap') != -1) {
52                                 hours = dateObj.getHours();
53                                 apHours = hours % 12 < 10 ? '0' + hours % 12 : hours % 12;
54
55                                 return apHours.toString().replace("00", "12") + ':' +
56                                         dateObj.getMinutes() + (hours > 11 ? ' PM' : ' AM');
57                         }
58
59                         return dateObj.toTimeString().substring(0, 5);
60                 },
61                 /**
62                  * Seconds to hours converter
63                  *
64                  * @param seconds
65                  * @returns {string}
66                  */
67                 secondsToHours: function Helpers_secondsToHours(seconds) {
68                         var str = '';
69                         str = ((seconds % 60 < 10) ? '0' : '') + (seconds % 60);
70                         seconds = parseInt(seconds / 60, 10);
71                         str = ':' + str;
72                         str = ((seconds % 60 < 10) ? '0' : '') + (seconds % 60) + str;
73                         seconds = parseInt(seconds / 60, 10);
74                         str = ':' + str;
75                         str = ((seconds % 24 < 10) ? '0' : '') + (seconds % 24) + str;
76                         seconds = parseInt(seconds / 24, 10);
77                         return str;
78                 },
79
80                 /**
81                  * Returns scroll position of the given scroll view.
82                  * @param {object} element ScrollView element
83                  * @returns {number} scroll position
84                  */
85                 getScrollPosition: function Helpers_getScrollPosition(element) {
86                         return element.scrollview('getScrollPosition').y;
87                 },
88
89                 /**
90                  * Scrolls given jQuery ScrollView to given Y position.
91                  * @param {object} element ScrollView element to be scrolled.
92                  * @param {number} position Y position to scroll to
93                  */
94                 scrollTo: function Helpers_scrollTo(element, position) {
95                         // don't allow scrolling too much
96                         var maxPosition = element.children().first().outerHeight(true) - element.height();
97                         if (maxPosition < 0) {
98                                 maxPosition = 0;
99                         }
100                         if (position > maxPosition) {
101                                 position = maxPosition;
102                         }
103                         if (position < 0) {
104                                 position = 0;
105                         }
106                         // scroll
107                         element.scrollview('scrollTo', 0, -position);
108                 }
109
110         };
111 }());