Created
October 19, 2016 19:07
-
-
Save angellromero/2d05a3a5ba5389ae8608cceba8846e28 to your computer and use it in GitHub Desktop.
Temp: Wishlist JS Snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var WishlistUtil = new Class({ | |
namespace: 'WishlistUtil', | |
/** | |
* Default option values | |
*/ | |
defaults: { | |
addUrl: '/my-account/saved-lists/add', | |
createUrl: '/my-account/create-list', | |
fetchUrl: '/my-account/saved-lists/lists', | |
strings: { | |
create: 'Create a Saved List', | |
listName: 'Enter new list name' | |
} | |
}, | |
initialize: function(options) { | |
this.opts = $.extend(this.defaults, options); | |
this.$document = $(document); | |
this._fetchWishLists(); | |
}, | |
open: function() { | |
}, | |
prompt: function(action, options) { | |
var _self = this; | |
switch(action) { | |
case 'create': | |
Gorilla.instance.prompt.open({ | |
title: this.opts.strings.create, | |
placeholder: this.opts.strings.listName, | |
confirmBtnLabel: this.opts.strings.create, | |
template: '#prompt--input-template', | |
onConfirm: function(formData) { | |
if(formData.length > 0) { | |
options.name = formData[0].value; | |
} | |
_self.create(options); | |
//_self.$addToSavedListFormListName.val(this.$inputValue); | |
//_self._postForm($el); | |
}, | |
onCancel: function() { | |
//_self.$el.Modal('hide'); | |
} | |
}); | |
break; | |
default: | |
break; | |
} | |
}, | |
create: function(options) { | |
// Dispatch Event to Create List | |
this._handleAjaxRequest(this.opts.addUrl, this._getParams(arguments[0]), function(data) { | |
this._triggerEvent('created', { | |
productCode: productCode | |
}); | |
}); | |
}, | |
add: function(options) { | |
this._handleAjaxRequest(this.opts.addUrl, this._getParams(arguments[0]), function(data) { | |
this._triggerEvent('added', { | |
}); | |
}); | |
}, | |
save: function() { | |
ss | |
}, | |
delete: function() { | |
}, | |
_fetchWishLists: function() { | |
var _self = this; | |
this._handleAjaxRequest(this.opts.fetchUrl, {}, function(data) { | |
// Dispatch Event | |
_self._triggerEvent('updated', { | |
list: data.responseObject | |
}); | |
}); | |
}, | |
_handleAjaxRequest: function(url, params, onComplete) { | |
var _self = this; | |
$.ajax({ | |
method: 'POST', | |
url: ACC.config.encodedContextPath + url, | |
data: $.param(params || {}), | |
beforeSend: function() { | |
//Gorilla.utilities.setLoadingState(true, $loader, $button); | |
} | |
}).fail(function(data, status) { | |
}).done(function(data) { | |
if(data.success) { | |
} else { | |
// Fail | |
//_self._handleErrorState.call(scope, data, $button); | |
} | |
if(typeof onComplete === 'function') { | |
onComplete(data); | |
} | |
// Gorilla.utilities.setLoadingState(false, $loader); | |
}); | |
}, | |
_getParams: function(obj) { | |
return { | |
name: obj.name || '', | |
productCode: obj.productCode || 0, | |
qty: obj.qty || 1, | |
cartType: obj.cartType || 'Standard' // Needed? - Only for addAllToCart. Makes sense | |
}; | |
}, | |
_triggerEvent: function(type, data) { | |
this.$document.trigger({ | |
type: type + '.' + this.namespace, | |
extra: data | |
}); | |
} | |
}); | |
var WishlistMenu2 = new Class ({ | |
namespace: 'WishlistMenu', | |
defaults: { | |
menuSelector: '.list-menu' | |
}, | |
initialize: function(options) { | |
var _self = this; | |
this.opts = $.extend(this.defaults, options); | |
this.$menu = $(this.opts.menuSelector); | |
this._mode = 'desktop'; | |
if(this.$menu.length > 0) { | |
Respond.to({ | |
'media' : 'screen and (max-width: 768px)', | |
'namespace' : _self.namespace, | |
'fallback' : 'else', | |
'if' : function() { | |
_self._mode = 'mobile'; | |
// Clean Up | |
if(_self.$menu.data('chosen')) { | |
_self.$menu.chosen('destroy'); | |
} | |
// Trigger Mobile Menu | |
_self.$menu.Modal(); | |
}, | |
'else' : function() { | |
_self._mode = 'desktop'; | |
_self.$menu.chosen({ | |
width: "100%" | |
}).on('change', _self._onChange.bind(_self.$menu)); | |
// Clean Up | |
if(_self.$menu.data('modal')) { | |
_self.$menu.Modal('unset'); | |
} | |
} | |
}); | |
} | |
this._bindEvents(); | |
}, | |
open: function(action) { | |
if(this._mode === 'desktop') { | |
} else if(this._mode === 'mobile') { | |
this.$menu.Modal('show'); | |
} | |
}, | |
_onChange: function(e) { | |
var $this = $(e.currentTarget); | |
var action = $this.find(':selected').data('action') || 'create', | |
options = { | |
productCode: $this.data('productCode'), | |
qty: $('#quantity-' + $this.data('productCode')).val() || 1 | |
}; | |
switch(action) { | |
case 'save': | |
Gorilla.instance.WishlistUtil.add(options); | |
break; | |
case 'create': | |
default: | |
$this.val('').trigger('chosen:updated'); | |
Gorilla.instance.WishlistUtil.prompt(action, options); | |
break; | |
} | |
}, | |
_bindEvents: function() { | |
var _self = this; | |
$(document).on('updated.WishlistUtil', function(e) { | |
// Respond To Handling | |
var $wishlistMenuTemplate = $('#list-menu__template'); | |
Gorilla.utilities.renderTemplate($wishlistMenuTemplate.html(), e.extra, _self.$menu); | |
_self.$menu.trigger('chosen:updated'); | |
}); | |
} | |
}); | |
// TODO: Move to a user restrictions class | |
if($('body').hasClass('logged-in')) { | |
Gorilla.instance.WishlistUtil = new WishlistUtil(); | |
new WishlistMenu2(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment