Scribe.js/static/js/services/logs.js

122 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-11-11 21:20:33 +01:00
(function () {
'use strict';
2014-11-13 21:33:37 +01:00
/**
* Logs service
*
* A bridge between dates or folder controller and logs controller
*
* Stores files to show in logs viewer
*/
2014-11-11 21:20:33 +01:00
window.app.factory('logs', [function () {
2014-11-13 21:33:37 +01:00
/**
* files
*
* @type {Array} Array of object
*
{
name : 'file name',
path : 'file full path (with logWriter dir)',
selected : True //if false, file belongs to currentFiles
//but won't be show by default in the log viewer
}
*
*/
2014-11-11 21:20:33 +01:00
var files = [];
2014-11-13 21:33:37 +01:00
/**
* basename
*
* @see http://stackoverflow.com/questions/3820381/need-a-basename-function-in-javascript#comment29942319_15270931
*
* @param {String} path
*/
2014-11-11 21:20:33 +01:00
var basename = function (path) {
return path.split(/[\\/]/).pop();
};
2014-11-13 21:33:37 +01:00
/**
* pathInFiles
*
* Is the path `search` in `files` ?
*
* @param {String} search
*/
2014-11-11 21:20:33 +01:00
var pathInFiles = function (search) {
var result = false;
files.forEach(function (elem) {
if (elem.path === search) {
result = true;
}
});
return result;
};
return {
2014-11-13 21:33:37 +01:00
/**
* getLogs
*
* @return all files
*/
2014-11-11 21:20:33 +01:00
getLogs : function () {
return files;
},
2014-11-13 21:33:37 +01:00
/**
* addLog
*
* Add a log file
*
* @param {String} path The path's file
* @param {Boolean} selected Whether the file is selected. Default false.
*
* @return all files
*/
2014-11-11 21:20:33 +01:00
addLog : function (path, selected) {
if (!pathInFiles(path)) {
var file;
2014-11-13 21:33:37 +01:00
//if path is string, we constructthe good object
2014-11-11 21:20:33 +01:00
if (typeof path === 'string') {
file = {
name : basename(path),
path : path,
selected : selected || false
};
} else {
2014-11-13 21:33:37 +01:00
//TODO : check something ?
2014-11-11 21:20:33 +01:00
file = path;
}
files.push(file);
}
return files;
},
2014-11-13 21:33:37 +01:00
/**
* setLogs
*
* Override `files`
*
* @param {Array} newFiles
*/
2014-11-11 21:20:33 +01:00
setLogs : function (newFiles) {
2014-11-13 21:33:37 +01:00
//simply delete null and undefined values that can comes from map()
2014-11-11 21:20:33 +01:00
files = newFiles.filter(function (item) {
2014-11-13 21:33:37 +01:00
return !!item;
2014-11-11 21:20:33 +01:00
});
}
};
}]);
}());