605aec37a7fa2747c60075c19a023e0bd003fa0b
[yaffs-website] / vendor / jcalderonzumba / mink-phantomjs-driver / src / Resources / Script / set_value.js.twig
1 {% autoescape 'js' %}
2 (function (xpath, value) {
3   function getElement(xpath, within) {
4     var result;
5     if (within === null || within === undefined) {
6       within = document;
7     }
8     result = document.evaluate(xpath, within, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
9     if (result.snapshotLength !== 1) {
10       return null;
11     }
12     return result.snapshotItem(0);
13   }
14
15   function isInput(element) {
16     if (element === null || element === undefined) {
17       return false;
18     }
19     return (element.tagName.toLowerCase() == "input");
20   }
21
22   function isTextArea(element) {
23     if (element === null || element === undefined) {
24       return false;
25     }
26     return (element.tagName.toLowerCase() == "textarea");
27   }
28
29   function isSelect(element) {
30     if (element === null || element === undefined) {
31       return false;
32     }
33     return (element.tagName.toLowerCase() == "select");
34   }
35
36   function deselectAllOptions(element) {
37     var i, l = element.options.length;
38     for (i = 0; i < l; i++) {
39       element.options[i].selected = false;
40     }
41   }
42
43   function xpathStringLiteral(s) {
44     if (s.indexOf('"') === -1)
45       return '"' + s + '"';
46     if (s.indexOf("'") === -1)
47       return "'" + s + "'";
48     return 'concat("' + s.replace(/"/g, '",\'"\',"') + '")';
49   }
50
51   function clickOnElement(element) {
52     // create a mouse click event
53     var event = document.createEvent('MouseEvents');
54     event.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
55
56     // send click to element
57     element.dispatchEvent(event);
58
59     //After dispatching the event let's wait for 2 seconds at least...
60     return setTimeout(function () {
61     }, 2);
62   }
63
64   function dispatchChange(element) {
65     var tagName =element.tagName.toLowerCase();
66     var elementType = element.getAttribute("type");
67     if (tagName != "option" || (tagName == "input" && elementType == "radio")){
68       return true;
69     }
70     //Force the change when element is option
71     var event;
72     event = document.createEvent('HTMLEvents');
73     event.initEvent('change', true, false);
74     element.dispatchEvent(event);
75     return true;
76   }
77
78   function selectOptionOnElement(element, option, multiple) {
79     var polterAgent = window.__poltergeist;
80     var escapedOption = xpathStringLiteral(option);
81     // The value of an option is the normalized version of its text when it has no value attribute
82     var optionQuery = ".//option[@value = " + escapedOption + " or (not(@value) and normalize-space(.) = " + escapedOption + ")]";
83     var ids = polterAgent.find("xpath", optionQuery, element);
84     var polterNode = polterAgent.get(ids[0]);
85     var optionElement = polterNode.element;
86
87     if (multiple || !element.multiple) {
88       if (!optionElement.selected) {
89         clickOnElement(optionElement);
90         optionElement.selected = true;
91       }
92       return dispatchChange(optionElement);
93     }
94
95     deselectAllOptions(element);
96     clickOnElement(optionElement);
97     optionElement.selected = true;
98     return dispatchChange(optionElement);
99   }
100
101   function selectSetValue(element, value) {
102     var option;
103     if ((Array.isArray && Array.isArray(value)) || (value instanceof Array)) {
104       deselectAllOptions(element);
105       for (option in value) {
106         if (value.hasOwnProperty(option)) {
107           selectOptionOnElement(element, value[option], true);
108         }
109       }
110       return true;
111     }
112
113     selectOptionOnElement(element, value, false);
114     return true;
115   }
116
117   function selectRadioValue(element, value) {
118     if (element.value === value) {
119       clickOnElement(element);
120       element.checked=true;
121       dispatchChange(element);
122       return true;
123     }
124
125     var formElements = element.form.elements;
126     var name = element.getAttribute("name");
127     var radioElement, i;
128
129     if (!name) {
130       return null;
131     }
132
133     for (i = 0; i < formElements.length; i++) {
134       radioElement = formElements[i];
135       if (radioElement.tagName.toLowerCase() == 'input' && radioElement.type.toLowerCase() == 'radio' && radioElement.name === name) {
136         if (value === radioElement.value) {
137           clickOnElement(radioElement);
138           radioElement.checked=true;
139           dispatchChange(radioElement);
140           return true;
141         }
142       }
143     }
144
145     return null;
146   }
147
148   function inputSetValue(element, value, elementXpath) {
149     var allowedTypes = ['submit', 'image', 'button', 'reset'];
150     var elementType = element.type.toLowerCase();
151     var textLikeInputType = ['file', 'text', 'password', 'url', 'email', 'search', 'number', 'tel', 'range', 'date', 'month', 'week', 'time', 'datetime', 'color', 'datetime-local'];
152
153     if (allowedTypes.indexOf(elementType) !== -1) {
154       return null;
155     }
156
157     if (elementType == "checkbox") {
158       var booleanValue = false;
159       if (value == "1" || value == 1) {
160         booleanValue = true;
161       } else if (value == "0" || value == 0) {
162         booleanValue = false;
163       }
164       if ((element.checked && !booleanValue) || (!element.checked && booleanValue)) {
165         clickOnElement(element);
166         dispatchChange(element);
167       }
168       return true;
169     }
170
171     if (elementType == "radio") {
172       return selectRadioValue(element, value);
173     }
174
175     if (textLikeInputType.indexOf(elementType) !== -1) {
176       return textAreaSetValue(elementXpath, value);
177     }
178
179     //No support for the moment for file stuff or other input types
180     return null;
181
182   }
183
184   function textAreaSetValue(elementXpath, value) {
185     var polterAgent = window.__poltergeist;
186     var ids = polterAgent.find("xpath", elementXpath, document);
187     var polterNode = polterAgent.get(ids[0]);
188     polterNode.set(value);
189     return true;
190   }
191
192   var node = getElement(xpath);
193   if (node === null) {
194     return null;
195   }
196
197   if (isSelect(node)) {
198     return selectSetValue(node, value);
199   }
200
201   if (isInput(node)) {
202     return inputSetValue(node, value, xpath);
203   }
204
205   if (isTextArea(node)) {
206     return textAreaSetValue(xpath, value);
207   }
208
209   //for the moment everything else also to textArea stuff
210   return textAreaSetValue(xpath, value);
211
212 }('{{xpath}}', JSON.parse('{{ value }}')));
213 {% endautoescape %}