diff options
Diffstat (limited to 'public/assets/js')
| -rw-r--r-- | public/assets/js/lib/sdk/auth.js | 7 | ||||
| -rw-r--r-- | public/assets/js/lib/views/index/hootbox.js | 2 | ||||
| -rw-r--r-- | public/assets/js/lib/views/profile/profile_edit.js | 16 | ||||
| -rw-r--r-- | public/assets/js/vendor/dataUriToBlob.js | 58 | ||||
| -rw-r--r-- | public/assets/js/vendor/view/formview.js | 96 |
5 files changed, 129 insertions, 50 deletions
diff --git a/public/assets/js/lib/sdk/auth.js b/public/assets/js/lib/sdk/auth.js index c720292..0b49449 100644 --- a/public/assets/js/lib/sdk/auth.js +++ b/public/assets/js/lib/sdk/auth.js @@ -70,6 +70,7 @@ var auth = (function(){ $.ajax({ method: 'put', url: '/api/checkin', + headers: { "csrf-token": $("[name=_csrf]").attr("value") }, success: function(data){ if (data && data.user && data.user.id !== -1) { auth.set_user(data.user) @@ -77,7 +78,11 @@ var auth = (function(){ return } opt.error() - } + }, + error: function(){ + window.location.href = '/login' + opt.error() + }, }) } diff --git a/public/assets/js/lib/views/index/hootbox.js b/public/assets/js/lib/views/index/hootbox.js index 85fd51e..2338153 100644 --- a/public/assets/js/lib/views/index/hootbox.js +++ b/public/assets/js/lib/views/index/hootbox.js @@ -23,7 +23,7 @@ var HootBox = FormView.extend({ }, parse: function(comment){ - var t = this.template.replace(/{{username}}/g, profile_image(comment.username)) + var t = this.template.replace(/{{image}}/g, profile_image(comment.username)) .replace(/{{username}}/g, comment.username) .replace(/{{comment}}/g, tidy_urls(comment.comment, true)) return t diff --git a/public/assets/js/lib/views/profile/profile_edit.js b/public/assets/js/lib/views/profile/profile_edit.js index a887357..e50a7c0 100644 --- a/public/assets/js/lib/views/profile/profile_edit.js +++ b/public/assets/js/lib/views/profile/profile_edit.js @@ -64,6 +64,7 @@ var ProfileForm = FormView.extend({ canvas.height = h ctx.drawImage(img, 0, 0, w, h) var dataURI = canvas.toDataURL('image/jpeg', 0.85) + this.avatarBlob = dataUriToBlob(dataURI) this.$("#profile-avatar-embed").show().attr("src", dataURI).css("width", w/2) }, @@ -71,6 +72,18 @@ var ProfileForm = FormView.extend({ this.$(".oldpassword").css('display', 'flex') }, + serialize: function(){ + var fd = this.__super__.serialize.call(this) + var oldpw = this.$("[name=oldpassword]").val() + var pw = this.$("[name=newpassword]").val() + var pw2 = this.$("[name=newpassword2]").val() + fd.delete('avatar') + if (this.avatarBlob) { + fd.append("avatar", this.avatarBlob) + } + return fd + }, + validate: function(){ var errors = [] var oldpw = this.$("[name=oldpassword]").val() @@ -89,6 +102,7 @@ var ProfileForm = FormView.extend({ if (data.error) { return alert(data.error) } - window.location.href = "/details/" + data.id + auth.set_user(data.user) + window.location.href = "/profile" } })
\ No newline at end of file diff --git a/public/assets/js/vendor/dataUriToBlob.js b/public/assets/js/vendor/dataUriToBlob.js new file mode 100644 index 0000000..80189b8 --- /dev/null +++ b/public/assets/js/vendor/dataUriToBlob.js @@ -0,0 +1,58 @@ +var dataUriToUint8Array = function(uri){ + var data = uri.split(',')[1]; + var bytes = atob(data); + var buf = new ArrayBuffer(bytes.length); + var u8 = new Uint8Array(buf); + for (var i = 0; i < bytes.length; i++) { + u8[i] = bytes.charCodeAt(i); + } + return u8 +} + +window.dataUriToBlob = (function(){ +/** + * Blob constructor. + */ + +var Blob = window.Blob; + +/** + * ArrayBufferView support. + */ + +var hasArrayBufferView = new Blob([new Uint8Array(100)]).size == 100; + +/** + * Return a `Blob` for the given data `uri`. + * + * @param {String} uri + * @return {Blob} + * @api public + */ + +var dataUriToBlob = function(uri){ + var data = uri.split(',')[1]; + var bytes = atob(data); + var buf = new ArrayBuffer(bytes.length); + var arr = new Uint8Array(buf); + for (var i = 0; i < bytes.length; i++) { + arr[i] = bytes.charCodeAt(i); + } + + if (!hasArrayBufferView) arr = buf; + var blob = new Blob([arr], { type: mime(uri) }); + blob.slice = blob.slice || blob.webkitSlice; + return blob; +}; + +/** + * Return data uri mime type. + */ + +function mime(uri) { + return uri.split(';')[0].slice(5); +} + +return dataUriToBlob; + +})() diff --git a/public/assets/js/vendor/view/formview.js b/public/assets/js/vendor/view/formview.js index 6b03849..923e212 100644 --- a/public/assets/js/vendor/view/formview.js +++ b/public/assets/js/vendor/view/formview.js @@ -90,44 +90,28 @@ var FormView = View.extend({ var action = typeof this.action == "function" ? this.action() : this.action if (! action) return - var data = this.serialize() - var request = $.ajax({ - url: action, - type: this.method, - data: data, - headers: { "csrf-token": $("[name=_csrf]").attr("value") }, - contentType: data instanceof FormData ? false : "application/json", - dataType: "json", - processData: false, - success: function(response){ - if (response.error) { - var errors = [] - if (response.error.errors) { - for (var key in response.error.errors) { - errors.push(response.error.errors[key].message); - } - } else { - errors.push(response.error) - } - if (errorCallback) { - errorCallback(errors) - } - else { - this.showErrors(errors) - } - return - } - if (successCallback) { - successCallback(response) - } - if (this.success) { - this.success(response) - } - }.bind(this), - error: function(response){ + var data = this.serialize() + var headers = new Headers() + headers.append("csrf-token", $("[name=_csrf]").attr("value")) + if (typeof data === "string") { + headers.append("content-type", "application/json") + } + + fetch(action, { + method: this.method.toUpperCase(), + headers: headers, + credentials: 'same-origin', + body: data, + }).then(raw => raw.json()) + .then(response => { + if (response.error) { var errors = [] - for (var key in response.error.errors) { - errors.push(response.error.errors[key].message); + if (response.error.errors) { + for (var key in response.error.errors) { + errors.push(response.error.errors[key].message); + } + } else { + errors.push(response.error) } if (errorCallback) { errorCallback(errors) @@ -135,17 +119,35 @@ var FormView = View.extend({ else { this.showErrors(errors) } - }.bind(this), - complete: function(response){ - if (this.useMinotaur) { - Minotaur.hide() - } - }.bind(this), + return + } + if (successCallback) { + successCallback(response) + } + if (this.success) { + this.success(response) + } + }).catch(response => { + var errors = [] + for (var key in response.error.errors) { + errors.push(response.error.errors[key].message); + } + if (errorCallback) { + errorCallback(errors) + } + else { + this.showErrors(errors) + } }) - - if (this.useMinotaur) { - Minotaur.show() - } +// complete: function(response){ +// if (this.useMinotaur) { +// Minotaur.hide() +// } +// } +// +// if (this.useMinotaur) { +// Minotaur.show() +// } this.beforeSend && this.beforeSend() }, |
