Module: Angular::ClientScript
- Defined in:
- lib/angular/client_script.rb
Constant Summary collapse
- FN_waitForAngular =
*/
<<-FN function(selector, callback) { var el = document.querySelector(selector); return el; try { if (angular.getTestability) { angular.getTestability(el).whenStable(callback); } else { angular.element(el).injector().get('$browser'). notifyWhenNoOutstandingRequests(callback); } } catch (e) { callback(e); } }; FN
- FN_findBindings =
/**
* Find a list of elements in the page by their angular binding. * * @param {string} binding The binding, e.g. {{cat.name}}. * @param {boolean} exactMatch Whether the binding needs to be matched exactly * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The elements containing the binding. */ <<-FN function(binding, exactMatch, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); if (angular.getTestability) { return angular.getTestability(using). findBindings(using, binding, exactMatch); } var bindings = using.getElementsByClassName('ng-binding'); var matches = []; for (var i = 0; i < bindings.length; ++i) { var dataBinding = angular.element(bindings[i]).data('$binding'); if(dataBinding) { var bindingName = dataBinding.exp || dataBinding[0].exp || dataBinding; if (exactMatch) { var matcher = new RegExp('({|\\\\s|$|\\\\|)' + binding + '(}|\\\\s|^|\\\\|)'); if (matcher.test(bindingName)) { matches.push(bindings[i]); } } else { if (bindingName.indexOf(binding) != -1) { matches.push(bindings[i]); } } } } return matches; /* Return the whole array for webdriver.findElements. */ }; FN
- FN_findBindingsIds =
/**
* Find a list of element Ids in the page by their angular binding. * * @param {string} binding The binding, e.g. {{cat.name}}. * @param {boolean} exactMatch Whether the binding needs to be matched exactly * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The elements containing the binding. */ <<-FN function(binding, exactMatch, using, rootSelector) { var elements = findBindings(binding, exactMatch, using, rootSelector); return createCapybaraNgMatches(elements); }; FN
- FN_findRepeaterRows =
/**
* Find an array of elements matching a row within an ng-repeat. * Always returns an array of only one element for plain old ng-repeat. * Returns an array of all the elements in one segment for ng-repeat-start. * * @param {string} repeater The text of the repeater, e.g. 'cat in cats'. * @param {number} index The row index. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The row of the repeater, or an array of elements * in the first row in the case of ng-repeat-start. */ <<-FN function(repeater, index, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\\\:']; var rows = []; for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { rows.push(repeatElems[i]); } } } /* multiRows is an array of arrays, where each inner array contains one row of elements. */ var multiRows = []; for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat-start'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { var elem = repeatElems[i]; var row = []; while (elem.nodeType != 8 || elem.nodeValue.indexOf(repeater) == -1) { if (elem.nodeType == 1) { row.push(elem); } elem = elem.nextSibling; } multiRows.push(row); } } } return [rows[index]].concat(multiRows[index]); }; FN
- FN_findRepeaterRowsIds =
/**
* Find an array of element ids matching a row within an ng-repeat. * Always returns an array of only one element for plain old ng-repeat. * Returns an array of all the elements in one segment for ng-repeat-start. * * @param {string} repeater The text of the repeater, e.g. 'cat in cats'. * @param {number} index The row index. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The row of the repeater, or an array of elements * in the first row in the case of ng-repeat-start. */ <<-FN function(repeater, index, using, rootSelector) { var elements = findRepeaterRows(repeater, index, using, rootSelector); return createCapybaraNgMatches(elements); }; FN
- FN_findAllRepeaterRows =
/**
-
Find all rows of an ng-repeat.
*
-
@param string repeater The text of the repeater, e.g. ‘cat in cats’.
-
@param Element using The scope of the search.
-
@param string rootSelector The selector to use for the root app element.
*
-
@return Array.<Element> All rows of the repeater.
*/
-
<<-FN function(repeater, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var rows = []; var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\\\:']; for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { rows.push(repeatElems[i]); } } } for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat-start'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { var elem = repeatElems[i]; while (elem.nodeType != 8 || elem.nodeValue.indexOf(repeater) == -1) { if (elem.nodeType == 1) { rows.push(elem); } elem = elem.nextSibling; } } } } return rows; }; FN
- FN_findAllRepeaterRowsIds =
/**
-
Find all rows ids of an ng-repeat.
*
-
@param string repeater The text of the repeater, e.g. ‘cat in cats’.
-
@param Element using The scope of the search.
-
@param string rootSelector The selector to use for the root app element.
*
-
@return Array.<Element> All rows of the repeater.
*/
-
<<-FN function(repeater, using, rootSelector) { var elements = findAllRepeaterRows(repeater, using, rootSelector); return createCapybaraNgMatches(elements); }; FN
- FN_findRepeaterElement =
/**
* Find an element within an ng-repeat by its row and column. * * @param {string} repeater The text of the repeater, e.g. 'cat in cats'. * @param {number} index The row index. * @param {string} binding The column binding, e.g. '{{cat.name}}'. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The element in an array. */ <<-FN function(repeater, index, binding, using, rootSelector) { var matches = []; rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var rows = []; var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\\\:']; for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { rows.push(repeatElems[i]); } } } /* multiRows is an array of arrays, where each inner array contains one row of elements. */ var multiRows = []; for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat-start'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { var elem = repeatElems[i]; var row = []; while (elem.nodeType != 8 || (elem.nodeValue && elem.nodeValue.indexOf(repeater) == -1)) { if (elem.nodeType == 1) { row.push(elem); } elem = elem.nextSibling; } multiRows.push(row); } } } var row = rows[index]; var multiRow = multiRows[index]; var bindings = []; if (row) { if (angular.getTestability) { matches.push.apply( matches, angular.getTestability(using).findBindings(row, binding)); } else { if (row.className.indexOf('ng-binding') != -1) { bindings.push(row); } var childBindings = row.getElementsByClassName('ng-binding'); for (var i = 0; i < childBindings.length; ++i) { bindings.push(childBindings[i]); } } } if (multiRow) { for (var i = 0; i < multiRow.length; ++i) { var rowElem = multiRow[i]; if (angular.getTestability) { matches.push.apply( matches, angular.getTestability(using).findBindings(rowElem, binding)); } else { if (rowElem.className.indexOf('ng-binding') != -1) { bindings.push(rowElem); } var childBindings = rowElem.getElementsByClassName('ng-binding'); for (var j = 0; j < childBindings.length; ++j) { bindings.push(childBindings[j]); } } } } for (var i = 0; i < bindings.length; ++i) { var dataBinding = angular.element(bindings[i]).data('$binding'); if(dataBinding) { var bindingName = dataBinding.exp || dataBinding[0].exp || dataBinding; if (bindingName.indexOf(binding) != -1) { matches.push(bindings[i]); } } } return matches; }; FN
- FN_findRepeaterElementIds =
/**
* Find an element ids within an ng-repeat by its row and column. * * @param {string} repeater The text of the repeater, e.g. 'cat in cats'. * @param {number} index The row index. * @param {string} binding The column binding, e.g. '{{cat.name}}'. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The element in an array. */ <<-FN function(repeater, index, binding, using, rootSelector) { var elements = findRepeaterElement(repeater, index, binding, using, rootSelector); return createCapybaraNgMatches(elements); }; FN
- FN_findRepeaterColumn =
/**
* Find the elements in a column of an ng-repeat. * * @param {string} repeater The text of the repeater, e.g. 'cat in cats'. * @param {string} binding The column binding, e.g. '{{cat.name}}'. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The elements in the column. */ <<-FN function(repeater, binding, using, rootSelector) { var matches = []; rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var rows = []; var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\\\:']; for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { rows.push(repeatElems[i]); } } } /* multiRows is an array of arrays, where each inner array contains one row of elements. */ var multiRows = []; for (var p = 0; p < prefixes.length; ++p) { var attr = prefixes[p] + 'repeat-start'; var repeatElems = using.querySelectorAll('[' + attr + ']'); attr = attr.replace(/\\\\/g, ''); for (var i = 0; i < repeatElems.length; ++i) { if (repeatElems[i].getAttribute(attr).indexOf(repeater) != -1) { var elem = repeatElems[i]; var row = []; while (elem.nodeType != 8 || (elem.nodeValue && elem.nodeValue.indexOf(repeater) == -1)) { if (elem.nodeType == 1) { row.push(elem); } elem = elem.nextSibling; } multiRows.push(row); } } } var bindings = []; for (var i = 0; i < rows.length; ++i) { if (angular.getTestability) { matches.push.apply( matches, angular.getTestability(using).findBindings(rows[i], binding)); } else { if (rows[i].className.indexOf('ng-binding') != -1) { bindings.push(rows[i]); } var childBindings = rows[i].getElementsByClassName('ng-binding'); for (var k = 0; k < childBindings.length; ++k) { bindings.push(childBindings[k]); } } } for (var i = 0; i < multiRows.length; ++i) { for (var j = 0; j < multiRows[i].length; ++j) { if (angular.getTestability) { matches.push.apply( matches, angular.getTestability(using).findBindings(multiRows[i][j], binding)); } else { var elem = multiRows[i][j]; if (elem.className.indexOf('ng-binding') != -1) { bindings.push(elem); } var childBindings = elem.getElementsByClassName('ng-binding'); for (var k = 0; k < childBindings.length; ++k) { bindings.push(childBindings[k]); } } } } for (var j = 0; j < bindings.length; ++j) { var dataBinding = angular.element(bindings[j]).data('$binding'); if (dataBinding) { var bindingName = dataBinding.exp || dataBinding[0].exp || dataBinding; if (bindingName.indexOf(binding) != -1) { matches.push(bindings[j]); } } } return matches; }; FN
- FN_findRepeaterColumnIds =
/**
* Find the elements in a column of an ng-repeat. * * @param {string} repeater The text of the repeater, e.g. 'cat in cats'. * @param {string} binding The column binding, e.g. '{{cat.name}}'. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The elements in the column. */ <<-FN function(repeater, binding, using, rootSelector) { var elements = findRepeaterColumn(repeater, binding, using, rootSelector); return createCapybaraNgMatches(elements); }; FN
- FN_findByModel =
/**
* Find elements by model name. * * @param {string} model The model name. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The matching elements. */ <<-FN function(model, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); if (angular.getTestability) { return angular.getTestability(using). findModels(using, model, true); } var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\\\:']; for (var p = 0; p < prefixes.length; ++p) { var selector = '[' + prefixes[p] + 'model="' + model + '"]'; var elements = using.querySelectorAll(selector); if (elements.length) { return elements; } } }; FN
- FN_findByModelIds =
/**
* Find element ids by model name. * * @param {string} model The model name. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The matching elements. */ <<-FN function(model, using, rootSelector) { var elements = findByModel(model, using, rootSelector); return createCapybaraNgMatches(elements); }; FN
- FN_findByOptions =
/**
* Find elements by options. * * @param {string} optionsDescriptor The descriptor for the option * (i.e. fruit for fruit in fruits). * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The matching elements. */ <<-FN function(optionsDescriptor, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var prefixes = ['ng-', 'ng_', 'data-ng-', 'x-ng-', 'ng\\\\:']; for (var p = 0; p < prefixes.length; ++p) { var selector = '[' + prefixes[p] + 'options="' + optionsDescriptor + '"] option'; var elements = using.querySelectorAll(selector); if (elements.length) { return elements; } } }; FN
- FN_findByOptionsIds =
/**
* Find elements by options. * * @param {string} optionsDescriptor The descriptor for the option * (i.e. fruit for fruit in fruits). * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The matching elements. */ <<-FN function(optionsDescriptor, using, rootSelector) { var elements = findByOptions(optionsDescriptor, using, rootSelector); return createCapybaraNgMatches(elements); }; FN
- FN_findByButtonText =
/**
* Find buttons by textual content. * * @param {string} searchText The exact text to match. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The matching elements. */ <<-FN function(searchText, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var elements = using.querySelectorAll('button, input[type="button"], input[type="submit"]'); var matches = []; for (var i = 0; i < elements.length; ++i) { var element = elements[i]; var elementText; if (element.tagName.toLowerCase() == 'button') { elementText = element.innerText || element.textContent; } else { elementText = element.value; } if (elementText.trim() === searchText) { matches.push(element); } } return matches; }; FN
- FN_findByPartialButtonText =
/**
* Find buttons by textual content. * * @param {string} searchText The exact text to match. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} The matching elements. */ <<-FN function(searchText, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var elements = using.querySelectorAll('button, input[type="button"], input[type="submit"]'); var matches = []; for (var i = 0; i < elements.length; ++i) { var element = elements[i]; var elementText; if (element.tagName.toLowerCase() == 'button') { elementText = element.innerText || element.textContent; } else { elementText = element.value; } if (elementText.indexOf(searchText) > -1) { matches.push(element); } } return matches; }; FN
- FN_findByCssContainingText =
/**
* Find elements by css selector and textual content. * * @param {string} cssSelector The css selector to match. * @param {string} searchText The exact text to match. * @param {Element} using The scope of the search. * @param {string} rootSelector The selector to use for the root app element. * * @return {Array.<Element>} An array of matching elements. */ <<-FN function(cssSelector, searchText, using, rootSelector) { rootSelector = rootSelector || 'body'; using = using || document.querySelector(rootSelector); var elements = using.querySelectorAll(cssSelector); var matches = []; for (var i = 0; i < elements.length; ++i) { var element = elements[i]; var elementText = element.innerText || element.textContent; if (elementText.indexOf(searchText) > -1) { matches.push(element); } } return matches; }; FN
- FN_testForAngular =
/**
* Tests whether the angular global variable is present on a page. Retries * in case the page is just loading slowly. * * Asynchronous. * * @param {number} attempts Number of times to retry. * @param {function} asyncCallback callback */ <<-FN function(attempts, asyncCallback) { var callback = function(args) { setTimeout(function() { asyncCallback(args); }, 0); }; var check = function(n) { try { if (window.angular && window.angular.resumeBootstrap) { callback([true, null]); } else if (n < 1) { if (window.angular) { callback([false, 'angular never provided resumeBootstrap']); } else { callback([false, 'retries looking for angular exceeded']); } } else { window.setTimeout(function() {check(n - 1);}, 1000); } } catch (e) { callback([false, e]); } }; check(attempts); }; FN
- FN_evaluate =
/**
* Evalute an Angular expression in the context of a given element. * * @param {Element} element The element in whose scope to evaluate. * @param {string} expression The expression to evaluate. * * @return {?Object} The result of the evaluation. */ <<-FN function(element, expression) { return angular.element(element).scope().$eval(expression); }; FN
- FN_allowAnimations =
<<-FN function(element, value) { var ngElement = angular.element(element); if (ngElement.allowAnimations) { // AngularDart: $testability API. return ngElement.allowAnimations(value); } else { // AngularJS var enabledFn = ngElement.injector().get('$animate').enabled; return (value == null) ? enabledFn() : enabledFn(value); } }; FN
- FN_getLocationAbsUrl =
/**
* Return the current url using $location.absUrl(). * * @param {string} selector The selector housing an ng-app */ <<-FN function(selector) { var el = document.querySelector(selector); if (angular.getTestability) { return angular.getTestability(el). getLocation(); } return angular.element(el).injector().get('$location').absUrl(); }; FN
- FN_getLocation =
/**
* Get current location * * @param {string} selector The selector housing an ng-app * @param {string} url In page URL using the same syntax as $location.url(), * /path?search=a&b=c#hash */ <<-FN function(selector) { var el = document.querySelector(selector); var $injector = angular.element(el).injector(); var $location = $injector.get('$location'); return $location.url(); }; FN
- FN_setLocation =
/**
* Browse to another page using in-page navigation. * * @param {string} selector The selector housing an ng-app * @param {string} url In page URL using the same syntax as $location.url(), * /path?search=a&b=c#hash */ <<-FN function(selector, url) { var el = document.querySelector(selector); if (angular.getTestability) { return angular.getTestability(el). setLocation(url); } var $injector = angular.element(el).injector(); var $location = $injector.get('$location'); var $rootScope = $injector.get('$rootScope'); if (url !== $location.url()) { $location.url(url); $rootScope.$digest(); } return $location.url(); }; FN
Class Method Summary collapse
- .format_script(name, fn) ⇒ Object
- .format_scripts ⇒ Object
- .functions ⇒ Object
- .window_scripts ⇒ Object
Class Method Details
.format_script(name, fn) ⇒ Object
776 777 778 779 |
# File 'lib/angular/client_script.rb', line 776 def self.format_script(name, fn) "try { return (#{fn}).apply(this, arguments); }\n" + "catch(e) { throw (e instanceof Error) ? e : new Error(e); }" end |
.format_scripts ⇒ Object
781 782 783 784 785 786 787 |
# File 'lib/angular/client_script.rb', line 781 def self.format_scripts Hash[ functions.map do |name, fn| [name, format_script(name,fn)] end ] end |
.functions ⇒ Object
766 767 768 769 770 771 772 773 774 |
# File 'lib/angular/client_script.rb', line 766 def self.functions Hash[ self.constants.map do |cn| name = cn.to_s name = name[3, name.length].to_sym [name, self.const_get(cn)] end ] end |
.window_scripts ⇒ Object
789 790 791 792 |
# File 'lib/angular/client_script.rb', line 789 def self.window_scripts functions .map { |name, fn| "window.#{name} = #{fn};" } end |