start replacing jquery ajax with fetch

This commit is contained in:
Luke Pulverenti 2015-11-22 13:17:58 -05:00
parent ffb022d13c
commit 62b528a41c
20 changed files with 547 additions and 69 deletions

View File

@ -0,0 +1,28 @@
{
"name": "fetch",
"main": "fetch.js",
"devDependencies": {
"es6-promise": "1.0.0"
},
"ignore": [
".*",
"*.md",
"examples/",
"Makefile",
"package.json",
"script/",
"test/"
],
"homepage": "https://github.com/github/fetch",
"version": "0.10.1",
"_release": "0.10.1",
"_resolution": {
"type": "version",
"tag": "v0.10.1",
"commit": "f4f8ca8d0ba6c7d11e5317a84189913cefe55809"
},
"_source": "git://github.com/github/fetch.git",
"_target": "~0.10.1",
"_originalSource": "fetch",
"_direct": true
}

View File

@ -0,0 +1,20 @@
Copyright (c) 2014-2015 GitHub, Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,16 @@
{
"name": "fetch",
"main": "fetch.js",
"devDependencies": {
"es6-promise": "1.0.0"
},
"ignore": [
".*",
"*.md",
"examples/",
"Makefile",
"package.json",
"script/",
"test/"
]
}

View File

