;(function($) { var fs2api = { //window.FS2 = { _URL: '/fs/fs2@api///?' + new Date().getTime(), _MARKER_CLASS_PREFIX: '.FS2_', _checkValidity: function(options, config) { if (typeof options !== 'object') throw 'options is not object'; for (var name in config) { var type, optional; if (typeof config[name] === 'object') { type = config[name]['type']; optional = config[name]['optional']; } else { type = config[name]; optional = false; } var value = options[name]; if (!value) { if (!optional) throw name + ' is required' } else if (typeof value !== type) { throw name + ' is not ' + type; } } }, _api: function(options) { var checker = { api: 'string', shopKey: 'string', ssl: { type: 'boolean', optional: true }, callback: 'function', error: { type: 'function', optional: true } }; if (options && options['ssl'] === true) { checker['ssldomain'] = 'string'; } else { checker['domain'] = 'string'; } // validate options this._checkValidity(options, checker); var url = FS2._URL.replace('', options.ssl ? 'https://' : 'http://') .replace('', options.ssl ? options.ssldomain : options.domain) .replace('', options.shopKey) .replace('', options.api); var dfDom = $.Deferred(); var dfAjax = $.Deferred(); $.when(dfAjax, dfDom).then(options.callback); $(function() { dfDom.resolve(); }); $.ajax({ type: options['method'] || 'GET', dataType: options['dataType'] || 'json', data: options['data'] || '', url: url, xhrFields: { withCredentials : true }, cache: true, crossDomain: true }) .done(function(data, textStatus, jqXHR) { dfAjax.resolve(data); }) .fail(function(jqXHR, textStatus, errorThrown) { if (typeof options.error === 'function') options.error(errorThrown); }); }, repeater: function(options) { var constants = { api: 'repeater', ssl: true, method: 'GET', dataType: 'json' }; var defaults = { callback: function(json) { var isGuest = json['member_id'] === 'guest'; // trigger guest/member callback function if (isGuest) { // for guest if (typeof options.guest === 'function') { var continuing = options.guest(json); if (continuing === false) return; } } else { // for member if (typeof options.member === 'function') { var continuing = options.member(json); if (continuing === false) return; } } // process words replacing var elements = $(FS2._MARKER_CLASS_PREFIX + constants['api']); var $div = $('
'); for (var key in json) { var upperedKey = key.toUpperCase() var target = ['$', upperedKey, '$'].join(''); var regexStr = ['\\$', upperedKey, '\\$'].join(''); var regexp = new RegExp(regexStr, 'g'); elements.each(function() { var value = $div.text(json[key]).html(); if (this.innerHTML.indexOf(target) >= 0) { this.innerHTML = this.innerHTML.replace(regexp, value); } if (this.className.indexOf(target) >= 0) { this.className = this.className.replace(regexp, value); } $(this).show(); }); } // trigger done callback function if (typeof options.done === 'function') { var continuing = options.done(json, isGuest); if (continuing === false) return; } } }; // merge settings var setting = $.extend(defaults, options, constants); var domain = setting['ssl'] === true ? setting['ssldomain'] : setting['domain']; if (domain !== document.domain) { // for cache by domain setting = $.extend(setting, {'data': {'!': document.domain}}); } // process api this._api(setting); }, addWishList: function(options) { var constants = { api: 'add_wish_list', ssl: ("https:" == location.protocol), method: 'POST', dataType: 'json' }; var defaults = { callback: function(json) { // check result var result = json['result']; if (result) { if (result === 'success') { // trigger done callback function if (typeof options.done === 'function') { options.done(); } } else if (result === 'no_login') { // if do not login, goto wish list entry page var loginUrl = json['login_url']; if (loginUrl) { location.href = loginUrl; } } else if (result === 'limit_over') { // if registered count is over, alert the message var message = json['message']; if (message) { alert(message); } } // other result do nothing } } }; // merge settings var setting = $.extend(defaults, options, constants); // process api this._api(setting); }, removeWishList: function(options) { var constants = { api: 'remove_wish_list', ssl: ("https:" == location.protocol), method: 'POST', dataType: 'json' }; var defaults = { callback: function(json) { // check result var result = json['result']; if (result && result === 'success') { // trigger done callback function if (typeof options.done === 'function') { options.done(); } } // other result do nothing } }; // merge settings var setting = $.extend(defaults, options, constants); // process api this._api(setting); }, saveGAClientId: function(gaClientId) { if(FS2.val.ga_saved || ! gaClientId){ return; } var constants = { api: 'save_ga_client_id', ssl: ("https:" == location.protocol), method: 'POST', dataType: 'json' }; var options = { shopKey: FS2.val.ga_shopKey, domain: FS2.val.ga_domain, ssldomain: FS2.val.ga_sslDomain, data: { client_id: gaClientId } } var defaults = { callback: function(json) { // check result var result = json['result']; if (result && result === 'success') { // trigger done callback function if (typeof options.done === 'function') { options.done(); } } // other result do nothing } }; // merge settings var setting = $.extend(defaults, options, constants); // process api this._api(setting); } }; window.FS2 = $.extend(fs2api, window.FS2); })(jQuery);