Skip to content

Commit

Permalink
Merge pull request ManageIQ#11390 from martinpovolny/qe_js
Browse files Browse the repository at this point in the history
QE javascript helpers
  • Loading branch information
himdel committed Oct 4, 2016
2 parents 9592328 + 774bf84 commit 327d52e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Expand Up @@ -33,6 +33,7 @@
//= require miq_browser_detect
//= require miq_application
//= require miq_change_stored_password
//= require miq_qe
//= require automate_import_export
//= require dialog_field_refresh
//= require excanvas
Expand Down
4 changes: 4 additions & 0 deletions app/assets/javascripts/directives/autofocus.js
Expand Up @@ -8,17 +8,21 @@ ManageIQ.angular.app.directive('autoFocus', ['$timeout', function($timeout) {
if (attr.autoFocus == "" || attr.autoFocus == "proactiveFocus") {
angular.element(scope['form_focus_' + ctrl.$name]).focus();
if (!angular.element(scope['form_focus_' + ctrl.$name]).is(":focus")) {
ManageIQ.qe.autofocus += 1;
$timeout(function () {
angular.element(scope['form_focus_' + ctrl.$name]).focus();
ManageIQ.qe.autofocus -= 1;
}, 1000);
}
}
});

scope.$on('reactiveFocus', function(_e) {
if (!angular.element(scope['form_focus_' + ctrl.$name]).is(":focus")) {
ManageIQ.qe.autofocus += 1;
$timeout(function() {
angular.element(scope['form_focus_' + ctrl.$name]).focus();
ManageIQ.qe.autofocus -= 1;
}, 0);
};
});
Expand Down
5 changes: 5 additions & 0 deletions app/assets/javascripts/miq_global.js
Expand Up @@ -76,5 +76,10 @@ if (! window.ManageIQ) {
processing: false, // is a request currently being processed?
queue: [], // a queue of pending requests
},
qe: {
autofocus: 0, // counter of pending autofocus fields
debounced: {}, // running debounces
debounce_counter: 1,
},
};
}
84 changes: 84 additions & 0 deletions app/assets/javascripts/miq_qe.js
@@ -0,0 +1,84 @@
ManageIQ.qe.get_debounce_index = function () {
if (ManageIQ.qe.debounce_counter > 30000) {
ManageIQ.qe.debounce_counter = 0;
}
return ManageIQ.qe.debounce_counter++;
}

if (typeof _ !== 'undefined' && typeof _.debounce !== 'undefined') {
var orig_debounce = _.debounce;
_.debounce = function(func, wait, options) {
var debounce_index = ManageIQ.qe.get_debounce_index();

// Override the original fn; new_func will be the original fn with wait prepended to it
// We make sure that once this fn is actually run, it decreases the counter
var new_func = function() {
try {
return func.apply({}, arguments);
} finally {
// this is run before the return above, always
delete ManageIQ.qe.debounced[debounce_index];
}
}
// Override the newly-created fn (prepended wait + original fn)
// We have to increase the counter before the waiting is initiated
var debounced_func = orig_debounce.call(this, new_func, wait, options);
var new_debounced_func = function() {
ManageIQ.qe.debounced[debounce_index] = 1;
return debounced_func.apply(this, arguments);
}
return new_debounced_func;
}
}

ManageIQ.qe.xpath = function(root, xpath) {
if (root == null) {
root = document;
}
return document.evaluate(xpath, root, null,
XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
};

ManageIQ.qe.isHidden = function(el) {
if (el === null) {
return true;
}
return el.offsetParent === null;
};

ManageIQ.qe.setAngularJsValue = function (el, value) {
var angular_elem = angular.element(elem);
var $parse = angular_elem.injector().get('$parse');
var getter = $parse(elem.getAttribute('ng-model'));
var setter = getter.assign;
angular_elem.scope().$apply(function($scope) { setter($scope, value); });
};

ManageIQ.qe.anythingInFlight = function() {
var state = ManageIQ.qe.inFlight();

return (state.autofocus != 0) ||
(state.debounce) ||
(state.document != 'complete') ||
(state.jquery != 0) ||
(state.spinner);
};

ManageIQ.qe.spinnerPresent = function() {
return (!ManageIQ.qe.isHidden(document.getElementById("spinner_div"))) &&
ManageIQ.qe.isHidden(document.getElementById("lightbox_div"));
};

ManageIQ.qe.debounceRunning = function() {
return Object.keys(ManageIQ.qe.debounced).length > 0;
};

ManageIQ.qe.inFlight = function() {
return {
autofocus: ManageIQ.qe.autofocus,
debounce: ManageIQ.qe.debounceRunning(),
document: document.readyState,
jquery: $.active,
spinner: ManageIQ.qe.spinnerPresent(),
};
};

0 comments on commit 327d52e

Please sign in to comment.