@ -0,0 +1,381 @@
(function() {
'use strict';
if (self.fetch) {
return
}
function normalizeName(name) {
if (typeof name !== 'string') {
name = String(name)
}
if (/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(name)) {
throw new TypeError('Invalid character in header field name')
}
return name.toLowerCase()
}
function normalizeValue(value) {
if (typeof value !== 'string') {
value = String(value)
}
return value
}
function Headers(headers) {
this.map = {}
if (headers instanceof Headers) {
headers.forEach(function(value, name) {
this.append(name, value)
}, this)
} else if (headers) {
Object.getOwnPropertyNames(headers).forEach(function(name) {
this.append(name, headers[name])
}, this)
}
}
Headers.prototype.append = function(name, value) {
name = normalizeName(name)
value = normalizeValue(value)
var list = this.map[name]
if (!list) {
list = []
this.map[name] = list
}
list.push(value)
}
Headers.prototype['delete'] = function(name) {
delete this.map[normalizeName(name)]
}
Headers.prototype.get = function(name) {
var values = this.map[normalizeName(name)]
return values ? values[0] : null
}
Headers.prototype.getAll = function(name) {
return this.map[normalizeName(name)] || []
}
Headers.prototype.has = function(name) {
return this.map.hasOwnProperty(normalizeName(name))
}
Headers.prototype.set = function(name, value) {
this.map[normalizeName(name)] = [normalizeValue(value)]
}
Headers.prototype.forEach = function(callback, thisArg) {
Object.getOwnPropertyNames(this.map).forEach(function(name) {
this.map[name].forEach(function(value) {
callback.call(thisArg, value, name, this)
}, this)
}, this)
}
function consumed(body) {
if (body.bodyUsed) {
return Promise.reject(new TypeError('Already read'))
}
body.bodyUsed = true
}
function fileReaderReady(reader) {
return new Promise(function(resolve, reject) {
reader.onload = function() {
resolve(reader.result)
}
reader.onerror = function() {
reject(reader.error)
}
})
}
function readBlobAsArrayBuffer(blob) {
var reader = new FileReader()
reader.readAsArrayBuffer(blob)
return fileReaderReady(reader)
}
function readBlobAsText(blob) {
var reader = new FileReader()
reader.readAsText(blob)
return fileReaderReady(reader)
}
var support = {
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
new Blob();
return true
} catch(e) {
return false
}
})(),
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
}
function Body() {
this.bodyUsed = false
this._initBody = function(body) {
this._bodyInit = body
if (typeof body === 'string') {
this._bodyText = body
} else if (support.blob && Blob.prototype.isPrototypeOf(body)) {
this._bodyBlob = body
} else if (support.formData && FormData.prototype.isPrototypeOf(body)) {
this._bodyFormData = body
} else if (!body) {
this._bodyText = ''
} else if (support.arrayBuffer && ArrayBuffer.prototype.isPrototypeOf(body)) {
// Only support ArrayBuffers for POST method.
// Receiving ArrayBuffers happens via Blobs, instead.
} else {
throw new Error('unsupported BodyInit type')
}
}
if (support.blob) {
this.blob = function() {
var rejected = consumed(this)
if (rejected) {
return rejected
}
if (this._bodyBlob) {
return Promise.resolve(this._bodyBlob)
} else if (this._bodyFormData) {
throw new Error('could not read FormData body as blob')
} else {
return Promise.resolve(new Blob([this._bodyText]))
}
}
this.arrayBuffer = function() {
return this.blob().then(readBlobAsArrayBuffer)
}
this.text = function() {
var rejected = consumed(this)
if (rejected) {
return rejected
}
if (this._bodyBlob) {
return readBlobAsText(this._bodyBlob)
} else if (this._bodyFormData) {
throw new Error('could not read FormData body as text')
} else {
return Promise.resolve(this._bodyText)
}
}
} else {
this.text = function() {
var rejected = consumed(this)
return rejected ? rejected : Promise.resolve(this._bodyText)
}
}
if (support.formData) {
this.formData = function() {
return this.text().then(decode)
}
}
this.json = function() {
return this.text().then(JSON.parse)
}
return this
}
// HTTP methods whose capitalization should be normalized
var methods = ['DELETE', 'GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']
function normalizeMethod(method) {
var upcased = method.toUpperCase()
return (methods.indexOf(upcased) > -1) ? upcased : method
}
function Request(input, options) {
options = options || {}
var body = options.body
if (Request.prototype.isPrototypeOf(input)) {
if (input.bodyUsed) {
throw new TypeError('Already read')
}
this.url = input.url
this.credentials = input.credentials
if (!options.headers) {
this.headers = new Headers(input.headers)
}
this.method = input.method
this.mode = input.mode
if (!body) {
body = input._bodyInit
input.bodyUsed = true
}
} else {
this.url = input
}
this.credentials = options.credentials || this.credentials || 'omit'
if (options.headers || !this.headers) {
this.headers = new Headers(options.headers)
}
this.method = normalizeMethod(options.method || this.method || 'GET')
this.mode = options.mode || this.mode || null
this.referrer = null
if ((this.method === 'GET' || this.method === 'HEAD') && body) {
throw new TypeError('Body not allowed for GET or HEAD requests')
}
this._initBody(body)
}
Request.prototype.clone = function() {
return new Request(this)
}
function decode(body) {
var form = new FormData()
body.trim().split('&').forEach(function(bytes) {
if (bytes) {
var split = bytes.split('=')
var name = split.shift().replace(/\+/g, ' ')
var value = split.join('=').replace(/\+/g, ' ')
form.append(decodeURIComponent(name), decodeURIComponent(value))
}
})
return form
}
function headers(xhr) {
var head = new Headers()
var pairs = xhr.getAllResponseHeaders().trim().split('\n')
pairs.forEach(function(header) {
var split = header.trim().split(':')
var key = split.shift().trim()
var value = split.join(':').trim()
head.append(key, value)
})
return head
}
Body.call(Request.prototype)
function Response(bodyInit, options) {
if (!options) {
options = {}
}
this._initBody(bodyInit)
this.type = 'default'
this.status = options.status
this.ok = this.status >= 200 && this.status < 300
this.statusText = options.statusText
this.headers = options.headers instanceof Headers ? options.headers : new Headers(options.headers)
this.url = options.url || ''
}
Body.call(Response.prototype)
Response.prototype.clone = function() {
return new Response(this._bodyInit, {
status: this.status,
statusText: this.statusText,
headers: new Headers(this.headers),
url: this.url
})
}
Response.error = function() {
var response = new Response(null, {status: 0, statusText: ''})
response.type = 'error'
return response
}
var redirectStatuses = [301, 302, 303, 307, 308]
Response.redirect = function(url, status) {
if (redirectStatuses.indexOf(status) === -1) {
throw new RangeError('Invalid status code')
}
return new Response(null, {status: status, headers: {location: url}})
}
self.Headers = Headers;
self.Request = Request;
self.Response = Response;
self.fetch = function(input, init) {
return new Promise(function(resolve, reject) {
var request
if (Request.prototype.isPrototypeOf(input) && !init) {
request = input
} else {
request = new Request(input, init)
}
var xhr = new XMLHttpRequest()
function responseURL() {
if ('responseURL' in xhr) {
return xhr.responseURL
}
// Avoid security warnings on getResponseHeader when not allowed by CORS
if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {
return xhr.getResponseHeader('X-Request-URL')
}
return;
}
xhr.onload = function() {
var status = (xhr.status === 1223) ? 204 : xhr.status
if (status < 100 || status > 599) {
reject(new TypeError('Network request failed'))
return
}
var options = {
status: status,
statusText: xhr.statusText,
headers: headers(xhr),
url: responseURL()
}
var body = 'response' in xhr ? xhr.response : xhr.responseText;
resolve(new Response(body, options))
}
xhr.onerror = function() {
reject(new TypeError('Network request failed'))
}
xhr.open(request.method, request.url, true)
if (request.credentials === 'include') {
xhr.withCredentials = true
}
if ('responseType' in xhr && support.blob) {
xhr.responseType = 'blob'
}
request.headers.forEach(function(value, name) {
xhr.setRequestHeader(name, value)
})
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
})
}
self.fetch.polyfill = true
})();

