b3904537b7c4df6156c8e34c054587d494b2bcc8
[archive/20170607/tools/tic.git] / public / src / js / util.js
1 define([
2     'jquery'
3 ], function (
4     $
5 ) {
6     'use strict';
7
8     var socket = null;
9
10     function setAnimateScroll() {
11         $('a.tic-page-scroll').bind('click', function(event) {
12             var body = $('html, body');
13             var $anchor = $(this);
14             body.stop().animate({scrollTop: $($anchor.attr('href')).offset().top}, 300, 'swing');
15             event.preventDefault();
16         });
17     }
18
19     function showLoadingDialog(display) {
20         if (display) {
21             $('#tic-load-dialog').modal('show');
22         } else {
23             $('#tic-load-dialog').modal('hide');
24         }
25     }
26
27     function showAlertDialog(content) {
28         $('#tic-alert-content').html(content);
29         $('#tic-alert-dialog').modal('show');
30     }
31
32     function showConfirmDialog(content) {
33         return new Promise(function (resolve, reject) {
34             $('#tic-confirm-content').html(content);
35             $('#tic-confirm-ok').on('click', function () {
36                 resolve();
37             })
38             $('#tic-confirm-cancel').on('click', function () {
39                 reject();
40             })
41             $('#tic-confirm-dialog').modal('show');
42         });
43     }
44
45     function bytesToSize(bytes) {
46         var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
47         if (bytes == 0) {
48             return '0 Byte';
49         }
50         var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
51         return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
52     };
53
54     function validateURL(url) {
55         var urlRegex = /(file|ftp|http|https):\/\/(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
56         return urlRegex.test(url);
57     }
58
59     function POST(url, postData) {
60         return new Promise(function (resolve, reject) {
61             $.ajax({
62                 type: 'POST',
63                 contentType: 'application/json; charset=UTF-8',
64                 dataType: 'json',
65                 data: JSON.stringify(postData),
66                 processData: false,
67                 url: url,
68                 success: function (result) {
69                     resolve(result);
70                 },
71                 error: function (err) {
72                     reject(err);
73                 }
74             });
75         })
76     }
77
78     function getAppConfig() {
79         return new Promise(function (resolve, reject) {
80             socket.emit('ws/app/config/from');
81             socket.on('ws/app/config/to', function (data) {
82                 resolve(data);
83             });
84         })
85     }
86
87     function getWebSocket() {
88         return socket;
89     }
90
91     function _init() {
92         socket = io.connect();
93     }
94
95     _init();
96
97     return {
98         /**
99          * Set smooth scrolling, one page websites
100          * @method setAnimateScroll
101          */
102         setAnimateScroll: setAnimateScroll,
103
104         /**
105          * Display or hide the loading dialog.
106          * @method showLoadingDialog
107          * @param {boolean} display flag
108          */
109         showLoadingDialog: showLoadingDialog,
110
111         /**
112          * Display the alert dialog.
113          * @method showAlertDialog
114          * @param {string} content in dialog
115          */
116         showAlertDialog: showAlertDialog,
117
118         /**
119          * Display the confirm dialog.
120          * @method showLoadingDialog
121          * @param {string} content in dialog
122          * @return {Promise}
123          */
124         showConfirmDialog: showConfirmDialog,
125
126         /*
127          * Convert size in bytes to KB, MB, GB, TB
128          * {@link http://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript}
129          * @method bytesToSize
130          * @param {number} byte number
131          */
132         bytesToSize: bytesToSize,
133
134         /**
135          * Makes the element require a valid url.
136          * @method validateURL
137          * @param {string} url - A string containing the URL.
138          * @return {boolean} Return true, if the value is a valid url.
139          */
140         validateURL: validateURL,
141
142         /**
143          * Load data from the server using a HTTP POST request.
144          * @method POST
145          * @param {string} url - A string containing the URL to which the request is sent.
146          * @param {object} data - A plain object or string that is sent to the server with the request.
147          * @return {Promise}
148          */
149         POST: POST,
150
151          /**
152          * Get WebSocket client
153          * @method getWebSocket
154          * @return {object} socket
155          */
156         getWebSocket: getWebSocket,
157
158         /**
159          * Load app configuration from config.json of root
160          * @method getAppConfig
161          * @return {Promise}
162          */
163         getAppConfig: getAppConfig,
164     }
165
166 });