Skip to content

Commit

Permalink
Merge pull request #42 from sorvell/master
Browse files Browse the repository at this point in the history
update g-ajax and minor g-panels and g-page fixes
  • Loading branch information
frankiefu committed Nov 21, 2012
2 parents 6979594 + c34d8a0 commit 6885c56
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 99 deletions.
111 changes: 57 additions & 54 deletions src/g-ajax.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,68 @@
</template>
<script>
this.component({
prototype: {
ready: function() {
// wait until ready to auto go only 1x.
this.readied = true;
this.asyncMethod('autoChanged');
},
receive: function(inResponse, inXhr) {
if (this.isSuccess(inXhr)) {
this.response(inXhr);
} else {
this.error(inXhr);
}
this.complete(inXhr);
},
isSuccess: function(inXhr) {
var status = inXhr.status || 0;
return !status || (status >= 200 && status < 300);
},
response: function(inXhr) {
var response = this.evalResponse(inXhr);
this.fireEvent('response', {response: response, xhr: inXhr});
},
error: function(inXhr) {
var response = inXhr.status + ': ' + inXhr.responseText;
this.fireEvent('error', {response: response, xhr: inXhr});
},
complete: function(inXhr) {
this.fireEvent('complete', {response: inXhr.status, xhr: inXhr});
},
evalResponse: function(inXhr) {
return this[(this.handleAs || 'text') + 'Handler'](inXhr);
},
xmlHandler: function(inXhr) {
return inXhr.responseXML;
},
textHandler: function(inXhr) {
return inXhr.responseText;
},
jsonHandler: function(inXhr) {
var r = this.textHandler(inXhr);
try {
return JSON.parse(r);
} catch (x) {
return r;
}
},
autoChanged: function() {
if (this.auto && this.readied) {
this.go();
}
},
paramsChanged: function() {
if (this.auto && this.readied) {
this.go();
}
},
publish: {
/**
* Performs an Ajax request to the url specified.
*/
go: function() {
var params = JSON.parse(this.params);
var params = this.params ? JSON.parse(this.params) : null;
return this.$.xhr.request({url: this.url, callback: this.receive.bind(this), params: params});
},
receive: function(inResponse, inXhr) {
if (this.isSuccess(inXhr)) {
this.response(inXhr);
} else {
this.error(inXhr);
}
this.complete(inXhr);
},
isSuccess: function(inXhr) {
var status = inXhr.status || 0;
return !status || (status >= 200 && status < 300);
},
dispatchAjaxEvent: function(inType, inResponse, inXhr) {
this.dispatchEvent(new CustomEvent(inType,
{detail: {response: inResponse, xhr: inXhr}}));
},
response: function(inXhr) {
var response = this.evalResponse(inXhr);
this.dispatchAjaxEvent('response', response, inXhr);
// Here we want to expose the response as a model so that it can be
// consumed by other components.
this.model = this.model || {};
this.model.response = response;
},
error: function(inXhr) {
this.dispatchAjaxEvent('error', inXhr.status + ': ' + inXhr.responseText, inXhr);
},
complete: function(inXhr) {
this.dispatchAjaxEvent('complete', inXhr.status, inXhr);
},
evalResponse: function(inXhr) {
return this[(this.handleAs || 'text') + 'Handler'](inXhr);
},
xmlHandler: function(inXhr) {
return inXhr.responseXML;
},
textHandler: function(inXhr) {
return inXhr.responseText;
},
jsonHandler: function(inXhr) {
var r = this.textHandler(inXhr);
try {
return JSON.parse(r);
} catch (x) {
return r;
}
},
paramsChanged: function() {
if (this.auto) {
this.go();
}
}
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/g-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
sheet.setAttribute('g-page', '');
sheet.removeAttribute('scoped');
sheet.removeAttribute('id')
document.head.appendChild(sheet);
var head = document.head;
head.insertBefore(sheet, head.firstChild);
}
sheet.textContent += '\n' + this.node.tagName.toLowerCase() + ' {height: 100%;}\n';
}
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion src/g-panels.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
ready: function() {
// ensure default transition
if (!this.transition) {
this.transition = this.defaultTransition;
this.node.transition = this.defaultTransition;
if (this.index != null) {
this.node.index = 0;
}
Expand Down
72 changes: 36 additions & 36 deletions src/g-xhr.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,42 @@
</template>
<script>
this.component({
prototype: {
getTransport: function() {
try {
return new XMLHttpRequest();
} catch (e) {}
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {}
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
return false;
},
makeReadyStateHandler: function(inXhr, inCallback) {
inXhr.onreadystatechange = function() {
if (inXhr.readyState == 4) {
inCallback && inCallback.apply(null, [inXhr.responseText, inXhr]);
}
};
},
setRequestHeaders: function(inXhr, inHeaders) {
if (inHeaders) {
for (var name in inHeaders) {
xhr.setRequestHeader(name, inHeaders[name]);
}
}
},
toQueryString: function(inParams) {
var r = [];
for (var n in inParams) {
var v = inParams[n];
n = encodeURIComponent(n);
r.push(v === undefined || v === null ? n : (n + '=' + encodeURIComponent(v)));
}
return r.join('&');
},
publish: {
/**
* Sends a HTTP request to the server and returns the XHR object.
*
Expand Down Expand Up @@ -48,41 +83,6 @@
transport.onreadystatechange(transport);
}
return transport;
},
getTransport: function() {
try {
return new XMLHttpRequest();
} catch (e) {}
try {
return new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {}
try {
return new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {}
return false;
},
makeReadyStateHandler: function(inXhr, inCallback) {
inXhr.onreadystatechange = function() {
if (inXhr.readyState == 4) {
inCallback && inCallback.apply(null, [inXhr.responseText, inXhr]);
}
};
},
setRequestHeaders: function(inXhr, inHeaders) {
if (inHeaders) {
for (var name in inHeaders) {
xhr.setRequestHeader(name, inHeaders[name]);
}
}
},
toQueryString: function(inParams) {
var r = [];
for (var n in inParams) {
var v = inParams[n];
n = encodeURIComponent(n);
r.push(v === undefined || v === null ? n : (n + '=' + encodeURIComponent(v)));
}
return r.join('&');
}
}
});
Expand Down
10 changes: 3 additions & 7 deletions src/panel-transitions/g-flow-panel-transition.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<element name="g-flow-panel-transition" extends="g-panel-transition">
<template>
<style scoped id="flowsheet">
/* TODO(sorvell): replace when reference combinators arrive */
g-panels[transition='flow'] > .right {
left: auto;
right: 0;
Expand All @@ -33,17 +32,14 @@
setup: function(inPanels) {
this.$super(arguments);
this.listenForMediaMatch();
// TODO(sorvell): revert when reference combinators arrive
//this.panels.node.appendChild(this.$.flowsheet);
document.head.appendChild(this.$.flowsheet);
// TODO(sorvell): redo when reference combinators arrive
this.panels.node.appendChild(this.$.flowsheet);
// TODO(sorvell): hack, must fix better
// async refresh in case we need to wait for stylesheet to load
this.asyncMethod('refresh');
},
teardown: function() {
// TODO(sorvell): revert when reference combinators arrive
//this.panels.node.removeChild(this.$.flowsheet);
document.head.removeChild(this.$.flowsheet);
this.panels.node.removeChild(this.$.flowsheet);
this.$super();
},
listenForMediaMatch: function() {
Expand Down

0 comments on commit 6885c56

Please sign in to comment.