View File

@ -31,6 +31,6 @@
"commit": "34fc5e4a0f252964ed2790138b8d7d30d04b55c1"
},
"_source": "git://github.com/desandro/get-style-property.git",
"_target": "1.x",
"_target": "~1.0.4",
"_originalSource": "get-style-property"
}

View File

@ -26,14 +26,14 @@
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
},
"main": "iron-meta.html",
"homepage": "https://github.com/polymerelements/iron-meta",
"homepage": "https://github.com/PolymerElements/iron-meta",
"_release": "1.1.1",
"_resolution": {
"type": "version",
"tag": "v1.1.1",
"commit": "e171ee234b482219c9514e6f9551df48ef48bd9f"
},
"_source": "git://github.com/polymerelements/iron-meta.git",
"_source": "git://github.com/PolymerElements/iron-meta.git",
"_target": "^1.0.0",
"_originalSource": "polymerelements/iron-meta"
"_originalSource": "PolymerElements/iron-meta"
}

View File

@ -33,14 +33,14 @@
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
},
"ignore": [],
"homepage": "https://github.com/PolymerElements/iron-overlay-behavior",
"homepage": "https://github.com/polymerelements/iron-overlay-behavior",
"_release": "1.1.1",
"_resolution": {
"type": "version",
"tag": "v1.1.1",
"commit": "1ed1603ce820456feab3f62ae86f8f3ec801d131"
},
"_source": "git://github.com/PolymerElements/iron-overlay-behavior.git",
"_source": "git://github.com/polymerelements/iron-overlay-behavior.git",
"_target": "^1.0.0",
"_originalSource": "PolymerElements/iron-overlay-behavior"
"_originalSource": "polymerelements/iron-overlay-behavior"
}

View File

@ -27,14 +27,14 @@
"web-component-tester": "*",
"webcomponentsjs": "webcomponents/webcomponentsjs#^0.7.0"
},
"homepage": "https://github.com/PolymerElements/iron-resizable-behavior",
"homepage": "https://github.com/polymerelements/iron-resizable-behavior",
"_release": "1.0.2",
"_resolution": {
"type": "version",
"tag": "v1.0.2",
"commit": "85de8ba28be2bf17c81d6436ef1119022b003674"
},
"_source": "git://github.com/PolymerElements/iron-resizable-behavior.git",
"_source": "git://github.com/polymerelements/iron-resizable-behavior.git",
"_target": "^1.0.0",
"_originalSource": "PolymerElements/iron-resizable-behavior"
"_originalSource": "polymerelements/iron-resizable-behavior"
}

