Revert "Updated application sources"
[apps/web/sample/CallLog.git] / project / 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() < 10 ? "0" + dateObj.getMinutes() :
57                                         dateObj.getMinutes()) + (hours > 11 ? ' PM' : ' AM');
58                         }
59
60                         return dateObj.toTimeString().substring(0, 5);
61                 },
62                 /**
63                  * Seconds to hours converter
64                  *
65                  * @param seconds
66                  * @returns {string}
67                  */
68                 secondsToHours: function Helpers_secondsToHours(seconds) {
69                         var str = '';
70                         str = ((seconds % 60 < 10) ? '0' : '') + (seconds % 60);
71                         seconds = parseInt(seconds / 60, 10);
72                         str = ':' + str;
73                         str = ((seconds % 60 < 10) ? '0' : '') + (seconds % 60) + str;
74                         seconds = parseInt(seconds / 60, 10);
75                         str = ':' + str;
76                         str = ((seconds % 24 < 10) ? '0' : '') + (seconds % 24) + str;
77                         seconds = parseInt(seconds / 24, 10);
78                         return str;
79                 },
80
81                 /**
82                  * Returns scroll position of the given scroll view.
83                  * @param {object} element ScrollView element
84                  * @returns {number} scroll position
85                  */
86                 getScrollPosition: function Helpers_getScrollPosition(element) {
87                         return element.scrollview('getScrollPosition').y;
88                 },
89
90                 /**
91                  * Scrolls given jQuery ScrollView to given Y position.
92                  * @param {object} element ScrollView element to be scrolled.
93                  * @param {number} position Y position to scroll to
94                  */
95                 scrollTo: function Helpers_scrollTo(element, position) {
96                         // don't allow scrolling too much
97                         var maxPosition = element.children().first().outerHeight(true) - element.height();
98                         if (maxPosition < 0) {
99                                 maxPosition = 0;
100                         }
101                         if (position > maxPosition) {
102                                 position = maxPosition;
103                         }
104                         if (position < 0) {
105                                 position = 0;
106                         }
107                         // scroll
108                         element.scrollview('scrollTo', 0, -position);
109                 }
110
111         };
112 }());