Mercurial > hg > isophonics-drupal-site
comparison vendor/jcalderonzumba/gastonjs/src/Client/node.js @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 var __slice = [].slice; | |
2 | |
3 Poltergeist.Node = (function () { | |
4 var name, _fn, _i, _len, _ref; | |
5 var xpathStringLiteral; | |
6 | |
7 Node.DELEGATES = ['allText', 'visibleText', 'getAttribute', 'value', 'set', 'checked', | |
8 'setAttribute', 'isObsolete', 'removeAttribute', 'isMultiple', | |
9 'select', 'tagName', 'find', 'getAttributes', 'isVisible', | |
10 'position', 'trigger', 'input', 'parentId', 'parentIds', 'mouseEventTest', | |
11 'scrollIntoView', 'isDOMEqual', 'isDisabled', 'deleteText', 'selectRadioValue', | |
12 'containsSelection', 'allHTML', 'changed', 'getXPathForElement', 'deselectAllOptions']; | |
13 | |
14 function Node(page, id) { | |
15 this.page = page; | |
16 this.id = id; | |
17 } | |
18 | |
19 /** | |
20 * Returns the parent Node of this Node | |
21 * @return {Poltergeist.Node} | |
22 */ | |
23 Node.prototype.parent = function () { | |
24 return new Poltergeist.Node(this.page, this.parentId()); | |
25 }; | |
26 | |
27 _ref = Node.DELEGATES; | |
28 | |
29 _fn = function (name) { | |
30 return Node.prototype[name] = function () { | |
31 var args = []; | |
32 if (arguments.length >= 1) { | |
33 args = __slice.call(arguments, 0) | |
34 } | |
35 return this.page.nodeCall(this.id, name, args); | |
36 }; | |
37 }; | |
38 | |
39 //Adding all the delegates from the agent Node to this Node | |
40 for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
41 name = _ref[_i]; | |
42 _fn(name); | |
43 } | |
44 | |
45 xpathStringLiteral = function (s) { | |
46 if (s.indexOf('"') === -1) | |
47 return '"' + s + '"'; | |
48 if (s.indexOf("'") === -1) | |
49 return "'" + s + "'"; | |
50 return 'concat("' + s.replace(/"/g, '",\'"\',"') + '")'; | |
51 }; | |
52 | |
53 /** | |
54 * Gets an x,y position tailored for mouse event actions | |
55 * @return {{x, y}} | |
56 */ | |
57 Node.prototype.mouseEventPosition = function () { | |
58 var middle, pos, viewport; | |
59 | |
60 viewport = this.page.viewportSize(); | |
61 pos = this.position(); | |
62 middle = function (start, end, size) { | |
63 return start + ((Math.min(end, size) - start) / 2); | |
64 }; | |
65 | |
66 return { | |
67 x: middle(pos.left, pos.right, viewport.width), | |
68 y: middle(pos.top, pos.bottom, viewport.height) | |
69 }; | |
70 }; | |
71 | |
72 /** | |
73 * Executes a phantomjs native mouse event | |
74 * @param name | |
75 * @return {{x, y}} | |
76 */ | |
77 Node.prototype.mouseEvent = function (name) { | |
78 var pos, test; | |
79 | |
80 this.scrollIntoView(); | |
81 pos = this.mouseEventPosition(); | |
82 test = this.mouseEventTest(pos.x, pos.y); | |
83 | |
84 if (test.status === 'success') { | |
85 if (name === 'rightclick') { | |
86 this.page.mouseEvent('click', pos.x, pos.y, 'right'); | |
87 this.trigger('contextmenu'); | |
88 } else { | |
89 this.page.mouseEvent(name, pos.x, pos.y); | |
90 } | |
91 return pos; | |
92 } else { | |
93 throw new Poltergeist.MouseEventFailed(name, test.selector, pos); | |
94 } | |
95 }; | |
96 | |
97 /** | |
98 * Executes a mouse based drag from one node to another | |
99 * @param other | |
100 * @return {{x, y}} | |
101 */ | |
102 Node.prototype.dragTo = function (other) { | |
103 var otherPosition, position; | |
104 | |
105 this.scrollIntoView(); | |
106 position = this.mouseEventPosition(); | |
107 otherPosition = other.mouseEventPosition(); | |
108 this.page.mouseEvent('mousedown', position.x, position.y); | |
109 return this.page.mouseEvent('mouseup', otherPosition.x, otherPosition.y); | |
110 }; | |
111 | |
112 /** | |
113 * Checks if one node is equal to another | |
114 * @param other | |
115 * @return {boolean} | |
116 */ | |
117 Node.prototype.isEqual = function (other) { | |
118 return this.page === other.page && this.isDOMEqual(other.id); | |
119 }; | |
120 | |
121 | |
122 /** | |
123 * The value to select | |
124 * @param value | |
125 * @param multiple | |
126 */ | |
127 Node.prototype.select_option = function (value, multiple) { | |
128 var tagName = this.tagName().toLowerCase(); | |
129 | |
130 if (tagName === "select") { | |
131 var escapedOption = xpathStringLiteral(value); | |
132 // The value of an option is the normalized version of its text when it has no value attribute | |
133 var optionQuery = ".//option[@value = " + escapedOption + " or (not(@value) and normalize-space(.) = " + escapedOption + ")]"; | |
134 var ids = this.find("xpath", optionQuery); | |
135 var polterNode = this.page.get(ids[0]); | |
136 | |
137 if (multiple || !this.getAttribute('multiple')) { | |
138 if (!polterNode.getAttribute('selected')) { | |
139 polterNode.select(value); | |
140 this.trigger('click'); | |
141 this.input(); | |
142 } | |
143 return true; | |
144 } | |
145 | |
146 this.deselectAllOptions(); | |
147 polterNode.select(value); | |
148 this.trigger('click'); | |
149 this.input(); | |
150 return true; | |
151 } else if (tagName === "input" && this.getAttribute("type").toLowerCase() === "radio") { | |
152 return this.selectRadioValue(value); | |
153 } | |
154 | |
155 throw new Poltergeist.BrowserError("The element is not a select or radio input"); | |
156 | |
157 }; | |
158 | |
159 return Node; | |
160 | |
161 }).call(this); |