[TIC-Web] source clean up
[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 showInfoDialog(content) {
33         $('#tic-info-content').html(content);
34         $('#tic-info-dialog').modal('show');
35     }
36
37     function showConfirmDialog(content) {
38         return new Promise(function (resolve, reject) {
39             $('#tic-confirm-content').html(content);
40             $('#tic-confirm-ok').on('click', function () {
41                 resolve();
42             })
43             $('#tic-confirm-cancel').on('click', function () {
44                 reject();
45             })
46             $('#tic-confirm-dialog').modal('show');
47         });
48     }
49
50     function bytesToSize(bytes) {
51         var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
52         if (bytes === 0) {
53             return '0 Byte';
54         }
55         var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
56         return Math.round(bytes / Math.pow(1024, i)) + ' ' + sizes[i];
57     };
58
59     function validateURL(url) {
60         var urlRegex = /(file|ftp|http|https):\/\/(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
61         return urlRegex.test(url);
62     }
63
64     function POST(url, postData) {
65         return new Promise(function (resolve, reject) {
66             $.ajax({
67                 type: 'POST',
68                 contentType: 'application/json; charset=UTF-8',
69                 dataType: 'json',
70                 data: JSON.stringify(postData),
71                 processData: false,
72                 url: url,
73                 success: function (result) {
74                     resolve(result);
75                 },
76                 error: function (err) {
77                     reject(err);
78                 }
79             });
80         });
81     }
82
83     function GET(url) {
84         return new Promise(function (resolve, reject) {
85             $.ajax({
86                 type: 'GET',
87                 contentType: 'application/json; charset=UTF-8',
88                 dataType: 'json',
89                 url: url,
90                 success: function (result) {
91                     resolve(result);
92                 },
93                 error: function (err) {
94                     reject(err);
95                 }
96             });
97         });
98     }
99
100     function DELETE(url) {
101         return new Promise(function (resolve, reject) {
102             $.ajax({
103                 type: 'DELETE',
104                 contentType: 'application/json; charset=UTF-8',
105                 dataType: 'json',
106                 url: url,
107                 success: function (result) {
108                     resolve(result);
109                 },
110                 error: function (err) {
111                     reject(err);
112                 }
113             });
114         });
115     }
116
117     function getAppConfig() {
118         return new Promise(function (resolve, reject) {
119             socket.emit('ws/app/config/from');
120             socket.on('ws/app/config/to', function (data) {
121                 resolve(data);
122             });
123         });
124     }
125
126     function getWebSocket() {
127         return socket;
128     }
129
130     function _init() {
131         socket = io.connect();
132     }
133
134     _init();
135
136     return {
137         /**
138          * Set smooth scrolling, one page websites
139          * @method setAnimateScroll
140          */
141         setAnimateScroll: setAnimateScroll,
142
143         /**
144          * Display or hide the loading dialog.
145          * @method showLoadingDialog
146          * @param {boolean} display flag
147          */
148         showLoadingDialog: showLoadingDialog,
149
150         /**
151          * Display the info dialog.
152          * @method showInfoDialog
153          * @param {string} content in dialog
154          */
155         showInfoDialog: showInfoDialog,
156
157         /**
158          * Display the alert dialog.
159          * @method showAlertDialog
160          * @param {string} content in dialog
161          */
162         showAlertDialog: showAlertDialog,
163
164         /**
165          * Display the confirm dialog.
166          * @method showLoadingDialog
167          * @param {string} content in dialog
168          * @return {Promise}
169          */
170         showConfirmDialog: showConfirmDialog,
171
172         /*
173          * Convert size in bytes to KB, MB, GB, TB
174          * {@link http://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript}
175          * @method bytesToSize
176          * @param {number} byte number
177          */
178         bytesToSize: bytesToSize,
179
180         /**
181          * Makes the element require a valid url.
182          * @method validateURL
183          * @param {string} url - A string containing the URL.
184          * @return {boolean} Return true, if the value is a valid url.
185          */
186         validateURL: validateURL,
187
188         /**
189          * Load data from the server using a HTTP POST request.
190          * @method POST
191          * @param {string} url - A string containing the URL to which the request is sent.
192          * @param {object} data - A plain object or string that is sent to the server with the request.
193          * @return {Promise}
194          */
195         POST: POST,
196
197         /**
198          * Load data from the server using a HTTP GET request.
199          * @method GET
200          * @param {string} url - A string containing the URL to which the request is sent.
201          * @return {Promise}
202          */
203         GET: GET,
204
205         /**
206          * Load data from the server using a HTTP DELETE request.
207          * @method DELETE
208          * @param {string} url - A string containing the URL to which the request is sent.
209          * @return {Promise}
210          */
211         DELETE: DELETE,
212
213          /**
214          * Get WebSocket client
215          * @method getWebSocket
216          * @return {object} socket
217          */
218         getWebSocket: getWebSocket,
219
220         /**
221          * Load app configuration from config.json of root
222          * @method getAppConfig
223          * @return {Promise}
224          */
225         getAppConfig: getAppConfig,
226     }
227
228 });