View File

@ -54,7 +54,7 @@
"tag": "v1.0.8",
"commit": "36656916b75a4715b025a03473620002c2650ee8"
},
"_source": "git://github.com/PolymerElements/neon-animation.git",
"_source": "git://github.com/polymerelements/neon-animation.git",
"_target": "^1.0.0",
"_originalSource": "PolymerElements/neon-animation"
"_originalSource": "polymerelements/neon-animation"
}

View File

@ -45,7 +45,7 @@
"tag": "v1.0.10",
"commit": "4b244a542af2c6c271498dfb98b00ed284df1d6a"
},
"_source": "git://github.com/polymerelements/paper-behaviors.git",
"_source": "git://github.com/PolymerElements/paper-behaviors.git",
"_target": "^1.0.0",
"_originalSource": "polymerelements/paper-behaviors"
"_originalSource": "PolymerElements/paper-behaviors"
}

View File

@ -50,7 +50,7 @@
"tag": "v1.1.1",
"commit": "1bbce220b027dc030b294163f7da6f3e9052ab13"
},
"_source": "git://github.com/PolymerElements/paper-input.git",
"_target": "^1.0.0",
"_originalSource": "PolymerElements/paper-input"
"_source": "git://github.com/polymerelements/paper-input.git",
"_target": "^1.0.9",
"_originalSource": "polymerelements/paper-input"
}

View File

@ -32,14 +32,14 @@
"iron-test-helpers": "PolymerElements/iron-test-helpers#^1.0.0"
},
"ignore": [],
"homepage": "https://github.com/polymerelements/paper-ripple",
"homepage": "https://github.com/PolymerElements/paper-ripple",
"_release": "1.0.5",
"_resolution": {
"type": "version",
"tag": "v1.0.5",
"commit": "d72e7a9a8ab518b901ed18dde492df3b87a93be5"
},
"_source": "git://github.com/polymerelements/paper-ripple.git",
"_source": "git://github.com/PolymerElements/paper-ripple.git",
"_target": "^1.0.0",
"_originalSource": "polymerelements/paper-ripple"
"_originalSource": "PolymerElements/paper-ripple"
}

View File

@ -87,7 +87,7 @@
if (this.status == 200) {
writeData(dir, filename, this.getResponseHeader('Content-Type'), this.response, callback, errorCallback);
} else {
errorCallback();
errorCallback('');
}
}

View File

