summaryrefslogtreecommitdiff
path: root/httemplate/elements
diff options
context:
space:
mode:
Diffstat (limited to 'httemplate/elements')
-rw-r--r--httemplate/elements/freeside.css8
-rw-r--r--httemplate/elements/jquery.deserialize.min.js8
-rw-r--r--httemplate/elements/location.html86
-rw-r--r--httemplate/elements/polyfill.js30
4 files changed, 132 insertions, 0 deletions
diff --git a/httemplate/elements/freeside.css b/httemplate/elements/freeside.css
index dbd27cbaa..5eb8f720d 100644
--- a/httemplate/elements/freeside.css
+++ b/httemplate/elements/freeside.css
@@ -335,3 +335,11 @@ div.package-marker-change_from {
border-left: solid #bbffbb 30px;
display: inline-block;
}
+
+/* elements/location.html and co. */
+fieldset.location {
+ padding: 0px;
+ margin: 0px;
+ border: none;
+}
+
diff --git a/httemplate/elements/jquery.deserialize.min.js b/httemplate/elements/jquery.deserialize.min.js
new file mode 100644
index 000000000..7054ea4bb
--- /dev/null
+++ b/httemplate/elements/jquery.deserialize.min.js
@@ -0,0 +1,8 @@
+/**
+ * @author Kyle Florence <kyle[dot]florence[at]gmail[dot]com>
+ * @website https://github.com/kflorence/jquery-deserialize/
+ * @version 1.2.1
+ *
+ * Dual licensed under the MIT and GPLv2 licenses.
+ */
+(function(i,b){var f=Array.prototype.push,a=/^(?:radio|checkbox)$/i,e=/\+/g,d=/^(?:option|select-one|select-multiple)$/i,g=/^(?:button|color|date|datetime|datetime-local|email|hidden|month|number|password|range|reset|search|submit|tel|text|textarea|time|url|week)$/i;function c(j){return j.map(function(){return this.elements?i.makeArray(this.elements):this}).filter(":input").get()}function h(j){var k,l={};i.each(j,function(n,m){k=l[m.name];l[m.name]=k===b?m:(i.isArray(k)?k.concat(m):[k,m])});return l}i.fn.deserialize=function(A,l){var y,n,q=c(this),t=[];if(!A||!q.length){return this}if(i.isArray(A)){t=A}else{if(i.isPlainObject(A)){var B,w;for(B in A){i.isArray(w=A[B])?f.apply(t,i.map(w,function(j){return{name:B,value:j}})):f.call(t,{name:B,value:w})}}else{if(typeof A==="string"){var v;A=A.split("&");for(y=0,n=A.length;y<n;y++){v=A[y].split("=");f.call(t,{name:decodeURIComponent(v[0]),value:decodeURIComponent(v[1].replace(e,"%20"))})}}}}if(!(n=t.length)){return this}var u,k,x,z,C,o,m,w,p=i.noop,s=i.noop,r={};l=l||{};q=h(q);if(i.isFunction(l)){s=l}else{p=i.isFunction(l.change)?l.change:p;s=i.isFunction(l.complete)?l.complete:s}for(y=0;y<n;y++){u=t[y];C=u.name;w=u.value;if(!(k=q[C])){continue}m=(z=k.length)?k[0]:k;m=(m.type||m.nodeName).toLowerCase();o=null;if(g.test(m)){if(z){x=r[C];k=k[r[C]=(x==b)?0:++x]}p.call(k,(k.value=w))}else{if(a.test(m)){o="checked"}else{if(d.test(m)){o="selected"}}}if(o){if(!z){k=[k];z=1}for(x=0;x<z;x++){u=k[x];if(u.value==w){p.call(u,(u[o]=true)&&w)}}}}s.call(this);return this}})(jQuery);
diff --git a/httemplate/elements/location.html b/httemplate/elements/location.html
index b5f0a963c..d4c0f14d4 100644
--- a/httemplate/elements/location.html
+++ b/httemplate/elements/location.html
@@ -19,6 +19,9 @@ Example:
</%doc>
+<SCRIPT SRC="<% $fsurl %>elements/jquery.deserialize.min.js"></SCRIPT>
+<SCRIPT SRC="<% $fsurl %>elements/polyfill.js"></SCRIPT>
+
% if ( $opt{'alt_format'} ) {
<TR>
@@ -324,6 +327,89 @@ Example:
}
</&>
+
+function Location(fieldset) {
+ if ( typeof fieldset == 'String' ) {
+ fieldset = $('#' + fieldset);
+ }
+ this.fieldset = $(fieldset);
+ var errorbox = document.createElement('DIV');
+ errorbox.className = 'error';
+ fieldset.append(errorbox); // after the <table>
+ $(errorbox).position({
+ my: 'left',
+ at: 'right+20px',
+ of: fieldset
+ });
+ this.errorbox = $(errorbox); // so we can find it
+
+ var img_tick = $('<IMG SRC="http://localhost/freeside/images/tick.png">');
+ var img_wait = $('<IMG SRC="http://localhost/freeside/images/wait-orange.gif">');
+
+ // get/set the serialized (URL parameter string) contents of the form fields
+ this.value = function(newvalue) {
+ if (newvalue) {
+ try {
+ this.fieldset.deserialize(newvalue);
+ this.errorbox.empty();
+ if ( newvalue['error'] ) {
+ this.errorbox.text(newvalue['error']);
+ } else {
+ this.errorbox.append(img_tick);
+ }
+ } catch(err) {
+ console.log("Couldn't parse returned data:\n" + newvalue);
+ // show an error also
+ }
+ }
+ return this.fieldset.serialize();
+ };
+
+ // send a standardization request and do something with the result
+ this.standardize = function(callback) {
+ this.errorbox.empty();
+ this.errorbox.append(img_wait);
+ $.ajax({
+ type: 'POST',
+ url: '<% $fsurl %>misc/address_standardize.cgi',
+ success: callback,
+ data: this.value()
+ });
+ };
+
+ // check if required fields are filled, and if so, standardize
+ var standardize_if_ready = function() {
+ var loc = this;
+ var ready = true;
+ var required_fields = this.fieldset.find(':data(required)');
+ for ( var i = 0; ready && i < required_fields.length; i++ ) {
+ if ( required_fields[i].prop('value').length == 0 ) {
+ ready = false;
+ }
+ }
+
+ if ( ready ) {
+ // pass the "value" method, prebound to the location object
+ this.standardize( this.value.bind(loc) );
+ }
+ };
+
+ // event handler; the Location object is passed in event.data
+ var location_change_timer;
+ var location_changed = function( ev ) {
+ if ( location_change_timer ) {
+ window.clearTimeout(location_change_timer);
+ }
+ location_change_timer = window.setTimeout(
+ standardize_if_ready.bind(ev.data),
+ 2000
+ );
+ };
+
+ fieldset.find('input').on('change', this, location_changed);
+ fieldset.find('select').on('change', this, location_changed);
+}
+
</SCRIPT>
<%init>
diff --git a/httemplate/elements/polyfill.js b/httemplate/elements/polyfill.js
new file mode 100644
index 000000000..5e08a9933
--- /dev/null
+++ b/httemplate/elements/polyfill.js
@@ -0,0 +1,30 @@
+// Function.bind(), not supported in IE8
+// polyfill from Mozilla Developer Network
+
+if (!Function.prototype.bind) {
+ Function.prototype.bind = function(oThis) {
+ if (typeof this !== 'function') {
+ // closest thing possible to the ECMAScript 5
+ // internal IsCallable function
+ throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
+ }
+
+ var aArgs = Array.prototype.slice.call(arguments, 1),
+ fToBind = this,
+ fNOP = function() {},
+ fBound = function() {
+ return fToBind.apply(this instanceof fNOP
+ ? this
+ : oThis,
+ aArgs.concat(Array.prototype.slice.call(arguments)));
+ };
+
+ if (this.prototype) {
+ // native functions don't have a prototype
+ fNOP.prototype = this.prototype;
+ }
+ fBound.prototype = new fNOP();
+
+ return fBound;
+ };
+}