define([
'jquery',
+ 'lodash',
'js/util'
], function (
$,
+ _,
Util
) {
'use strict';
// the path for images
var PATH_TIC_IMAGES = '/tmp/tic/images/';
- function updateList(socket) {
- if (!_.isEmpty(socket)) {
- client = socket;
- }
-
- var msgData = {
- path: PATH_TIC_IMAGES
- };
+ // template
+ var IMAGE_LOG = '<p><%= log %></p>';
+ var IMAGE_ITEM = '<li class="list-group-item image-item">' +
+ '<p class="image-list-name" title="<%= fileName %>"><%= fileName %></p>' +
+ '<p class="image-list-detail">Size: <%= fileSize %></p>' +
+ '<p class="image-list-detail">Updated: <%= fileTime %></p>' +
+ '<a class="image-list-btndownload" href="<%= hrefPath %>" date-name="<%= fileName %>" download="download">Download</a>' +
+ '</li>';
- client.emit('ws/fs/image/list/from', msgData);
+ function initSocket(socket) {
+ client = socket;
client.on('ws/fs/image/list/to', function (data) {
- var list, tableDomElem;
-
- list = data.list;
- tableDomElem = $('#tic-image-list');
- tableDomElem.empty();
-
- list.forEach(function (file) {
- var liElem, aElem, spanElem, fileName, hrefPath;
-
- fileName = file.name;
- hrefPath = '/api/fs/download/' + fileName;
-
- liElem = document.createElement('li');
- liElem.setAttribute('class', 'list-group-item image-item');
-
- spanElem = document.createElement('p');
- spanElem.innerText = fileName;
- spanElem.setAttribute('title', fileName);
- spanElem.setAttribute('class', 'image-list-name');
- liElem.appendChild(spanElem);
-
- spanElem = document.createElement('p');
- spanElem.innerText = 'Size: ' + Util.bytesToSize(file.size);
- spanElem.setAttribute('class', 'image-list-detail');
- liElem.appendChild(spanElem);
-
- spanElem = document.createElement('p');
- spanElem.innerText = 'Updated: ' + new Date(file.birthtime).toLocaleString();
- spanElem.setAttribute('class', 'image-list-detail');
- liElem.appendChild(spanElem);
-
- aElem = document.createElement('a');
- aElem.innerText = 'Download';
- aElem.setAttribute('class', 'image-list-btndownload');
- aElem.setAttribute('href', hrefPath);
- aElem.setAttribute('data-name', fileName);
- aElem.setAttribute('download', 'download');
-
- liElem.appendChild(aElem);
- tableDomElem.append(liElem);
+ var $imageList = $('#tic-image-list').empty();
+ _.forEach(data.list, function (file) {
+ var imageItem = _.template(IMAGE_ITEM)({
+ fileName: file.name,
+ fileSize: Util.bytesToSize(file.size),
+ fileTime: new Date(file.birthtime).toLocaleString(),
+ hrefPath: '/api/fs/download/' + file.name
+ });
+ $imageList.append(imageItem);
+ });
+ });
+
+ client.on('ws/fs/image/add/to', function (data) {
+ var logItem = _.template(IMAGE_LOG)({
+ log: data
});
+ var $imageNewLog = $('#tic-image-new-log').append(logItem);
+ $imageNewLog.animate({ scrollTop : $imageNewLog.height() }, 'slow');
});
+ /**
+ * TODO
+ *
+ * manage the logs for file.
+ */
+ // when finished
+ client.on('ws/fs/image/add/finished', function (data) {
+ // button enabled
+ $('#tic-image-create').prop('disabled', false);
+
+ // upate the list of images
+ updateList();
+ });
+ }
+
+ function updateList() {
+ var msgData = {
+ path: PATH_TIC_IMAGES
+ };
+ client.emit('ws/fs/image/list/from', msgData);
}
function updateSummary() {
}
function createImage(pathKsFile) {
+ $('#tic-image-new-log').empty();
+
/**
* TODO - checks the msgData
*/
- var msgData, tableDomElem;
- msgData = {
+ var msgData = {
pathKsFile: pathKsFile,
pathOutput: PATH_TIC_IMAGES
};
-
- tableDomElem = $('#tic-image-new-log');
- tableDomElem.empty();
-
client.emit('ws/fs/image/add/from', msgData);
-
- client.on('ws/fs/image/add/to', function (data) {
- var elem = document.createElement('p');
- elem.innerText = data;
- tableDomElem.append(elem);
-
- $('#tic-image-new').animate({ scrollTop : $('#tic-image-new').height() }, 'slow');
- });
-
- /**
- * TODO
- *
- * manage the logs for file.
- */
- // when finished
- client.on('ws/fs/image/add/finished', function (data) {
- // button enabled
- $('#tic-image-create').prop('disabled', false);
-
- // upate the list of images
- updateList(null);
- });
}
function getExportsUrl() {
}
function getKickstartRecipeFile() {
- return new Promise(function (resolve, reject) {
- var msgData = {
- recipe: {
- name: 'default'
- },
- packages: _.map(checkedPackagesList, 'name'),
- output: PATH_TIC_KS
- };
-
- $.ajax({
- type: 'POST',
- contentType: 'application/json; charset=UTF-8',
- dataType: 'json',
- data: JSON.stringify(msgData),
- processData: false,
- url: getExportsUrl(),
- success: function (res) {
- resolve(res.data);
- },
- error: function (err) {
- reject(err.responseText);
- }
- });
+ var msgData = {
+ recipe: {
+ name: 'default'
+ },
+ packages: _.map(checkedPackagesList, 'name'),
+ output: PATH_TIC_KS
+ };
+ return Util.POST(getExportsUrl(), msgData)
+ .then(function (result) {
+ return result.data
});
}
return {
/**
+ * Initialize listener of socket
+ * @method initSocket
+ */
+ initSocket: initSocket,
+ /**
* Update summary in image page
* @method updateSummary
*/
updateList: updateList
}
-});
\ No newline at end of file
+});
return urlRegex.test(url);
}
+ function POST(url, postData) {
+ return new Promise(function (resolve, reject) {
+ $.ajax({
+ type: 'POST',
+ contentType: 'application/json; charset=UTF-8',
+ dataType: 'json',
+ data: JSON.stringify(postData),
+ processData: false,
+ url: url,
+ success: function (result) {
+ resolve(result);
+ },
+ error: function (err) {
+ reject(err);
+ }
+ });
+ })
+ }
+
return {
/**
* Set smooth scrolling, one page websites
* Display the confirm dialog.
* @method showLoadingDialog
* @param {string} content in dialog
- * @return Promise
+ * @return {Promise}
*/
showConfirmDialog: showConfirmDialog,
*/
bytesToSize: bytesToSize,
- validateURL: validateURL
+ /**
+ * Makes the element require a valid url.
+ * @method validateURL
+ * @param {string} url - A string containing the URL.
+ * @return {boolean} Return true, if the value is a valid url.
+ */
+ validateURL: validateURL,
+
+ /**
+ * Load data from the server using a HTTP POST request.
+ * @method POST
+ * @param {string} url - A string containing the URL to which the request is sent.
+ * @param {object} data - A plain object or string that is sent to the server with the request.
+ * @return {Promise}
+ */
+ POST: POST
}
});