@ -522,7 +522,7 @@
}
}
@media all and (min-width: 1920px) {
@media all and (min-width: 2100px) {
.squareCard {
width: 11.111111111111111111111111111111%;

View File

@ -144,6 +144,13 @@
}
}
@media all and (min-width: 2560px) {
.ehsContent:not(.fullWidth), .ehsContent .pageTabContent {
width: 96%;
}
}
.detailPageContent, .detailPageContent > table {
margin: 0 auto;
padding: 0;

View File

@ -3,7 +3,7 @@
}
.viewMenuBar.semiTransparent {
background-color: rgba(15, 15, 15, .40);
background-color: rgba(15, 15, 15, .35);
}
.background-theme-b, paper-dialog.background-theme-b {

View File

@ -19,33 +19,45 @@
function loadDictionary(name, culture) {
var deferred = DeferredBuilder.Deferred();
return new Promise(function (resolve, reject) {
if (getDictionary(name, culture)) {
deferred.resolve();
} else {
if (getDictionary(name, culture)) {
resolve();
return;
}
var url = getUrl(name, culture);
var requestUrl = url + "?v=" + window.dashboardVersion;
$.getJSON(requestUrl).done(function (dictionary) {
fetch(requestUrl, { mode: 'no-cors' }).then(function (response) {
dictionaries[url] = dictionary;
deferred.resolve();
if (response.status < 400) {
}).fail(function () {
return response.json();
// If there's no dictionary for that language, grab English
$.getJSON(getUrl(name, 'en-US')).done(function (dictionary) {
} else {
dictionaries[url] = dictionary;
deferred.resolve();
// Grab the english version
fetch(getUrl(name, 'en-US'), { mode: 'no-cors' }).then(function (response) {
});
return response.json();
}).then(function (json) {
dictionaries[url] = json;
resolve();
});
}
}).then(function (json) {
if (json) {
dictionaries[url] = json;
resolve();
}
});
}
return deferred.promise();
});
}
var currentCulture = 'en-US';
@ -55,7 +67,7 @@
currentCulture = value;
return $.when(loadDictionary('html', value), loadDictionary('javascript', value));
return Promise.all([loadDictionary('html', value), loadDictionary('javascript', value)]);
}
function normalizeLocaleName(culture) {
@ -74,25 +86,25 @@
}
function getDeviceCulture() {
var deferred = DeferredBuilder.Deferred();
if (AppInfo.isNativeApp) {
return new Promise(function (resolve, reject) {
deferred.resolveWith(null, [navigator.language || navigator.userLanguage]);
if (AppInfo.isNativeApp) {
} else if (AppInfo.supportsUserDisplayLanguageSetting) {
resolve(navigator.language || navigator.userLanguage);
Logger.log('AppInfo.supportsUserDisplayLanguageSetting is true');
} else if (AppInfo.supportsUserDisplayLanguageSetting) {
deferred.resolveWith(null, [AppSettings.displayLanguage()]);
Logger.log('AppInfo.supportsUserDisplayLanguageSetting is true');
} else {
resolve(AppSettings.displayLanguage());
Logger.log('Getting culture from document');
deferred.resolveWith(null, [document.documentElement.getAttribute('data-culture')]);
}
} else {
return deferred.promise();
Logger.log('Getting culture from document');
resolve(document.documentElement.getAttribute('data-culture'));
}
});
}
@ -100,18 +112,15 @@
Logger.log('Entering Globalize.ensure');
var deferred = DeferredBuilder.Deferred();
return new Promise(function (resolve, reject) {
getDeviceCulture().done(function (culture) {
getDeviceCulture().then(function (culture) {
culture = normalizeLocaleName(culture || 'en-US');
culture = normalizeLocaleName(culture || 'en-US');
setCulture(culture).done(function () {
deferred.resolve();
setCulture(culture).then(resolve);
});
});
return deferred.promise();
}
function translateDocument(html, dictionaryName) {

View File

@ -224,7 +224,12 @@
var url = "http://mb3admin.com/admin/service/user/getPayPalEmail?id=" + pkg.owner;
$.getJSON(url).done(function (dev) {
fetch(url, { mode: 'no-cors' }).then(function (response) {
return response.json();
}).then(function (dev) {
if (dev.payPalEmail) {
$('#payPalEmail', page).val(dev.payPalEmail);
@ -232,6 +237,7 @@
$('#ppButton', page).hide();
}
});
} else {
// Supporter-only feature
$('.premiumHasPrice', page).hide();

View File

@ -2265,6 +2265,10 @@ var AppInfo = {};
deps.push('bower_components/native-promise-only/lib/npo.src');
}
if (!window.fetch) {
deps.push('bower_components/fetch/fetch');
}
require(deps, function () {
loadImageCache();
@ -2330,7 +2334,7 @@ var AppInfo = {};
function onConnectionManagerCreated(deferred) {
Globalize.ensure().done(function () {
Globalize.ensure().then(function () {
document.title = Globalize.translateDocument(document.title, 'html');
$(function () {

View File

@ -3113,16 +3113,23 @@
return deferred.promise();
}
// Load the new content.
$.ajax({
url: fileUrl,
type: settings.type,
data: settings.data,
contentType: settings.contentType,
dataType: "html",
success: successFn,
error: this._loadError(absUrl, triggerData, settings, deferred)
});
//// Load the new content.
//$.ajax({
// url: fileUrl,
// type: settings.type,
// data: settings.data,
// contentType: settings.contentType,
// dataType: "html",
// success: successFn,
// error: this._loadError(absUrl, triggerData, settings, deferred)
//});
fetch(fileUrl, {
mode: 'no-cors'
}).then(function (response) {
return response.text();
}).then(successFn);
return deferred.promise();
},