[TIC-Web] add notification dialog when image creation
[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), 2) + ' ' + 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 getAppConfig() {
84         return new Promise(function (resolve, reject) {
85             socket.emit('ws/app/config/from');
86             socket.on('ws/app/config/to', function (data) {
87                 resolve(data);
88             });
89         })
90     }
91
92     function getWebSocket() {
93         return socket;
94     }
95
96     function _init() {
97         socket = io.connect();
98     }
99
100     _init();
101
102     return {
103         /**
104          * Set smooth scrolling, one page websites
105          * @method setAnimateScroll
106          */
107         setAnimateScroll: setAnimateScroll,
108
109         /**
110          * Display or hide the loading dialog.
111          * @method showLoadingDialog
112          * @param {boolean} display flag
113          */
114         showLoadingDialog: showLoadingDialog,
115
116         /**
117          * Display the info dialog.
118          * @method showInfoDialog
119          * @param {string} content in dialog
120          */
121         showInfoDialog: showInfoDialog,
122
123         /**
124          * Display the alert dialog.
125          * @method showAlertDialog
126          * @param {string} content in dialog
127          */
128         showAlertDialog: showAlertDialog,
129
130         /**
131          * Display the confirm dialog.
132          * @method showLoadingDialog
133          * @param {string} content in dialog
134          * @return {Promise}
135          */
136         showConfirmDialog: showConfirmDialog,
137
138         /*
139          * Convert size in bytes to KB, MB, GB, TB
140          * {@link http://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript}
141          * @method bytesToSize
142          * @param {number} byte number
143          */
144         bytesToSize: bytesToSize,
145
146         /**
147          * Makes the element require a valid url.
148          * @method validateURL
149          * @param {string} url - A string containing the URL.
150          * @return {boolean} Return true, if the value is a valid url.
151          */
152         validateURL: validateURL,
153
154         /**
155          * Load data from the server using a HTTP POST request.
156          * @method POST
157          * @param {string} url - A string containing the URL to which the request is sent.
158          * @param {object} data - A plain object or string that is sent to the server with the request.
159          * @return {Promise}
160          */
161         POST: POST,
162
163          /**
164          * Get WebSocket client
165          * @method getWebSocket
166          * @return {object} socket
167          */
168         getWebSocket: getWebSocket,
169
170         /**
171          * Load app configuration from config.json of root
172          * @method getAppConfig
173          * @return {Promise}
174          */
175         getAppConfig: getAppConfig,
176     }
177
178 });