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 all 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
21 changes: 19 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,27 @@
<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 sitekey="6LdRcP4SAAAAAJ4Dq1gXcD9AyhzuG77iz7E2Dmu4"></google-recaptcha>
<div><button type="submit">Submit</button></div>
</form>

</body>
</html>
60 changes: 51 additions & 9 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 @@ -22,12 +24,18 @@
@demo demo/index.html
-->
<dom-module id="google-recaptcha">
<style>
:host {
display: block;
}
</style>
<template><content></content></template>
<template>
<style>
:host {
display: inline-block;
padding: 2px 0 0 2px;
}
:host[invalid]:not([pristine]) {
border: 1px solid red;
border-radius: 3px;
}
</style>
</template>
</dom-module>

<script>
Expand All @@ -43,7 +51,38 @@
_containerId:'',
_captchaId:'',

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

_getValidity: function () {
this.pristine = false;
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: {
/**
* Whether a captcha in an iron-form is a required input.
*/
required: {
type: Boolean,
reflectToAttribute: true,
value: true
},

invalid: {
type: Boolean,
reflectToAttribute: true,
value: true
},

pristine: {
type: Boolean,
reflectToAttribute: true,
value: true
},

/**
* The total time (in milliseconds) to wait for API loading
*/
Expand Down Expand Up @@ -95,10 +134,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 @@ -135,7 +174,6 @@
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
},
ready: function () {

},
attached: function () {
if(this.sitekey===''){
Expand Down Expand Up @@ -217,6 +255,8 @@
* @param {String} response response to store
*/
_responseHandler: function (response) {
this.pristine = false;
this.invalid = false;
this.response=response;
this.fire('captcha-response',{response:response});
},
Expand All @@ -232,6 +272,8 @@
* The `expiredHandler` method fires the captcha-expired event.
*/
_expiredHandler: function () {
this.pristine = false;
this.invalid = true;
this.fire('captcha-expired');
}
});
Expand Down