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