diff options
| author | Rene Ae <aehtyb@gmail.com> | 2015-11-30 15:24:27 -0600 |
|---|---|---|
| committer | Rene Ae <aehtyb@gmail.com> | 2015-11-30 15:24:27 -0600 |
| commit | 9d0e54228c79d151c30c527ea83efa7479793686 (patch) | |
| tree | 3baf83427cd61cb1994e8551eefba5cd21d1a65b /StoneIsland/platforms/ios/www/js/lib | |
| parent | b208cb0283c23d68e8013c0dd5ddd4e5e6df8950 (diff) | |
| parent | d26e72aea3b3127c95c5ba7069c95f73cf52a2fd (diff) | |
Merge branch 'master' of https://github.com/okfocus/stone-island
Diffstat (limited to 'StoneIsland/platforms/ios/www/js/lib')
22 files changed, 1173 insertions, 128 deletions
diff --git a/StoneIsland/platforms/ios/www/js/lib/account/AccountView.js b/StoneIsland/platforms/ios/www/js/lib/account/AccountView.js index cbd9a9c6..1c5c9f16 100644 --- a/StoneIsland/platforms/ios/www/js/lib/account/AccountView.js +++ b/StoneIsland/platforms/ios/www/js/lib/account/AccountView.js @@ -16,43 +16,102 @@ var AccountView = View.extend({ } }, - populateAddresses: function(data){ - console.log("populate addresses:", data) + addresses: [], + addressLookup: {}, + ccs: [], + ccLookup: {}, + + listAddresses: function(cb){ + sdk.address.list({ + success: function(data){ + this.populateAddresses(data, cb) + }.bind(this) + }) + }, + + populateAddresses: function(data, cb){ + console.log("populate addresses:", data.AddressBook.addressBookItem) + if (! data.AddressBook) { console.log("no addresses") + cb && cb() return } - // console.log(data.AddressBook) + + this.addresses = data.AddressBook.addressBookItem + this.addressLookup = {} data.AddressBook.addressBookItem.forEach(function(item){ - if (item.isDefault) { - // populate app.shipping.address + this.addressLookup[ item.Id ] = item + if (item.IsDefault) { + console.log("SHIPPING ADDRESS", item) + app.shipping.populate(item) } - else if (item.isBillingDefault) { - // populate app.billing.address + if (item.IsBillingDefault) { + console.log("BILLING ADDRESS") + app.payment.populate(item) } + }.bind(this)) + + app.cart.shipping.populate() + + cb && cb() + }, + + listCreditCards: function(cb){ + sdk.payment.list_credit_cards({ + success: function(data){ + this.populateCreditCards(data, cb) + }.bind(this) }) }, - + + populateCreditCards: function(data, cb){ + console.log("populate ccs:", data.CreditCards) + this.ccs = data.CreditCards + this.ccLookup = {} + if (! data.CreditCards || ! data.CreditCards.length) { + } + else { + data.CreditCards.forEach(function(cc){ + this.ccLookup[cc.Id] = cc + }.bind(this)) + app.payment.populate( data.CreditCards[0] ) + app.cart.payment.populate() + } + cb && cb() + }, + logged_in: function(cb){ - sdk.address.list({ - success: this.populateAddresses.bind(this) - }) + this.listAddresses() + this.listCreditCards() $("#nav .login").hide() $("#nav .account, #nav .logout").show() - if (app.last_view && app.last_view != app.login && app.last_view != app.signin && app.last_view != app.logout) { - app.view && app.view.hide && app.view.hide() - app.view = app.last_view - app.view.show() + if (! auth.deferred_product && app.last_view) { + if (app.last_view != app.login && app.last_view != app.signin && app.last_view != app.logout) { + app.view && app.view.hide && app.view.hide() + app.view = app.last_view + app.view.show() + } } else { cb && cb() } if ( ! auth.has_cart() ) { - auth.create_cart(auth.add_deferred_product_to_cart) + app.curtain.show("loading") + auth.create_cart(function(){ + auth.add_deferred_product_to_cart(function(){ + app.router.go("cart") + setTimeout(function(){ + app.curtain.hide("loading") + }, 500) + }) + }) } else { if (auth.deferred_product) { - auth.add_deferred_product_to_cart() + auth.add_deferred_product_to_cart(function(){ + app.router.go("cart") + }) } else { app.cart.load() diff --git a/StoneIsland/platforms/ios/www/js/lib/account/PaymentView.js b/StoneIsland/platforms/ios/www/js/lib/account/PaymentView.js index d61ab5ab..9410fa42 100644 --- a/StoneIsland/platforms/ios/www/js/lib/account/PaymentView.js +++ b/StoneIsland/platforms/ios/www/js/lib/account/PaymentView.js @@ -1,14 +1,33 @@ var PaymentView = FormView.extend({ el: "#payment", + + action: sdk.payment.add_credit_card, events: { }, + test_data: { + "Name":"Name", + "Surname":"Surname", + "Address1":"address", + "Address2":"address2", + "City":"Ferrara", + "Province":"NY", + "HolderIsoCountry":"IT", + "CreditCardCountry": "US", + "ZipCode":"40200", + "Type":"Visa", + "Number":"4111111111111111", + "ExpirationMonth":"09", + "ExpirationYear":"2017", + "Cvv":"1233", + }, + initialize: function(){ this.$form = this.$("form") this.$msg = this.$(".msg") - this.address = new AddressView ({ parent: this }) + this.address = new AddressView ({ parent: this, checkPhone: false }) this.cc = new CreditCardView ({ parent: this }) this.scroller = new IScroll('#payment', app.iscroll_options) }, @@ -17,9 +36,79 @@ var PaymentView = FormView.extend({ if (! auth.logged_in()) { return app.router.go("intro") } app.footer.show("SAVE", "CANCEL") document.body.className = "payment" + // this.preload() + }, + + populate: function(data){ + this.data = data || this.data + this.address.populate(data) + this.cc.populate(data) + }, + + finalize: function(data){ + if (this.cc.data && this.cc.data.Guid) { + sdk.payment.delete_credit_card({ + guid: this.cc.data.Guid, + success: function(){}, + error: function(){}, + }) + } + + data.IsDefault = "true" // this.$isDefault.prop("checked") ? "true" : "false" + data.UserId = sdk.auth.user_id + data.HolderIsoCountry = "US" + data.CreditCardNumber = data.Number + data.IsPreferred = "true" + + console.log(data) + return data + }, + + success: function(data){ + app.curtain.show("loading") + app.account.listAddresses(function(){ + app.curtain.hide("loading") + }) + }, + + error: function(data){ + console.log(data) }, - save: function(){ + cancel: function(){ + app.router.go("intro") }, }) + +/* + var new_card = { + "Name":"Name", + "Surname":"Surname", + "Address":"address", + "City":"Ferrara", + "Province":"FE", + "HolderIsoCountry":"IT", + "ZipCode":"40200", + "Type":"Visa", + "Number":"0000567890124285", + "ExpirationMonth":"02", + "ExpirationYear":"2017", + } + promise(sdk.payment.add_credit_card, { data: new_card }).then(function(data){ + last_guid = data['CreditCard']['Guid'] + assert(data.Header.StatusCode == 201) + assert(!! last_guid) + done() + }) + + promise(sdk.payment.list_credit_cards, { data: {} }).then(function(data){ + assert(data.Header.StatusCode == 201) + console.log(data) + done() + }) + + promise(sdk.payment.delete_credit_card, { guid: last_guid }).then(function(data){ + assert(data.Header.StatusCode == 200) + done() +*/
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/account/ProfileView.js b/StoneIsland/platforms/ios/www/js/lib/account/ProfileView.js index 9150f874..999e8d65 100644 --- a/StoneIsland/platforms/ios/www/js/lib/account/ProfileView.js +++ b/StoneIsland/platforms/ios/www/js/lib/account/ProfileView.js @@ -31,24 +31,9 @@ var ProfileView = FormView.extend({ if (data.CurrentPassword && ! data.NewPassword) { errors.push([ "NewPassword", "Please enter your new password." ]) } if (data.NewPassword && data.NewPassword.length < 7) { errors.push([ "CurrentPassword", "New password must be 7 characters or more." ]) } if (data.Gender === "NONE") { errors.push([ "Gender", "Please supply your gender." ]) } - - data.BirthDay += "T00:00:00Z" - data.YooxLetter = data.YooxLetter || "false" }, - save: function(e){ - e && e.preventDefault() - - var valid = this.validate() - if (valid.errors.length) { - this.show_errors(valid.errors) - return - } - else { - this.hide_errors() - } - - var data = valid.data + finalize: function(data){ if (data.CurrentPassword && (data.NewPassword || data.Email !== auth.user.Email)) { data.NewPassword = data.NewPassword || data.CurrentPassword data.NewEmail = data.NewEmail || auth.user.Email @@ -65,30 +50,20 @@ var ProfileView = FormView.extend({ }) } - var submissible_data = _.pick(valid.data, "Name Surname BirthDay Gender YooxLetter".split(" ")) - - submissible_data.idUser = auth.user_id - submissible_data.AccessToken = auth.access_token - submissible_data.Premium = "false" - submissible_data.LanguageId = "" - submissible_data.SiteCode = "STONEISLAND_US" - submissible_data.FuriganaName = "" - submissible_data.FuriganaSurname = "" - submissible_data.UserPromocode = "" + var submissible_data = _.pick(data, "Name Surname BirthDay Gender YooxLetter".split(" ")) +// submissible_data.idUser = auth.user_id +// submissible_data.AccessToken = auth.access_token +// submissible_data.Premium = "false" +// submissible_data.LanguageId = "" +// submissible_data.SiteCode = "STONEISLAND_US" +// submissible_data.FuriganaName = "" +// submissible_data.FuriganaSurname = "" +// submissible_data.UserPromocode = "" + submissible_data.BirthDay += "T00:00:00Z" + submissible_data.YooxLetter = data.YooxLetter || "false" submissible_data.DataProfiling = "true" - - app.curtain.show("loading") - sdk.account.update({ - data: submissible_data, - success: function(data){ - app.curtain.hide("loading") - this.success(data) - }.bind(this), - error: function(data){ - app.curtain.hide("loading") - this.error(data) - }.bind(this), - }) + + return submissible_data }, success: function(data){ @@ -96,5 +71,9 @@ var ProfileView = FormView.extend({ error: function(data){ }, + + cancel: function(){ + app.router.go("intro") + }, })
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/account/ShippingView.js b/StoneIsland/platforms/ios/www/js/lib/account/ShippingView.js index f5bf1661..b8b260ab 100644 --- a/StoneIsland/platforms/ios/www/js/lib/account/ShippingView.js +++ b/StoneIsland/platforms/ios/www/js/lib/account/ShippingView.js @@ -2,8 +2,26 @@ var ShippingView = FormView.extend({ el: "#shipping", + action: sdk.address.add, + events: { }, + + test_data: { + "Name":"name", + "Surname":"surname", + "Address":"address1\naddress2", + "IsDefault":false, + "IsBillingDefault":false, + "IsOwner":false, + "ZipCode":"88040", + "City":"City", + "Province":"NY", + "Phone":"1234567890", + "Mobile":"Mobile", + "Mail":"Mail", + "UserId": sdk.auth.user_id, + }, initialize: function(){ this.$form = this.$("form") @@ -14,11 +32,45 @@ var ShippingView = FormView.extend({ show: function(){ if (! auth.logged_in()) { return app.router.go("intro") } +// this.preload( this.data || this.test_data ) app.footer.show("SAVE", "CANCEL") document.body.className = "shipping" }, - save: function(){ + populate: function(data){ + this.data = data || this.data + this.address.populate(data) + }, + + finalize: function(data){ + if (this.address.data && this.address.data.Id) { + sdk.address.destroy({ + id: this.address.data.Id, + success: function(){}, + error: function(){}, + }) + } + + data.IsDefault = "true" // this.$isDefault.prop("checked") ? "true" : "false" + data.UserId = sdk.auth.user_id + + console.log(data) + return data + }, + + success: function(data){ + app.curtain.show("loading") + app.account.listAddresses(function(){ + app.curtain.hide("loading") + }) + }, + + error: function(data){ + console.log(data) + }, + + cancel: function(){ + app.router.go("intro") }, })
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/auth/LogoutView.js b/StoneIsland/platforms/ios/www/js/lib/auth/LogoutView.js index 735c0242..481dcb8d 100644 --- a/StoneIsland/platforms/ios/www/js/lib/auth/LogoutView.js +++ b/StoneIsland/platforms/ios/www/js/lib/auth/LogoutView.js @@ -7,9 +7,10 @@ var LogoutView = View.extend({ show: function(){ document.body.className = "logout" + app.header.set_cart_count(0) app.footer.hide() + auth.log_out() app.account.logged_out() - auth.clear_user() }, })
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/auth/SignupView.js b/StoneIsland/platforms/ios/www/js/lib/auth/SignupView.js index 7f894c3c..7e6fc04d 100644 --- a/StoneIsland/platforms/ios/www/js/lib/auth/SignupView.js +++ b/StoneIsland/platforms/ios/www/js/lib/auth/SignupView.js @@ -61,7 +61,9 @@ var SignupView = FormView.extend({ if (data.DataProfiling !== "true") { errors.push([ "DataProfiling", "You must consent to use this service." ]) } if (data.DataProfiling2 !== "true") { errors.push([ "DataProfiling2", "You must consent to use this service." ]) } if (! data.YooxLetter) { data.YooxLetter = false } - + }, + + finalize: function(data){ delete data.DataProfiling2 delete data.ConfirmEmail @@ -69,6 +71,7 @@ var SignupView = FormView.extend({ this.last_data = data console.log(data) + return data }, privacy_link: function(){ diff --git a/StoneIsland/platforms/ios/www/js/lib/blogs/ArchiveView.js b/StoneIsland/platforms/ios/www/js/lib/blogs/ArchiveView.js index 051ef0c9..3db5c8da 100644 --- a/StoneIsland/platforms/ios/www/js/lib/blogs/ArchiveView.js +++ b/StoneIsland/platforms/ios/www/js/lib/blogs/ArchiveView.js @@ -1,20 +1,34 @@ var ArchiveView = ScrollableView.extend({ el: "#archive", - template: $("#archive .template").html(), + menu_template: $("#archive .menu .template").html(), + row_template: $("#archive .scroll .template").html(), events: { + "click .item": "pick", }, initialize: function(){ + this.$menu_items = this.$(".menu .items") this.$content = this.$(".content") this.$loader = this.$(".loader") - this.scroller = new IScroll('#archive', app.iscroll_options) + this.scroller = new IScroll('#archive .scroll', app.iscroll_options) }, + back: function(){ + this.$el.addClass("menu") + app.header.set_back(false) + }, + + pick: function(){ + this.$el.removeClass("menu") + app.header.set_back(true) + }, + show: function(){ this.deferScrollToTop() app.footer.hide() + this.back() document.body.className = "archive" }, @@ -24,12 +38,22 @@ var ArchiveView = ScrollableView.extend({ this.$content.empty() // id title images[ uri label code caption ] - this.data.forEach(function(row){ - var t = this.template.replace(/{{image}}/, row.images[0].uri) - .replace(/{{code}}/, row.code) - .replace(/{{title}}/, row.title) + this.data.forEach(function(row, index){ + var t = this.row_template.replace(/{{image}}/, row.images[0].uri) + .replace(/{{label}}/, row.images[0].label) + .replace(/{{code}}/, row.images[0].code) + .replace(/{{caption}}/, row.images[0].caption) this.$content.append(t) + + var t = this.menu_template.replace(/{{title}}/, row.title) + var $t = $(t) + $t.data("title", row.title) + $t.data("index", index) + this.$menu_items.append($t) }.bind(this)) + + this.back() + this.deferScrollToTop() }, })
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/cart/CartConfirm.js b/StoneIsland/platforms/ios/www/js/lib/cart/CartConfirm.js index 687f3a93..aa6ec9e4 100644 --- a/StoneIsland/platforms/ios/www/js/lib/cart/CartConfirm.js +++ b/StoneIsland/platforms/ios/www/js/lib/cart/CartConfirm.js @@ -14,6 +14,7 @@ var CartConfirm = FormView.extend({ document.body.className = "cart" app.cart.el.className = "confirm" app.footer.show("PLACE ORDER", "CANCEL") + window.location.hash = "#/cart/confirm" this.deferScrollToTop() }, diff --git a/StoneIsland/platforms/ios/www/js/lib/cart/CartPayment.js b/StoneIsland/platforms/ios/www/js/lib/cart/CartPayment.js index 19d21ffd..fec5e1d1 100644 --- a/StoneIsland/platforms/ios/www/js/lib/cart/CartPayment.js +++ b/StoneIsland/platforms/ios/www/js/lib/cart/CartPayment.js @@ -2,39 +2,151 @@ var CartPayment = FormView.extend({ el: "#cart_payment", + address_template: $("#cart_payment .address_template").html(), + cc_template: $("#cart_payment .cc_template").html(), + + action: sdk.cart.set_credit_card, + + address_list_mode: false, + cc_list_mode: false, + events: { + "change [name=SameAsShipping]": "toggle_shipping", + "click .address_dropdown": "toggle_address", + "click .cc_dropdown": "toggle_cc", }, initialize: function(opt){ this.parent = opt.parent this.$form = this.$("form") this.$msg = this.$(".msg") - this.address = new AddressView ({ parent: this }) + this.$same_as_shipping = this.$("[name=SameAsShipping]") + this.$billing_address_rapper = this.$(".billing_address_rapper") + this.$address_list = this.$(".address_list") + this.$address_form = this.$(".address") + this.$address_dropdown = this.$(".address_dropdown") + this.$cc_list = this.$(".cc_list") + this.$cc_form = this.$(".cc") + this.$cc_dropdown = this.$(".cc_dropdown") + + this.address = new AddressView ({ parent: this, checkPhone: false }) this.cc = new CreditCardView ({ parent: this }) this.scroller = new IScroll('#cart_payment', app.iscroll_options) + this.address.disabled = true + this.cc.disabled = true }, - // sdk.cart.set_credit_card - // sdk.payment.list_credit_cards - // sdk.payment.add_credit_card - // sdk.payment.delete_credit_card - // sdk.payment.get_payment_types - // sdk.cart.use_stored_credit_card - show: function(){ document.body.className = "cart" app.cart.el.className = "payment" app.footer.show("CONFIRM >", "CANCEL") + window.location.hash = "#/cart/payment" + this.populate() this.deferScrollToTop() + }, + + toggle_shipping: function(){ + setTimeout(function(){ + var state = this.$same_as_shipping.prop("checked") + this.$billing_address_rapper.toggle( ! state ) + }.bind(this)) + }, + + toggle_address: function(state){ + if (! app.account.ccs.length) { + state = false + } + // this.$address_dropdown.toggle( !! app.account.ccs.length ) + + this.address_list_mode = typeof state == "boolean" ? state : ! this.list_mode + this.address.disabled = this.address_list_mode + this.$address_form.toggle(! this.address_list_mode) + this.$address_list.toggle(this.address_list_mode) + }, - sdk.cart.set_payment_type({ data: payment_id }) + toggle_cc: function(state){ + if (! app.account.ccs.length) { + state = false + } + // this.$cc_dropdown.toggle( !! app.account.ccs.length ) + + this.cc_list_mode = typeof state == "boolean" ? state : ! this.cc_list_mode + this.cc.disabled = this.cc_list_mode + this.$cc_form.toggle(! this.cc_list_mode) + this.$cc_list.toggle(this.cc_list_mode) + }, + + populate: function(){ + this.$(".save_as_default").show() + this.$address_list.empty() + this.$cc_list.empty() + this.toggle_address( !! app.account.ccs.length ) + this.toggle_cc( !! app.account.ccs.length ) + + app.account.ccs.forEach(function(cc){ + console.log(cc) + + var address_t = this.address_template.replace(/{{id}}/g, cc.Id) + .replace(/{{checked}}/g, cc.IsDefault ? "checked" : "") + .replace(/{{name}}/g, cc.Name + " " + cc.Surname) + .replace(/{{address}}/g, cc.Address.replace(/\n$/,"").replace("\n", "<br>")) + .replace(/{{city}}/g, cc.City) + .replace(/{{state}}/g, cc.Province) + .replace(/{{zip}}/g, cc.ZipCode) + + var cc_t = this.cc_template.replace(/{{id}}/g, cc.Id) + .replace(/{{checked}}/g, cc.IsDefault ? "checked" : "") + .replace(/{{last4}}/g, cc.Last4) + .replace(/{{type}}/g, cc.Type) + .replace(/{{exp}}/g, cc.ExpirationMonth + "/" + cc.ExpirationYear) + + this.$address_list.append(address_t) + this.$cc_list.append(cc_t) + }) + }, + + finalize: function(data){ + var shipping_info = {}, address_data, address_id, cc_info = {}, cc_data, cc_id + var shipping_type = $("[name=ShippingType]").filter(function(){ return $(this).prop("checked") }).val() + + if (this.list_mode) { + address_id = $("[name=AddressId]").filter(function(){ return $(this).prop("checked") }).val() + address_data = app.account.addressLookup[ address_id ] + } + else { + address_data = data + } + if (this.cc_list_mode) { + cc_id = $("[name=CCId]").filter(function(){ return $(this).prop("checked") }).val() + cc_data = app.account.ccLookup[ cc_id ] + } + else { + cc_data = data + } + + shipping_info.Name = address_data.Name + shipping_info.Surname = address_data.Surname + shipping_info.Email = auth.user.Email + shipping_info.Phone = address_data.Phone + shipping_info.Mobile = address_data.Phone + shipping_info.StreetWithNumber = address_data.Address + shipping_info.PostalCode = address_data.ZipCode + shipping_info.City = address_data.City + shipping_info.Province = address_data.Province + shipping_info.Region = address_data.Province + shipping_info.CountryCode = "US" + + return shipping_info }, - save: function(){ + success: function(){ + app.router.go('cart/confirm') }, cancel: function(){ + app.router.go('cart/shipping') }, + })
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/cart/CartShipping.js b/StoneIsland/platforms/ios/www/js/lib/cart/CartShipping.js index 7d7c8540..1a9653e1 100644 --- a/StoneIsland/platforms/ios/www/js/lib/cart/CartShipping.js +++ b/StoneIsland/platforms/ios/www/js/lib/cart/CartShipping.js @@ -2,32 +2,116 @@ var CartShipping = FormView.extend({ el: "#cart_shipping", + action: sdk.cart.set_shipping_address, + + list_mode: true, + + template: $("#cart_shipping .template").html(), + events: { + "click .dropdown-wrapper": "toggle_dropdown", }, initialize: function(opt){ this.parent = opt.parent this.$form = this.$("form") + this.$dropdown_wrapper = this.$(".dropdown-wrapper") + this.$address_list = this.$(".address_list") + this.$address_form = this.$(".address") this.$msg = this.$(".msg") this.address = new AddressView ({ parent: this }) this.scroller = new IScroll('#cart_shipping', app.iscroll_options) + this.address.disabled = true }, show: function(){ document.body.className = "cart" app.cart.el.className = "shipping" app.footer.show("PAYMENT >", "CANCEL") + window.location.hash = "#/cart/shipping" + this.populate() this.deferScrollToTop() }, + populate: function(){ + // id checked name address city state zip + this.$(".save_as_default").show() + this.$address_list.empty() + if (! app.account.addresses.length) { + this.toggle_dropdown(false) + return + } + app.account.addresses.forEach(function(address){ + var t = this.template.replace(/{{id}}/g, address.Id) + .replace(/{{checked}}/g, address.IsDefault ? "checked" : "") + .replace(/{{name}}/g, address.Name + " " + address.Surname) + .replace(/{{address}}/g, address.Address.replace(/\n$/,"").replace("\n", "<br>")) + .replace(/{{city}}/g, address.City) + .replace(/{{state}}/g, address.Province) + .replace(/{{zip}}/g, address.ZipCode) + this.$address_list.append(t) + }.bind(this)) + }, + + toggle_dropdown: function(state){ + if (! app.account.addresses.length) { + state = false + } + this.list_mode = typeof state == "boolean" ? state : ! this.list_mode + this.$dropdown_wrapper.toggle( !! app.account.addresses.length ) + this.address.disabled = this.list_mode + this.$address_form.toggle(! this.list_mode) + this.$address_list.toggle(this.list_mode) + }, + // sdk.cart.set_shipping_address // sdk.shipping.get_delivery_types // sdk.shipping.set_delivery_type - save: function(){ + shipping_types: { + Standard: 1, + Express: 2, + }, + + finalize: function(data){ + var shipping_info = {}, address_data, address_id + var shipping_type = $("[name=ShippingType]").filter(function(){ return $(this).prop("checked") }).val() + + sdk.shipping.set_delivery_type({ + id: this.shipping_types[shipping_type], + success: function(data){ console.log("set shipping type", data) }, + error: function(data){ console.log("didnt set shipping type", data) }, + }) + + if (this.list_mode) { + address_id = $("[name=AddressId]").filter(function(){ return $(this).prop("checked") }).val() + address_data = app.account.addressLookup[ address_id ] + } + else { + address_data = data + } + + shipping_info.Name = address_data.Name + shipping_info.Surname = address_data.Surname + shipping_info.Email = auth.user.Email + shipping_info.Phone = address_data.Phone + shipping_info.Mobile = address_data.Phone + shipping_info.StreetWithNumber = address_data.Address + shipping_info.PostalCode = address_data.ZipCode + shipping_info.City = address_data.City + shipping_info.Province = address_data.Province + shipping_info.Region = address_data.Province + shipping_info.CountryCode = "US" + + return shipping_info + }, + + success: function(){ + app.router.go('cart/payment') }, cancel: function(){ + app.router.go('cart/summary') }, })
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/cart/CartSummary.js b/StoneIsland/platforms/ios/www/js/lib/cart/CartSummary.js index 209e3102..9a24afa5 100644 --- a/StoneIsland/platforms/ios/www/js/lib/cart/CartSummary.js +++ b/StoneIsland/platforms/ios/www/js/lib/cart/CartSummary.js @@ -27,6 +27,7 @@ var CartSummary = ScrollableView.extend({ show: function(){ document.body.className = "cart" app.cart.el.className = "summary" + window.location.hash = "#/cart/summary" if (auth.has_cart()) { this.load() } @@ -110,21 +111,21 @@ var CartSummary = ScrollableView.extend({ .replace(/{{price}}/, as_cash(details.Item.Price.DiscountedPrice)) $el.data("price", details.Item.Price.DiscountedPrice) $el.html(t) - this.deferScrollToTop() + this.refreshScroller() }.bind(this)) }.bind(this)) this.updateTotals() this.el.className = "full" - this.deferScrollToTop() + this.refreshScroller() }, updateCounts: function(){ - app.header.set_cart_count(this.data.Cart.Items.length) - this.parent.$itemcount.html(pluralize(this.data.Cart.Items.length, "ITEM", "S")) + app.header.set_cart_count( this.data.Cart.Items.length ) + this.parent.setHeaderCount( this.data.Cart.Items.length ) }, - + updateTotals: function(){ var subtotal = this.data.Cart.Totals.TotalWithoutPromotions var shipping_cost = this.data.Cart.DeliveryMethod.Selected.Amount.Total @@ -140,6 +141,7 @@ var CartSummary = ScrollableView.extend({ empty: function(){ app.footer.hide() app.header.set_cart_count(0) + this.parent.setHeaderCount( 0 ) this.parent.$itemcount.html("0 ITEMS") this.el.className = "empty" }, @@ -155,9 +157,11 @@ var CartSummary = ScrollableView.extend({ remove_item: function(e){ var $el = $( e.currentTarget ).closest(".cart_item_row") var data = $el.data() - data.Cart.Totals.TotalWithoutPromotions -= data.price - data.Cart.Totals.TotalToPay -= data.price - + + console.log(this.data.Cart) + + this.data.Cart.Totals.TotalWithoutPromotions -= data.price + this.data.Cart.Totals.TotalToPay -= data.price this.data.Cart.Items = this.data.Cart.Items.filter(function(item){ return ( item['Code10'] !== data.code || item['Size'] !== data.size) }) @@ -165,18 +169,20 @@ var CartSummary = ScrollableView.extend({ this.updateTotals() this.updateCounts() $el.remove() + this.refreshScroller() if (this.data.Cart.Items.length == 0) { this.empty() } + app.curtain.show("loading") sdk.cart.delete_item({ data: { Code10: data.code, Size: data.size, }, success: function(){ - console.log("damn") + app.curtain.hide("loading") }, }) }, diff --git a/StoneIsland/platforms/ios/www/js/lib/cart/CartView.js b/StoneIsland/platforms/ios/www/js/lib/cart/CartView.js index 8bea43dc..b57caadd 100644 --- a/StoneIsland/platforms/ios/www/js/lib/cart/CartView.js +++ b/StoneIsland/platforms/ios/www/js/lib/cart/CartView.js @@ -15,6 +15,8 @@ var CartView = View.extend({ this.confirm = new CartConfirm ({ parent: this }) this.thanks = new CartThanks ({ parent: this }) + this.$full_msg = this.$(".full_msg") + this.$empty_msg = this.$(".empty_msg") this.$itemcount = this.$(".itemcount") }, @@ -47,4 +49,16 @@ var CartView = View.extend({ this.payment.show() }, + setHeaderCount: function(n){ + if (n) { + this.$itemcount.html(pluralize(n, "ITEM", "S")) + this.$full_msg.show() + this.$empty_msg.hide() + } + else { + this.$full_msg.hide() + this.$empty_msg.show() + } + }, + })
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/nav/AddressView.js b/StoneIsland/platforms/ios/www/js/lib/nav/AddressView.js index 15a5784d..31e9d802 100644 --- a/StoneIsland/platforms/ios/www/js/lib/nav/AddressView.js +++ b/StoneIsland/platforms/ios/www/js/lib/nav/AddressView.js @@ -3,24 +3,25 @@ var AddressView = SerializableView.extend({ template: $("#address_template").html(), + disabled: false, + events: { }, initialize: function(opt){ this.parent = opt.parent - this.$el = this.parent.$(".address") - this.el = this.$el[0] + this.checkPhone = 'checkPhone' in opt ? opt.checkPhone : true + this.setElement( this.parent.$(".address") ) this.$el.html(this.template) }, populate: function(data){ - this.parent.$(".address input").val("") - Object.keys(data).forEach(function(key){ - this.parent$(".address [name=" + key + "]").val(data[key]) - }.bind(this)) - }, - - deserialize: function(){ + this.data = data + var address = data.Address.split("\n") + data.Address1 = address[0] + data.Address2 = address[1] + this.$(".address input").val("") + this.load_data(data) }, validate_presence: { @@ -28,12 +29,223 @@ var AddressView = SerializableView.extend({ "Surname": "Please enter your last name.", "Address1": "Please enter your street address.", "City": "Please enter your city.", - "State": "Please choose your state.", - "Phone": "Please enter your phone number.", + "ZipCode": "Please enter your zip code.", }, validate_fields: function(data, errors){ - if (data.Phone.replace(/[^0-9]/g, "").length < 10) { errors.push([ "Phone", "Phone numbers must be at least 10 digits." ]) } + if (this.disabled) { return } + if (this.checkPhone && ! data.Phone) { errors.push([ "Phone", "Please enter your phone number." ]) } + if (this.checkPhone && data.Phone && data.Phone.replace(/[^0-9]/g, "").length < 10) { errors.push([ "Phone", "Phone numbers must be at least 10 digits." ]) } + if (! data.Province || data.Province == "NONE") { errors.push([ "Province", "Please choose your state." ]) } + data.Address = data.Address1 + "\n" + data.Address2 + data.UserId = auth.user_id + delete data.Address1 + delete data.Address2 }, }) + + +var COUNTRIES = [ + ['Country Name', 'NONE'], + ['United States', 'US'], + ['Abkhazia', 'GE'], + ['Afghanistan', 'AF'], + ['Albania', 'AL'], + ['Algeria', 'DZ'], + ['Andorra', 'AD'], + ['Angola', 'AO'], + ['Antigua and Barbuda', 'AG'], + ['Argentina', 'AR'], + ['Armenia', 'AM'], + ['Australia', 'AU'], + ['Austria', 'AT'], + ['Azerbaijan', 'AZ'], + ['Bahamas', 'BS'], + ['Bahrain', 'BH'], + ['Bangladesh', 'BD'], + ['Barbados', 'BB'], + ['Belarus', 'BY'], + ['Belgium', 'BE'], + ['Belize', 'BZ'], + ['Benin', 'BJ'], + ['Bhutan', 'BT'], + ['Bolivia', 'BO'], + ['Bosnia and Herzegovina', 'BA'], + ['Botswana', 'BW'], + ['Brazil', 'BR'], + ['Brunei', 'BN'], + ['Bulgaria', 'BG'], + ['Burkina Faso', 'BF'], + ['Burundi', 'BI'], + ['Cambodia', 'KH'], + ['Cameroon', 'CM'], + ['Canada', 'CA'], + ['Cape Verde', 'CV'], + ['Central African Republic', 'CF'], + ['Chad', 'TD'], + ['Chile', 'CL'], + ['China', 'CN'], + ['Colombia', 'CO'], + ['Comoros', 'KM'], + ['Congo', 'CD'], + ['Congo-Brazzaville', 'CG'], + ['Costa Rica', 'CR'], + ['Cote d\'Ivoire (Ivory Coast)', 'CI'], + ['Croatia', 'HR'], + ['Cuba', 'CU'], + ['Cyprus', 'CY'], + ['Czech Republic', 'CZ'], + ['Denmark', 'DK'], + ['Djibouti', 'DJ'], + ['Dominica', 'DM'], + ['Dominican Republic', 'DO'], + ['Ecuador', 'EC'], + ['Egypt', 'EG'], + ['El Salvador', 'SV'], + ['Equatorial Guinea', 'GQ'], + ['Eritrea', 'ER'], + ['Estonia', 'EE'], + ['Ethiopia', 'ET'], + ['Fiji', 'FJ'], + ['Finland', 'FI'], + ['France', 'FR'], + ['Gabon', 'GA'], + ['Gambia', '220'], + ['Georgia', 'GE'], + ['Germany', 'DE'], + ['Ghana', 'GH'], + ['Greece', 'GR'], + ['Grenada', 'GD'], + ['Guatemala', 'GT'], + ['Guinea', 'GN'], + ['Guinea-Bissau', 'GW'], + ['Guyana', 'GY'], + ['Haiti', 'HT'], + ['Honduras', 'HN'], + ['Hungary', 'HU'], + ['Iceland', 'IS'], + ['India', 'IN'], + ['Indonesia', 'ID'], + ['Iran', 'IR'], + ['Iraq', 'IQ'], + ['Ireland', 'IE'], + ['Israel', 'IL'], + ['Italy', 'IT'], + ['Jamaica', 'JM'], + ['Japan', 'JP'], + ['Jordan', 'JO'], + ['Kazakhstan', 'KZ'], + ['Kenya', 'KE'], + ['Kiribati', 'KI'], + ['Kuwait', 'KW'], + ['Kyrgyzstan', 'KG'], + ['Laos', 'LA'], + ['Latvia', 'LV'], + ['Lebanon', 'LB'], + ['Lesotho', 'LS'], + ['Liberia', 'LR'], + ['Libya', 'LY'], + ['Liechtenstein', 'LI'], + ['Lithuania', 'LT'], + ['Luxembourg', 'LU'], + ['Macedonia', 'MK'], + ['Madagascar', 'MG'], + ['Malawi', 'MW'], + ['Malaysia', 'MY'], + ['Maldives', 'MV'], + ['Mali', 'ML'], + ['Malta', 'MT'], + ['Marshall Islands', 'MH'], + ['Mauritania', 'MR'], + ['Mauritius', 'MU'], + ['Mexico', 'MX'], + ['Micronesia', 'FM'], + ['Moldova', 'MD'], + ['Monaco', 'MC'], + ['Mongolia', 'MN'], + ['Montenegro', 'ME'], + ['Morocco', 'MA'], + ['Mozambique', 'MZ'], + ['Myanmar', 'MM'], + ['Nagorno-Karabakh', 'AZ'], + ['Namibia', 'NA'], + ['Nauru', 'NR'], + ['Nepal', 'NP'], + ['Netherlands', 'NL'], + ['New Zealand', 'NZ'], + ['Nicaragua', 'NI'], + ['Niger', 'NE'], + ['Nigeria', 'NG'], + ['North Korea', 'KP'], + ['Northern Cyprus', 'CY'], + ['Norway', 'NO'], + ['Oman', 'OM'], + ['Pakistan', 'PK'], + ['Palau', 'PW'], + ['Panama', 'PA'], + ['Papua New Guinea', 'PG'], + ['Paraguay', 'PY'], + ['Peru', 'PE'], + ['Philippines', 'PH'], + ['Poland', 'PL'], + ['Portugal', 'PT'], + ['Qatar', 'QA'], + ['Romania', 'RO'], + ['Russia', 'RU'], + ['Rwanda', 'RW'], + ['Saint Kitts and Nevis', 'KN'], + ['Saint Lucia', 'LC'], + ['Saint Vincent and the Grenadines', 'VC'], + ['Samoa', 'WS'], + ['San Marino', 'SM'], + ['Sao Tome and Principe', 'ST'], + ['Saudi Arabia', 'SA'], + ['Senegal', 'SN'], + ['Serbia', 'RS'], + ['Seychelles', 'SC'], + ['Sierra Leone', 'SL'], + ['Singapore', 'SG'], + ['Slovakia', 'SK'], + ['Slovenia', 'SI'], + ['Solomon Islands', 'SB'], + ['Somalia', 'SO'], + ['Somaliland', 'SO'], + ['South Africa', 'Rand'], + ['South Korea', 'KR'], + ['South Ossetia', 'GE'], + ['Spain', 'ES'], + ['Sri Lanka', 'LK'], + ['Sudan', 'SD'], + ['Suriname', 'SR'], + ['Swaziland', 'SZ'], + ['Sweden', 'SE'], + ['Switzerland', 'CH'], + ['Syria', 'SY'], + ['Taiwan', 'TW'], + ['Tajikistan', 'TJ'], + ['Tanzania', 'TZ'], + ['Thailand', 'TH'], + ['Timor-Leste', 'TL'], + ['Togo', 'TG'], + ['Tonga', 'TO'], + ['Transnistria', 'MD'], + ['Trinidad and Tobago', 'TT'], + ['Tunisia', 'TN'], + ['Turkey', 'TR'], + ['Turkmenistan', 'TM'], + ['Tuvalu', 'TV'], + ['Uganda', 'UG'], + ['Ukraine', 'UA'], + ['United Arab Emirates', 'AE'], + ['United Kingdom', 'GB'], + ['Uruguay', 'UY'], + ['Uzbekistan', 'UZ'], + ['Vanuatu', 'VU'], + ['Vatican City', 'VA'], + ['Venezuela', 'VE'], + ['Vietnam', 'VN'], + ['Yemen', 'YE'], + ['Zambia', 'ZM'], + ['Zimbabwe', 'ZW'], +] diff --git a/StoneIsland/platforms/ios/www/js/lib/nav/CreditCardView.js b/StoneIsland/platforms/ios/www/js/lib/nav/CreditCardView.js index 33ecab79..1855b7a9 100644 --- a/StoneIsland/platforms/ios/www/js/lib/nav/CreditCardView.js +++ b/StoneIsland/platforms/ios/www/js/lib/nav/CreditCardView.js @@ -1,27 +1,53 @@ -var CreditCardView = View.extend({ +var CreditCardView = SerializableView.extend({ template: $("#creditcard_template").html(), + cardOptions: { + accept: ['visa', 'mastercard', 'amex'], + }, + events: { }, initialize: function(opt){ this.parent = opt.parent - this.parent.$(".cc").html(this.template) + this.setElement( this.parent.$(".cc") ) + this.$el.html(this.template) + + this.$number = this.$("[name=Number]") + this.$number.validateCreditCard(this.updateCard.bind(this), this.cardOptions) }, populate: function(data){ + this.data = data + data.Number = "XXXX XXXX XXXX " + data.Number + this.$number.attr("type", "text") this.parent.$(".cc input").val("") - Object.keys(data).forEach(function(key){ - this.parent$(".cc [name=" + key + "]").val(data[key]) - }.bind(this)) + this.$(".cc input").val("") + this.load_data(data) + }, + + updateCard: function(card){ + // console.log(card) + // card.card_type.name + // card.card_type.valid }, validate_presence: { + 'Number': 'Please enter your credit card number.', + 'Cvv': 'Please enter your security code.', }, validate_fields: function(data, errors){ + var card = this.$number.validateCreditCard(this.cardOptions) + if (! card.valid) { errors.push([ "Number", "Your card number is invalid." ]) } + if (! data.ExpirationMonth || data.ExpirationMonth == "NONE") { errors.push([ "ExpirationMonth", "Please enter the expiration month." ]) } + if (! data.ExpirationYear || data.ExpirationYear == "NONE") { errors.push([ "ExpirationYear", "Please select the expiration month." ]) } + data.UserId = auth.user_id + if (card.valid) { + data.Type = card.card_type.name + } }, }) diff --git a/StoneIsland/platforms/ios/www/js/lib/nav/HeaderView.js b/StoneIsland/platforms/ios/www/js/lib/nav/HeaderView.js index 7563be2d..b2f01208 100644 --- a/StoneIsland/platforms/ios/www/js/lib/nav/HeaderView.js +++ b/StoneIsland/platforms/ios/www/js/lib/nav/HeaderView.js @@ -14,7 +14,9 @@ var HeaderView = View.extend({ this.$cart_count = this.$(".cart_count") }, + back_state: false, set_back: function(state){ + this.back_state = state if (state) { this.$burger[0].className = "burger ion-ios-arrow-left" } @@ -24,7 +26,7 @@ var HeaderView = View.extend({ }, nav: function(){ - if (app.view.back) { + if (this.back_state) { app.view.back() } else { @@ -43,7 +45,7 @@ var HeaderView = View.extend({ count: 0, set_cart_count: function(n){ this.count = n - this.$cart_count.html(n) + this.$cart_count.html(n || " ") }, increment_cart_count: function(){ this.$cart_count.html( ++this.count ) diff --git a/StoneIsland/platforms/ios/www/js/lib/nav/IntroView.js b/StoneIsland/platforms/ios/www/js/lib/nav/IntroView.js index 87e68959..2d8dca43 100644 --- a/StoneIsland/platforms/ios/www/js/lib/nav/IntroView.js +++ b/StoneIsland/platforms/ios/www/js/lib/nav/IntroView.js @@ -34,6 +34,7 @@ var IntroView = View.extend({ else { heading = e.alpha || 0 } + heading = - heading this.compass.style[transformProp] = "translateZ(0) translateX(-50%) translateY(-50%) rotate(" + heading + "deg)" }, diff --git a/StoneIsland/platforms/ios/www/js/lib/products/CollectionView.js b/StoneIsland/platforms/ios/www/js/lib/products/CollectionView.js index 8ea03bd4..e35b789d 100644 --- a/StoneIsland/platforms/ios/www/js/lib/products/CollectionView.js +++ b/StoneIsland/platforms/ios/www/js/lib/products/CollectionView.js @@ -36,7 +36,7 @@ var CollectionView = ScrollableView.extend({ this.fetch() }, - ok: function(){ + save: function(){ this.filterView.filter() }, diff --git a/StoneIsland/platforms/ios/www/js/lib/products/ProductView.js b/StoneIsland/platforms/ios/www/js/lib/products/ProductView.js index c99ff0ab..e151c208 100644 --- a/StoneIsland/platforms/ios/www/js/lib/products/ProductView.js +++ b/StoneIsland/platforms/ios/www/js/lib/products/ProductView.js @@ -34,6 +34,7 @@ var ProductView = ScrollableView.extend({ size: null, color: null, code: null, + is_onesize: false, sizes: null, colors: null, @@ -105,6 +106,8 @@ var ProductView = ScrollableView.extend({ var sizes = this.find_sizes_for_color(default_color_id) var size = sizes[0] var size_label = this.sizes[size].label + + this.is_onesize = !! this.sizes[1] // console.log(color, color_label, size, size_label) @@ -158,7 +161,7 @@ var ProductView = ScrollableView.extend({ colors[ color['ColorId'] ] = { id: color['Code10'], code: color['Code10'], - label: color['ColorDescription'], + label: color['ColorDescription'].toUpperCase(), sizes: {}, } }) @@ -167,7 +170,7 @@ var ProductView = ScrollableView.extend({ size_lookup[ label ] = size['SizeId'] sizes[ size['SizeId'] ] = { id: label, - label: label, + label: label.toUpperCase(), colors: {}, } }) @@ -182,6 +185,7 @@ var ProductView = ScrollableView.extend({ }, select_size: function(){ + if (this.is_onesize) { return this.select_color() } if (this.item['Sizes'].length == 0) { return } var sizes = Object.keys(this.sizes).map(function(key){ return this.sizes[key] @@ -205,27 +209,14 @@ var ProductView = ScrollableView.extend({ // ADD TO CART save: function(){ - auth.deferred_product = { Size: this.size, Code10: this.code } - if ( ! auth.logged_in() ) { - app.router.go("account/login") - app.last_view = app.cart - } - else if ( ! auth.has_cart() ) { - auth.create_cart(function(){ - auth.add_deferred_product_to_cart(function(){ - app.router.go("cart") - }) - }) - } - else { - auth.add_deferred_product_to_cart(function(){ - app.router.go("cart") - }) - } + this.add_to_cart({ route: false }) }, - // BUY NOW cancel: function(){ + this.add_to_cart({ route: true }) + }, + + add_to_cart: function(opt){ auth.deferred_product = { Size: this.size, Code10: this.code } if ( ! auth.logged_in() ) { app.router.go("account/login") @@ -240,7 +231,9 @@ var ProductView = ScrollableView.extend({ } else { auth.add_deferred_product_to_cart(function(){ - app.router.go("cart") + if (opt.route) { + app.router.go("cart") + } }) } }, @@ -262,6 +255,7 @@ var SIZE_LOOKUP = { "XL": "X-LARGE", "XXL": "XX-LARGE", "3XL": "3X-LARGE", + "OneSize": "ONESIZE", } var SIZE_ORDER = "XS S M L XL XXL 3XL".split(" ") diff --git a/StoneIsland/platforms/ios/www/js/lib/view/Router.js b/StoneIsland/platforms/ios/www/js/lib/view/Router.js new file mode 100644 index 00000000..a8ec331f --- /dev/null +++ b/StoneIsland/platforms/ios/www/js/lib/view/Router.js @@ -0,0 +1,75 @@ +var Router = View.extend({ + + routeByHash: false, + + go: function(url){ + this.parseRoute(url) + }, + + route: function(){ + var path = this.routeByHash ? window.location.hash.substr(0) : window.location.pathname + path = path || "/" + this.originalPath = path + this.parseRoute(path) + }, + + parseRoute: function(pathname){ + + pathname = pathname.replace(/^#/, "") + + if (pathname[0] !== "/") { pathname = "/" + pathname } + + var routes = this.routes, + path = pathname.split("/"); + + for (var i = 0; i < path.length; i++) { + if (! path[i].length) { + path[i] = null + } + } + + if (pathname in routes) { + this[this.routes[pathname]]() + return + } + + if (path[path.length-1] == null) { + path.pop() + } + + for (var route in routes) { + var routePath = route.split("/") + if (routePath[1] == path[1]) { + if (routePath[2] && routePath[2].indexOf(":") !== -1 && path[2] && (path[3] === routePath[3]) ) { + this[this.routes[route]](path[2]) + return + } + else if (routePath[2] == path[2]) { + if (routePath[3] && path[3]) { + if (routePath[3].indexOf(":") !== -1) { + this[this.routes[route]](path[3]) + return + } + else if (routePath[3] == path[3]) { + this[this.routes[route]]() + return + } + } + else if (! routePath[3] && ! path[3]) { + this[this.routes[route]]() + return + } + } + else if (! routePath[2] && (! path[2].length || ! path[2])) { + this[this.routes[route]]() + return + } + } + } + + if (is_mobile) { + window.location.href = "/" + } + } + +}) diff --git a/StoneIsland/platforms/ios/www/js/lib/view/Scrollable.js b/StoneIsland/platforms/ios/www/js/lib/view/Scrollable.js new file mode 100644 index 00000000..7cd96f89 --- /dev/null +++ b/StoneIsland/platforms/ios/www/js/lib/view/Scrollable.js @@ -0,0 +1,20 @@ +var ScrollableView = View.extend({ + + events: { + "load img": "deferScrollToTop", + }, + + deferScrollToTop: function(){ + setTimeout(this.scrollToTop.bind(this), 0) + }, + + refreshScroller: function(){ + this.scroller.refresh() + }, + + scrollToTop: function(){ + this.scroller.refresh() + app.collection.scroller.scrollTo(0, 0) + }, + +})
\ No newline at end of file diff --git a/StoneIsland/platforms/ios/www/js/lib/view/Serializable.js b/StoneIsland/platforms/ios/www/js/lib/view/Serializable.js new file mode 100644 index 00000000..b1e095d3 --- /dev/null +++ b/StoneIsland/platforms/ios/www/js/lib/view/Serializable.js @@ -0,0 +1,144 @@ +var SerializableView = View.extend({ + + events: { + "change select": "update_select", + "focus input": "focus_input", + "submit form": "save", + }, + + preload: function(data){ + if (! data && sdk.env == "production") { return } + data = data || this.test_data + if (! data) { return } + this.load_data(data) + }, + + load_data: function(data){ + Object.keys(data).forEach(function(key){ + var value = data[key] + var $el = this.$("[name=" + key + "]") + + if ($el.attr("type") == "checkbox") { + $el.prop("checked", value) + } + else if ($el.prop("tagName") == "SELECT") { + $el.val( value ) + this.update_select({ currentTarget: $el }) + } + else { + $el.val( value ) + } + }.bind(this)) + }, + + serialize: function(){ + var fields = {} + this.$("input[name], select[name], textarea[name]").each( function(){ + if (this.type == "checkbox") { + if ($(this).prop("checked")) { + fields[this.name] = this.value + } + } + else { + fields[this.name] = this.value + } + }) + return fields + }, + + deserialize: function(data){ + this.$("input[name], textarea[name]").val("") + Object.keys(data).forEach(function(k){ + this.$("[" + k + "]").val(data[k]) + }) + }, + + focus_input: function(e){ + $(e.currentTarget).removeClass("error_hilite") + }, + + update_select: function(e){ + var $target = $(e.currentTarget), value = $target.val() + var label = $target.find("option").filter(function(){ return this.value === value }).html() + $target.parent().addClass("picked") + $target.parent().find("span").html(label) + }, + + validate: function(data, errors){ + var data = data || this.serialize() + var errors = errors || [] + var presence_msgs = this.validate_presence || {} + Object.keys(presence_msgs).forEach(function(k){ + if (! data[k]) errors.push( [ k, presence_msgs[k] ] ) + }) + this.validate_fields && this.validate_fields(data, errors) + this.cc && this.cc.validate(data, errors) + this.address && this.address.validate(data, errors) + return { errors: errors, data: data } + }, + + show_errors: function(errors){ + var msgs = [] + errors.forEach(function(e, i){ + if (i > 0) { return } + this.$("[name=" + e[0] + "]").addClass('error_hilite') + msgs.push(e[1]) + }.bind(this)) + this.$msg.html(msgs.join("<br>")) + this.$msg.addClass('alert-notice') + }, + + hide_errors: function(){ + this.$msg.removeClass('alert-notice') + this.$msg.html("") + }, + + finalize: function(data){ + return data + }, + + save: function(e){ + e && e.preventDefault() + + var valid = this.validate() + if (valid.errors.length) { + this.show_errors(valid.errors) + return + } + else { + this.hide_errors() + } + + var finalized_data = this.finalize(valid.data) + this.submit( finalized_data ) + }, + + submit: function(data){ + if (! data) { + return + } + app.curtain.show("loading") + this.action({ + data: data, + success: function(data){ + app.curtain.hide("loading") + this.success(data) + }.bind(this), + error: function(data){ + app.curtain.hide("loading") + this.error(data) + }.bind(this), + }) + }, + + success: function(data){ + console.log("SUCCESS", data) + }, + + error: function(data){ + console.log("FAIL", data) + }, + +}) + +var FormView = View.extend(SerializableView.prototype).extend(ScrollableView.prototype) diff --git a/StoneIsland/platforms/ios/www/js/lib/view/View.js b/StoneIsland/platforms/ios/www/js/lib/view/View.js new file mode 100644 index 00000000..41638ab7 --- /dev/null +++ b/StoneIsland/platforms/ios/www/js/lib/view/View.js @@ -0,0 +1,147 @@ +var View = (function($, _){ + + var View = function(options) { + this._id = _.uniqueId('view') + this.type = "view" + options || (options = {}); + _.extend(this, _.pick(options, viewOptions)) + this._ensureElement() + this.initialize.apply(this, arguments) + this.delegateEvents() + } + + var delegateEventSplitter = /^(\S+)\s*(.*)$/; + + var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'className', 'tagName', 'events']; + + _.extend(View.prototype, { + + // The default `tagName` of a View's element is `"div"`. + tagName: 'div', + + $: function(selector) { + return this.$el.find(selector); + }, + + initialize: function(){}, + + setElement: function(element, delegate) { + if (this.$el) this.undelegateEvents(); + this.$el = element instanceof $ ? element : $(element); + this.el = this.$el[0]; + if (delegate !== false) this.delegateEvents(); + return this; + }, + + // Set callbacks, where `this.events` is a hash of + // + // *{"event selector": "callback"}* + // + // { + // 'mousedown .title': 'edit', + // 'click .button': 'save', + // 'click .open': function(e) { ... } + // } + // + // pairs. Callbacks will be bound to the view, with `this` set properly. + // Uses event delegation for efficiency. + // Omitting the selector binds the event to `this.el`. + // This only works for delegate-able events: not `focus`, `blur`, and + // not `change`, `submit`, and `reset` in Internet Explorer. + delegateEvents: function(events) { + if (!(events || (events = _.result(this, 'events')))) return this; + this.undelegateEvents(); + for (var key in events) { + var method = events[key]; + if (!_.isFunction(method)) method = this[events[key]]; + if (!method) continue; + + var match = key.match(delegateEventSplitter); + var eventName = match[1], selector = match[2]; + method = _.bind(method, this); + eventName += '.delegateEvents' + this._id; + if (is_mobile) { + if (eventName === 'mouseenter' || eventName === 'mouseleave') { + continue + } +// if (eventName === 'click') { +// eventName = 'tap' +// } + } + if (selector === '') { + this.$el.on(eventName, method); + } else { + this.$el.on(eventName, selector, method); + } + } + return this; + }, + + // Clears all callbacks previously bound to the view with `delegateEvents`. + undelegateEvents: function() { + this.$el.off('.delegateEvents' + this._id); + return this; + }, + + // Ensure that the View has a DOM element to render into. + // If `this.el` is a string, pass it through `$()`, take the first + // matching element, and re-assign it to `el`. Otherwise, create + // an element from the `id`, `className` and `tagName` properties. + _ensureElement: function() { + this.setElement(_.result(this, 'el'), false); + }, + + preventDefault: function(e){ + e && e.preventDefault() + }, + + stopPropagation: function(e){ + e && e.stopPropagation() + }, + + }); + + + var extend = function(protoProps, staticProps) { + var staticProps = staticProps || {} + var parent = this; + var child; + var childEvents = {}; + + // The constructor function for the new subclass is either defined by you + // (the "constructor" property in your `extend` definition), or defaulted + // by us to simply call the parent's constructor. + if (protoProps && _.has(protoProps, 'constructor')) { + child = protoProps.constructor; + } else { + child = function(){ return parent.apply(this, arguments); }; + } + + // Extend events so we can subclass views + _.extend(childEvents, parent.prototype.events, protoProps.events) + + // Add static properties to the constructor function, if supplied. + _.extend(child, parent, staticProps); + + // Set the prototype chain to inherit from `parent`, without calling + // `parent`'s constructor function. + var Surrogate = function(){ this.constructor = child; }; + Surrogate.prototype = parent.prototype; + child.prototype = new Surrogate; + + // Add prototype properties (instance properties) to the subclass, + // if supplied. + if (protoProps) _.extend(child.prototype, protoProps); + + // Set a convenience property in case the parent's prototype is needed + // later. + child.prototype.__super__ = parent.prototype; + child.prototype.events = childEvents + + return child; + }; + + View.extend = extend; + + return View; +})(jQuery, _) |
