Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

proof of concept demo with iron-form #12

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
],
"main": "google-recaptcha.html",
"dependencies": {
"polymer": "Polymer/polymer#^1.0.0"
"iron-form": "PolymerElements/iron-form#^1.0.9",
"polymer": "Polymer/polymer#^1.0.0",
"iron-form-element-behavior": "PolymerElements/iron-form-element-behavior#~1.0.4",
"iron-validatable-behavior": "PolymerElements/iron-validatable-behavior#~1.0.4"
},
"devDependencies": {
"web-component-tester": "*",
Expand Down
22 changes: 20 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</style>
<script src="../../webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="../google-recaptcha.html">
<link rel="import" href="../iron-form/iron-form.html">
<script type="text/javascript">
window.addEventListener('WebComponentsReady', function(e) {
var rec = document.querySelector('google-recaptcha');
Expand All @@ -40,11 +41,28 @@
<body>
<p>Default re-captcha</p>
<google-recaptcha sitekey="6LdRcP4SAAAAAJ4Dq1gXcD9AyhzuG77iz7E2Dmu4"></google-recaptcha>

<p>Event message (working with the first captcha)</p>
<p id="eventMessage"></p>

<hr>

<p>Dark re-captcha</p>
<google-recaptcha theme="dark" sitekey="6LdRcP4SAAAAAJ4Dq1gXcD9AyhzuG77iz7E2Dmu4"></google-recaptcha>

<hr>

<p>Audio re-captcha</p>
<google-recaptcha type="audio" sitekey="6LdRcP4SAAAAAJ4Dq1gXcD9AyhzuG77iz7E2Dmu4"></google-recaptcha>
<p>Event message (working with the first captcha)</p>
<p id="eventMessage"></p>

<hr>

<form is="iron-form" style="border: 1px solid #ccc; padding: 0 25px 25px;">
<h2>Demo within an iron-form</h2>
<google-recaptcha required sitekey="6LdRcP4SAAAAAJ4Dq1gXcD9AyhzuG77iz7E2Dmu4"
style="display: inline-block; padding-top: 2px; padding-left: 2px;"></google-recaptcha>
<div><button type="submit">Submit</button></div>
</form>

</body>
</html>
26 changes: 24 additions & 2 deletions google-recaptcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
license that can be found in the LICENSE file.
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-form-element-behavior/iron-form-element-behavior.html">
<link rel="import" href="../iron-validatable-behavior/iron-validatable-behavior.html">

<!--
Polymer element for Google reCAPTCHA
Expand All @@ -26,6 +28,10 @@
:host {
display: block;
}
:host.invalid {
border: 1px solid red;
border-radius: 3px;
}
</style>
<template><content></content></template>
</dom-module>
Expand All @@ -43,6 +49,18 @@
_containerId:'',
_captchaId:'',

_invalid: true,

behaviors: [
Polymer.IronFormElementBehavior,
Polymer.IronValidatableBehavior
],

_getValidity: function () {
this.toggleClass('invalid', this._invalid);
return !this._invalid;
},
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cbalit wrote:

i wonder why you call toggle class in _getValidity method ?

a google-recaptcha element's _getValidity method now gets called when you submit an iron-form that contains it. Without this toggleClass call, the red border would not appear around an invalid recaptcha upon attempting to submit the form.


properties: {
/**
* The total time (in milliseconds) to wait for API loading
Expand Down Expand Up @@ -95,10 +113,10 @@
},

observers: [
'_validate(theme, type, timeout)'
'_checkOptions(theme, type, timeout)'
],

_validate:function () {
_checkOptions: function () {
if(this.theme!='dark' && this.theme!='light'){
this.theme='light';
}
Expand Down Expand Up @@ -217,6 +235,8 @@
* @param {String} response response to store
*/
_responseHandler: function (response) {
this._invalid = false;
this.toggleClass('invalid', this._invalid);
this.response=response;
this.fire('captcha-response',{response:response});
},
Expand All @@ -232,6 +252,8 @@
* The `expiredHandler` method fires the captcha-expired event.
*/
_expiredHandler: function () {
this._invalid = true;
this.toggleClass('invalid', this._invalid);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we should be modifying the dom imperatively like this rather than achieving this via a declarative binding, but just wanted to get something working.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(something like this and this is what I meant by "achieving this via a declarative binding")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i.e. invalid="{{_invalid}}" or some such. Is that possible/preferable?)

this.fire('captcha-expired');
}
});
Expand Down