AC-1010 Fixed breadcrumb capitalization to accommodate multi-word paths. Added .jshintrc which contains jsHint directives for delinting .js files. Delinted Utilities.js.

This commit is contained in:
Chris Houseknecht
2014-02-05 11:54:16 -05:00
parent a115988a4b
commit 229df6d4f7
2 changed files with 596 additions and 585 deletions

21
.jshintrc Normal file
View File

@@ -0,0 +1,21 @@
{
// Details: https://github.com/victorporof/Sublime-JSHint#using-your-own-jshintrc-options
// Example: https://github.com/jshint/jshint/blob/master/examples/.jshintrc
// Documentation: http://www.jshint.com/docs/
"browser": true,
"jquery": true,
"esnext": true,
"globalstrict": true,
"globals": { "angular":false, "alert":false, "$AnsibleConfig":true, "$basePath":true },
"strict": false,
"quotmark": false,
"smarttabs": true,
"trailing": true,
"undef": true,
"unused": true,
"eqeqeq": true,
"indent": 4,
"onevar": true,
"newcap": false
}

View File

@@ -6,6 +6,8 @@
*
*/
/* jshint devel:true */
'use strict';
angular.module('Utilities',['RestServices', 'Utilities'])
@@ -13,18 +15,18 @@ angular.module('Utilities',['RestServices', 'Utilities'])
.factory('ClearScope', [ function() {
return function(id) {
var element = document.getElementById(id);
var element = document.getElementById(id), scope;
if (element) {
var scope = angular.element(element).scope();
scope = angular.element(element).scope();
scope.$destroy();
}
$('.tooltip').each( function(index) {
$('.tooltip').each( function() {
// Remove any lingering tooltip and popover <div> elements
$(this).remove();
});
$('.popover').each(function(index) {
$('.popover').each(function() {
// remove lingering popover <div>. Seems to be a bug in TB3 RC1
$(this).remove();
});
@@ -38,7 +40,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
$(window).unbind('resize');
}
};
}])
@@ -51,7 +53,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
.factory('Empty', [ function() {
return function(val) {
return (val === null || val === undefined || val === '') ? true : false;
}
};
}])
@@ -64,11 +66,11 @@ angular.module('Utilities',['RestServices', 'Utilities'])
else if ($(selector)) {
$(selector).addClass(cssClass);
}
}
};
})
.factory('Alert', ['$rootScope', '$location', function($rootScope, $location) {
.factory('Alert', ['$rootScope', function($rootScope) {
return function(hdr, msg, cls, action, secondAlert, disableButtons) {
// Pass in the header and message you want displayed on TB modal dialog found in index.html.
// Assumes an #id of 'alert-modal'. Pass in an optional TB alert class (i.e. alert-danger, alert-success,
@@ -116,31 +118,32 @@ angular.module('Utilities',['RestServices', 'Utilities'])
});
}
}
}
};
}])
.factory('ProcessErrors', ['$rootScope', '$cookieStore', '$log', '$location', 'Alert', 'Wait',
function($rootScope, $cookieStore, $log, $location, Alert, Wait) {
return function(scope, data, status, form, defaultMsg) {
var field, fieldErrors, msg;
Wait('stop');
if ($AnsibleConfig.debug_mode && console) {
console.log('Debug status: ' + status);
console.log('Debug data: ');
console.log(data);
}
if (status == 403) {
var msg = 'The API responded with a 403 Access Denied error. ';
if (data['detail']) {
msg += 'Detail: ' + data['detail'];
if (status === 403) {
msg = 'The API responded with a 403 Access Denied error. ';
if (data.detail) {
msg += 'Detail: ' + data.detail;
}
else {
msg += 'Please contact your system administrator.';
}
Alert(defaultMsg.hdr, msg);
}
else if ( (status == 401 && data.detail && data.detail == 'Token is expired') ||
(status == 401 && data.detail && data.detail == 'Invalid token') ) {
else if ( (status === 401 && data.detail && data.detail === 'Token is expired') ||
(status === 401 && data.detail && data.detail === 'Invalid token') ) {
$rootScope.sessionTimer.expireSession();
$location.url('/login');
}
@@ -150,12 +153,12 @@ angular.module('Utilities',['RestServices', 'Utilities'])
else if (data.detail) {
Alert(defaultMsg.hdr, defaultMsg.msg + ' ' + data.detail);
}
else if (data['__all__']) {
Alert('Error!', data['__all__']);
else if (data.__all__) {
Alert('Error!', data.__all__);
}
else if (form) {
var fieldErrors = false;
for (var field in form.fields ) {
fieldErrors = false;
for (field in form.fields ) {
if (data[field] && form.fields[field].tab) {
// If the form is part of a tab group, activate the tab
$('#' + form.name + "_tabs a[href=\"#" + form.fields[field].tab + '"]').tab('show');
@@ -186,21 +189,30 @@ angular.module('Utilities',['RestServices', 'Utilities'])
}
}
}
if ( (!fieldErrors) && defaultMsg) {
if ((!fieldErrors) && defaultMsg) {
Alert(defaultMsg.hdr, defaultMsg.msg);
}
}
else {
Alert(defaultMsg.hdr, defaultMsg.msg);
}
}
};
}])
.factory('LoadBreadCrumbs', ['$rootScope', '$routeParams', '$location', 'Empty',
function($rootScope, $routeParams, $location, Empty) {
return function(crumb) {
var title, found, j, i;
var title, found, j, i, paths, ppath, parent, child;
function toUppercase(a) {
return a.toUpperCase();
}
function singular(a) {
return (a === 'ies') ? 'y' : '';
}
//Keep a list of path/title mappings. When we see /organizations/XX in the path, for example,
//we'll know the actual organization name it maps to.
@@ -208,7 +220,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
found = false;
//crumb.title = crumb.title.charAt(0).toUpperCase() + crumb.title.slice(1);
for (i=0; i < $rootScope.crumbCache.length; i++) {
if ($rootScope.crumbCache[i].path == crumb.path) {
if ($rootScope.crumbCache[i].path === crumb.path) {
found = true;
$rootScope.crumbCache[i] = crumb;
break;
@@ -218,62 +230,47 @@ angular.module('Utilities',['RestServices', 'Utilities'])
$rootScope.crumbCache.push(crumb);
}
}
var paths = $location.path().replace(/^\//,'').split('/');
var ppath = '';
paths = $location.path().replace(/^\//,'').split('/');
ppath = '';
$rootScope.breadcrumbs = [];
if (paths.length > 1) {
var parent, child;
for (var i=0; i < paths.length - 1; i++) {
for (i=0; i < paths.length - 1; i++) {
if (i > 0 && paths[i].match(/\d+/)) {
parent = paths[i-1];
if (parent == 'inventories') {
child = 'Inventory';
}
else {
child = parent.substring(0,parent.length - 1); //assumes parent ends with 's'
child = parent.replace(/(ies$|s$)/, singular);
child = child.charAt(0).toUpperCase() + child.slice(1);
}
// find the correct title
found = false;
for (var j=0; j < $rootScope.crumbCache.length; j++) {
if ($rootScope.crumbCache[j].path == '/' + parent + '/' + paths[i]) {
for (j=0; j < $rootScope.crumbCache.length; j++) {
if ($rootScope.crumbCache[j].path === '/' + parent + '/' + paths[i]) {
child = $rootScope.crumbCache[j].title;
found = true;
break;
}
}
if (found && $rootScope.crumbCache[j]['altPath'] !== undefined) {
if (found && $rootScope.crumbCache[j].altPath !== undefined) {
// Use altPath to override default path construction
$rootScope.breadcrumbs.push({ title: child, path: $rootScope.crumbCache[j].altPath });
}
else {
$rootScope.breadcrumbs.push({ title: child, path: ppath + '/' + paths[i] });
/*if (paths[i - 1] == 'hosts') {
// For hosts, there is no /hosts, so we need to link back to the inventory
// We end up here when user has clicked refresh and the crumbcache is missing
$rootScope.breadcrumbs.push({ title: child,
path: '/inventories/' + $routeParams.inventory + '/hosts' });
}
else { */
//}
}
}
else {
if (/_/.test(paths[i])) {
// replace '_' with space and uppercase each word
paths[i] = paths[i].replace(/(?:^|_)\S/g, toUppercase)
.replace(/_/g,' ');
}
title = paths[i].charAt(0).toUpperCase() + paths[i].slice(1);
$rootScope.breadcrumbs.push({ title: title, path: ppath + '/' + paths[i] });
/*if (paths[i] == 'hosts') {
$rootScope.breadcrumbs.push({ title: paths[i], path: '/inventories/' + $routeParams.inventory +
'/hosts' });
}
else {*/
//}
}
ppath += '/' + paths[i];
}
}
}
};
}])
.factory('HelpDialog', ['$rootScope', '$location', 'Store', function($rootScope, $location, Store) {
@@ -283,12 +280,13 @@ angular.module('Utilities',['RestServices', 'Utilities'])
// HelpDialog({ defn: <HelpDefinition> })
//
var defn = params.defn;
var current_step = params.step;
var autoShow = params.autoShow || false;
var defn = params.defn,
current_step = params.step,
autoShow = params.autoShow || false;
function showHelp(step) {
var width, height, isOpen=false;
var btns, ww, width, height, isOpen=false;
current_step = step;
function buildHtml(step) {
@@ -299,8 +297,8 @@ angular.module('Utilities',['RestServices', 'Utilities'])
html += "<img src=\"" + $basePath + "img/help/" + step.img.src + "\" >";
html += "</div>\n";
html += "<div class=\"help-box\">" + step.box + "</div>";
html += (autoShow && step.autoOffNotice) ? "<div class=\"help-auto-off\"><label><input type=\"checkbox\" name=\"auto-off-checkbox\" " +
"id=\"auto-off-checkbox\"> Do not show this message in the future</label></div>\n" : "";
html += (autoShow && step.autoOffNotice) ? "<div class=\"help-auto-off\"><label><input type=\"checkbox\" " +
"name=\"auto-off-checkbox\" id=\"auto-off-checkbox\"> Do not show this message in the future</label></div>\n" : "";
return html;
}
@@ -308,7 +306,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
height = (defn.story.height) ? defn.story.height : 600;
// Limit modal width to width of viewport
var ww = $(document).width();
ww = $(document).width();
width = (width > ww) ? ww : width;
try {
@@ -323,12 +321,12 @@ angular.module('Utilities',['RestServices', 'Utilities'])
}
else {
// Define buttons based on story length
var btns = [];
btns = [];
if (defn.story.steps.length > 1) {
btns.push({
text: "Prev",
click: function(e, ui) {
if (current_step - 1 == 0) {
click: function(e) {
if (current_step - 1 === 0) {
$(e.target).button('disable');
}
if (current_step - 1 < defn.story.steps.length - 1) {
@@ -340,11 +338,11 @@ angular.module('Utilities',['RestServices', 'Utilities'])
});
btns.push({
text: "Next",
click: function(e, ui) {
click: function(e) {
if (current_step + 1 > 0) {
$(e.target).prev().button('enable');
}
if (current_step + 1 == defn.story.steps.length - 1) {
if (current_step + 1 === defn.story.steps.length - 1) {
$(e.target).button('disable');
}
showHelp(current_step + 1);
@@ -369,7 +367,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
});
// Make the buttons look like TB and add FA icons
$('.ui-dialog-buttonset button').each( function(idx) {
$('.ui-dialog-buttonset button').each( function() {
var c, h, l;
l = $(this).text();
if (l === 'Close') {
@@ -406,35 +404,30 @@ angular.module('Utilities',['RestServices', 'Utilities'])
showHelp(0);
}
};
}])
.factory('ReturnToCaller', ['$location', function($location) {
.factory('ReturnToCaller', ['$location', 'Empty', function($location, Empty) {
return function(idx) {
// Split the current path by '/' and use the array elements from 0 up to and
// including idx as the new path. If no idx value supplied, use 0 to length - 1.
var paths = $location.path().replace(/^\//,'').split('/');
var newpath = '';
idx = (idx == null || idx == undefined) ? paths.length - 1 : idx + 1;
for (var i=0; i < idx; i++) {
newpath += '/' + paths[i]
var paths = $location.path().replace(/^\//,'').split('/'),
newpath = '', i;
idx = (Empty(idx)) ? paths.length - 1 : idx + 1;
for (i=0; i < idx; i++) {
newpath += '/' + paths[i];
}
$location.path(newpath);
}
};
}])
.factory('FormatDate', [ function() {
.factory('FormatDate', ['$filter', function($filter) {
return function(dt) {
var result = ('0' + (dt.getMonth() + 1)).slice(-2) + '/';
result += ('0' + dt.getDate()).slice(-2) + '/';
result += ('0' + (dt.getFullYear() - 2000)).slice(-2) + ' ';
result += ('0' + dt.getHours()).slice(-2) + ':';
result += ('0' + dt.getMinutes()).slice(-2) + ':';
result += ('0' + dt.getSeconds()).slice(-2);
//result += ('000' + dt.getMilliseconds()).slice(-3);
return result;
}
// Wrapper for data filter- an attempt to insure all dates display in
// the same format. Pass in date object.
return $filter('date')(dt, 'MM/dd/yy HH:mm:ss');
};
}])
.factory('Wait', [ '$rootScope', function($rootScope) {
@@ -442,38 +435,40 @@ angular.module('Utilities',['RestServices', 'Utilities'])
// Display a spinning icon in the center of the screen to freeze the
// UI while waiting on async things to complete (i.e. API calls).
// Wait('start' | 'stop');
if (directive == 'start' && !$rootScope.waiting) {
var docw, doch, spinnyw, spinnyh, x, y;
if (directive === 'start' && !$rootScope.waiting) {
$rootScope.waiting = true;
var docw = $(window).width();
var doch = $(window).height();
var spinnyw = $('.spinny').width();
var spinnyh = $('.spinny').height();
var x = (docw - spinnyw) / 2;
var y = (doch - spinnyh) / 2;
docw = $(window).width();
doch = $(window).height();
spinnyw = $('.spinny').width();
spinnyh = $('.spinny').height();
x = (docw - spinnyw) / 2;
y = (doch - spinnyh) / 2;
$('.overlay').css({
width: $(document).width(),
height: $(document).height()
}).fadeIn();
$('.spinny').css({ top: y, left: x }).fadeIn(400);
}
else if (directive == 'stop' && $rootScope.waiting){
else if (directive === 'stop' && $rootScope.waiting){
$('.spinny, .overlay').fadeOut(400, function(){ $rootScope.waiting = false; });
}
}
};
}])
.factory('HideElement', [ function() {
return function(selector, action) {
// Fade-in a cloack or vail or a specific element
var target = $(selector);
var width = target.css('width');
var height = target.css('height');
var position = target.position();
var parent = target.parent();
var borderRadius = target.css('border-radius');
var backgroundColor = target.css('background-color');
var margin = target.css('margin');
var padding = target.css('padding');
var target = $(selector),
width = target.css('width'),
height = target.css('height'),
position = target.position(),
parent = target.parent(),
borderRadius = target.css('border-radius'),
backgroundColor = target.css('background-color'),
margin = target.css('margin'),
padding = target.css('padding');
parent.append("<div id=\"curtain-div\" style=\"" +
"position: absolute;" +
"top: " + position.top + "px; " +
@@ -489,26 +484,26 @@ angular.module('Utilities',['RestServices', 'Utilities'])
"display: none; " +
"\"></div>");
$('#curtain-div').show(0, action);
}
};
}])
.factory('ShowElement', [ function() {
return function() {
// And Fade-out the cloack revealing the element
$('#curtain-div').fadeOut(500, function() { $(this).remove(); });
}
};
}])
.factory('GetChoices', [ 'Rest', 'ProcessErrors', function(Rest, ProcessErrors) {
return function(params) {
// Get dropdown options
var scope = params.scope;
var url = params.url;
var field = params.field;
var variable = params.variable;
var callback = params.callback; // Optional. Provide if you want scop.$emit on completion.
var choice_name = params.choice_name; // Optional. Used when data is in something other than 'choices'
var scope = params.scope,
url = params.url,
field = params.field,
variable = params.variable,
callback = params.callback, // Optional. Provide if you want scop.$emit on completion.
choice_name = params.choice_name; // Optional. Used when data is in something other than 'choices'
if (scope[variable]) {
scope[variable].length = 0;
@@ -519,21 +514,22 @@ angular.module('Utilities',['RestServices', 'Utilities'])
Rest.setUrl(url);
Rest.options()
.success( function(data, status, headers, config) {
var choices = (choice_name) ? data.actions.GET[field][choice_name] : data.actions.GET[field].choices
.success( function(data) {
var choices, i;
choices = (choice_name) ? data.actions.GET[field][choice_name] : data.actions.GET[field].choices;
// including 'name' property so list can be used by search
for (var i=0; i < choices.length; i++) {
for (i=0; i < choices.length; i++) {
scope[variable].push({ label: choices[i][1], value: choices[i][0], name: choices[i][1]});
}
if (callback) {
scope.$emit(callback);
}
})
.error( function(data, status, headers, config) {
.error( function(data, status) {
ProcessErrors(scope, data, status, null,
{ hdr: 'Error!', msg: 'Failed to get ' + url + '. GET status: ' + status });
});
}
};
}])
/*
@@ -543,25 +539,21 @@ angular.module('Utilities',['RestServices', 'Utilities'])
*/
.factory('Find', [ function(){
return function(params) {
var list = params.list;
var key = params.key;
var val = params.val;
var found = false;
if (typeof list == 'object' && Array.isArray(list)) {
for (var i=0; i < params.list.length; i++) {
if (list[i][key] == val) {
var list = params.list,
key = params.key,
val = params.val,
found = false, i;
if (typeof list === 'object' && Array.isArray(list)) {
for (i=0; i < params.list.length; i++) {
if (list[i][key] === val) {
found = true;
break;
}
}
return (found) ? list[i] : null;
}
else {
// list parameter is not an array
return null;
}
}
};
}])
/*
@@ -573,9 +565,9 @@ angular.module('Utilities',['RestServices', 'Utilities'])
*/
.factory('DebugForm', [ function() {
return function(params) {
var form = params.form;
var scope = params.scope;
for (var fld in form.fields) {
var form = params.form,
scope = params.scope, fld;
for (fld in form.fields) {
if (scope[form.name + '_form'][fld]) {
console.log(fld + ' valid: ' + scope[form.name + '_form'][fld].$valid);
}
@@ -588,7 +580,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
}
console.log('form pristine: ' + scope[form.name + '_form'].$pristine);
console.log('form valid: ' + scope[form.name + '_form'].$valid);
}
};
}])
@@ -615,8 +607,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
var val = localStorage[key];
return (!Empty(val)) ? JSON.parse(val) : null;
}
}
};
}])
/*
@@ -635,8 +626,7 @@ angular.module('Utilities',['RestServices', 'Utilities'])
}
// Find and process the text.
$(selector).each(function() {
var setTitle = true;
var txt;
var setTitle = true, txt, w, pw, cw, df;
if ($(this).attr('title')) {
txt = $(this).attr('title');
setTitle = false;
@@ -645,27 +635,27 @@ angular.module('Utilities',['RestServices', 'Utilities'])
txt = $(this).text();
}
tmp.text(txt);
var w = tmp.width(); //text width
var pw = $(this).parent().width(); //parent width
w = tmp.width(); //text width
pw = $(this).parent().width(); //parent width
if (w > pw) {
// text is wider than parent width
if (setTitle) {
// Save the original text in the title
$(this).attr('title',txt);
}
var cw = w / txt.length; // px width per character
var df = w - pw; // difference in px
cw = w / txt.length; // px width per character
df = w - pw; // difference in px
txt = txt.substr(0, txt.length - (Math.ceil(df / cw) + 3));
$(this).text(txt + '...');
}
if (pw > w && !setTitle) {
// the parent has expanded and we previously set the title text
var txt = $(this).attr('title');
txt = $(this).attr('title');
$(this).text(txt);
}
});
}
};
}]);