initial commit

This commit is contained in:
2021-12-10 12:03:04 +00:00
commit c46c7ddbf0
3643 changed files with 582794 additions and 0 deletions

View File

@ -0,0 +1,413 @@
/*!
* accounting.js v0.4.2
* Copyright 2014 Open Exchange Rates
*
* Freely distributable under the MIT license.
* Portions of accounting.js are inspired or borrowed from underscore.js
*
* Full details and documentation:
* http://openexchangerates.github.io/accounting.js/
*/
(function(root, undefined) {
/* --- Setup --- */
// Create the local library object, to be exported or referenced globally later
var lib = {};
// Current version
lib.version = '0.4.1';
/* --- Exposed settings --- */
// The library's settings configuration object. Contains default parameters for
// currency and number formatting
lib.settings = {
currency: {
symbol : "$", // default currency symbol is '$'
format : "%s%v", // controls output: %s = symbol, %v = value (can be object, see docs)
decimal : ".", // decimal point separator
thousand : ",", // thousands separator
precision : 2, // decimal places
grouping : 3 // digit grouping (not implemented yet)
},
number: {
precision : 0, // default precision on numbers is 0
grouping : 3, // digit grouping (not implemented yet)
thousand : ",",
decimal : "."
}
};
/* --- Internal Helper Methods --- */
// Store reference to possibly-available ECMAScript 5 methods for later
var nativeMap = Array.prototype.map,
nativeIsArray = Array.isArray,
toString = Object.prototype.toString;
/**
* Tests whether supplied parameter is a string
* from underscore.js
*/
function isString(obj) {
return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
}
/**
* Tests whether supplied parameter is a string
* from underscore.js, delegates to ECMA5's native Array.isArray
*/
function isArray(obj) {
return nativeIsArray ? nativeIsArray(obj) : toString.call(obj) === '[object Array]';
}
/**
* Tests whether supplied parameter is a true object
*/
function isObject(obj) {
return obj && toString.call(obj) === '[object Object]';
}
/**
* Extends an object with a defaults object, similar to underscore's _.defaults
*
* Used for abstracting parameter handling from API methods
*/
function defaults(object, defs) {
var key;
object = object || {};
defs = defs || {};
// Iterate over object non-prototype properties:
for (key in defs) {
if (defs.hasOwnProperty(key)) {
// Replace values with defaults only if undefined (allow empty/zero values):
if (object[key] == null) object[key] = defs[key];
}
}
return object;
}
/**
* Implementation of `Array.map()` for iteration loops
*
* Returns a new Array as a result of calling `iterator` on each array value.
* Defers to native Array.map if available
*/
function map(obj, iterator, context) {
var results = [], i, j;
if (!obj) return results;
// Use native .map method if it exists:
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
// Fallback for native .map:
for (i = 0, j = obj.length; i < j; i++ ) {
results[i] = iterator.call(context, obj[i], i, obj);
}
return results;
}
/**
* Check and normalise the value of precision (must be positive integer)
*/
function checkPrecision(val, base) {
val = Math.round(Math.abs(val));
return isNaN(val)? base : val;
}
/**
* Parses a format string or object and returns format obj for use in rendering
*
* `format` is either a string with the default (positive) format, or object
* containing `pos` (required), `neg` and `zero` values (or a function returning
* either a string or object)
*
* Either string or format.pos must contain "%v" (value) to be valid
*/
function checkCurrencyFormat(format) {
var defaults = lib.settings.currency.format;
// Allow function as format parameter (should return string or object):
if ( typeof format === "function" ) format = format();
// Format can be a string, in which case `value` ("%v") must be present:
if ( isString( format ) && format.match("%v") ) {
// Create and return positive, negative and zero formats:
return {
pos : format,
neg : format.replace("-", "").replace("%v", "-%v"),
zero : format
};
// If no format, or object is missing valid positive value, use defaults:
} else if ( !format || !format.pos || !format.pos.match("%v") ) {
// If defaults is a string, casts it to an object for faster checking next time:
return ( !isString( defaults ) ) ? defaults : lib.settings.currency.format = {
pos : defaults,
neg : defaults.replace("%v", "-%v"),
zero : defaults
};
}
// Otherwise, assume format was fine:
return format;
}
/* --- API Methods --- */
/**
* Takes a string/array of strings, removes all formatting/cruft and returns the raw float value
* Alias: `accounting.parse(string)`
*
* Decimal must be included in the regular expression to match floats (defaults to
* accounting.settings.number.decimal), so if the number uses a non-standard decimal
* separator, provide it as the second argument.
*
* Also matches bracketed negatives (eg. "$ (1.99)" => -1.99)
*
* Doesn't throw any errors (`NaN`s become 0) but this may change in future
*/
var unformat = lib.unformat = lib.parse = function(value, decimal) {
// Recursively unformat arrays:
if (isArray(value)) {
return map(value, function(val) {
return unformat(val, decimal);
});
}
// Fails silently (need decent errors):
value = value || 0;
// Return the value as-is if it's already a number:
if (typeof value === "number") return value;
// Default decimal point comes from settings, but could be set to eg. "," in opts:
decimal = decimal || lib.settings.number.decimal;
// Build regex to strip out everything except digits, decimal point and minus sign:
var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]),
unformatted = parseFloat(
("" + value)
.replace(/\((.*)\)/, "-$1") // replace bracketed values with negatives
.replace(regex, '') // strip out any cruft
.replace(decimal, '.') // make sure decimal point is standard
);
// This will fail silently which may cause trouble, let's wait and see:
return !isNaN(unformatted) ? unformatted : 0;
};
/**
* Implementation of toFixed() that treats floats more like decimals
*
* Fixes binary rounding issues (eg. (0.615).toFixed(2) === "0.61") that present
* problems for accounting- and finance-related software.
*/
var toFixed = lib.toFixed = function(value, precision) {
precision = checkPrecision(precision, lib.settings.number.precision);
var power = Math.pow(10, precision);
// Multiply up by precision, round accurately, then divide and use native toFixed():
return (Math.round(lib.unformat(value) * power) / power).toFixed(precision);
};
/**
* Format a number, with comma-separated thousands and custom precision/decimal places
* Alias: `accounting.format()`
*
* Localise by overriding the precision and thousand / decimal separators
* 2nd parameter `precision` can be an object matching `settings.number`
*/
var formatNumber = lib.formatNumber = lib.format = function(number, precision, thousand, decimal) {
// Resursively format arrays:
if (isArray(number)) {
return map(number, function(val) {
return formatNumber(val, precision, thousand, decimal);
});
}
// Clean up number:
number = unformat(number);
// Build options object from second param (if object) or all params, extending defaults:
var opts = defaults(
(isObject(precision) ? precision : {
precision : precision,
thousand : thousand,
decimal : decimal
}),
lib.settings.number
),
// Clean up precision
usePrecision = checkPrecision(opts.precision),
// Do some calc:
negative = number < 0 ? "-" : "",
base = parseInt(toFixed(Math.abs(number || 0), usePrecision), 10) + "",
mod = base.length > 3 ? base.length % 3 : 0;
// Format the number:
return negative + (mod ? base.substr(0, mod) + opts.thousand : "") + base.substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + opts.thousand) + (usePrecision ? opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] : "");
};
/**
* Format a number into currency
*
* Usage: accounting.formatMoney(number, symbol, precision, thousandsSep, decimalSep, format)
* defaults: (0, "$", 2, ",", ".", "%s%v")
*
* Localise by overriding the symbol, precision, thousand / decimal separators and format
* Second param can be an object matching `settings.currency` which is the easiest way.
*
* To do: tidy up the parameters
*/
var formatMoney = lib.formatMoney = function(number, symbol, precision, thousand, decimal, format) {
// Resursively format arrays:
if (isArray(number)) {
return map(number, function(val){
return formatMoney(val, symbol, precision, thousand, decimal, format);
});
}
// Clean up number:
number = unformat(number);
// Build options object from second param (if object) or all params, extending defaults:
var opts = defaults(
(isObject(symbol) ? symbol : {
symbol : symbol,
precision : precision,
thousand : thousand,
decimal : decimal,
format : format
}),
lib.settings.currency
),
// Check format (returns object with pos, neg and zero):
formats = checkCurrencyFormat(opts.format),
// Choose which format to use for this value:
useFormat = number > 0 ? formats.pos : number < 0 ? formats.neg : formats.zero;
// Return with currency symbol added:
return useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(number), checkPrecision(opts.precision), opts.thousand, opts.decimal));
};
/**
* Format a list of numbers into an accounting column, padding with whitespace
* to line up currency symbols, thousand separators and decimals places
*
* List should be an array of numbers
* Second parameter can be an object containing keys that match the params
*
* Returns array of accouting-formatted number strings of same length
*
* NB: `white-space:pre` CSS rule is required on the list container to prevent
* browsers from collapsing the whitespace in the output strings.
*/
lib.formatColumn = function(list, symbol, precision, thousand, decimal, format) {
if (!list) return [];
// Build options object from second param (if object) or all params, extending defaults:
var opts = defaults(
(isObject(symbol) ? symbol : {
symbol : symbol,
precision : precision,
thousand : thousand,
decimal : decimal,
format : format
}),
lib.settings.currency
),
// Check format (returns object with pos, neg and zero), only need pos for now:
formats = checkCurrencyFormat(opts.format),
// Whether to pad at start of string or after currency symbol:
padAfterSymbol = formats.pos.indexOf("%s") < formats.pos.indexOf("%v") ? true : false,
// Store value for the length of the longest string in the column:
maxLength = 0,
// Format the list according to options, store the length of the longest string:
formatted = map(list, function(val, i) {
if (isArray(val)) {
// Recursively format columns if list is a multi-dimensional array:
return lib.formatColumn(val, opts);
} else {
// Clean up the value
val = unformat(val);
// Choose which format to use for this value (pos, neg or zero):
var useFormat = val > 0 ? formats.pos : val < 0 ? formats.neg : formats.zero,
// Format this value, push into formatted list and save the length:
fVal = useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(val), checkPrecision(opts.precision), opts.thousand, opts.decimal));
if (fVal.length > maxLength) maxLength = fVal.length;
return fVal;
}
});
// Pad each number in the list and send back the column of numbers:
return map(formatted, function(val, i) {
// Only if this is a string (not a nested array, which would have already been padded):
if (isString(val) && val.length < maxLength) {
// Depending on symbol position, pad after symbol or at index 0:
return padAfterSymbol ? val.replace(opts.symbol, opts.symbol+(new Array(maxLength - val.length + 1).join(" "))) : (new Array(maxLength - val.length + 1).join(" ")) + val;
}
return val;
});
};
/* --- Module Definition --- */
// Export accounting for CommonJS. If being loaded as an AMD module, define it as such.
// Otherwise, just add `accounting` to the global object
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = lib;
}
exports.accounting = lib;
} else if (typeof define === 'function' && define.amd) {
// Return the library as an AMD module:
define([], function() {
return lib;
});
} else {
// Use accounting.noConflict to restore `accounting` back to its original value.
// Returns a reference to the library's `accounting` object;
// e.g. `var numbers = accounting.noConflict();`
lib.noConflict = (function(oldAccounting) {
return function() {
// Reset the value of the root's `accounting` variable:
root.accounting = oldAccounting;
// Delete the noConflict method:
lib.noConflict = undefined;
// Return reference to the library to re-assign it:
return lib;
};
})(root.accounting);
// Declare `fx` on the root (global/window) object:
root['accounting'] = lib;
}
// Root will be `window` in browser or `global` on the server:
}(this));

11
assets/js/accounting/accounting.min.js vendored Normal file
View File

@ -0,0 +1,11 @@
/*!
* accounting.js v0.4.2
* Copyright 2014 Open Exchange Rates
*
* Freely distributable under the MIT license.
* Portions of accounting.js are inspired or borrowed from underscore.js
*
* Full details and documentation:
* http://openexchangerates.github.io/accounting.js/
*/
!function(n){var f={version:"0.4.1",settings:{currency:{symbol:"$",format:"%s%v",decimal:".",thousand:",",precision:2,grouping:3},number:{precision:0,grouping:3,thousand:",",decimal:"."}}},i=Array.prototype.map,r=Array.isArray,e=Object.prototype.toString;function p(n){return""===n||n&&n.charCodeAt&&n.substr}function l(n){return r?r(n):"[object Array]"===e.call(n)}function m(n){return n&&"[object Object]"===e.call(n)}function d(n,r){for(var e in n=n||{},r=r||{})r.hasOwnProperty(e)&&null==n[e]&&(n[e]=r[e]);return n}function g(n,r,e){var t,o,a=[];if(!n)return a;if(i&&n.map===i)return n.map(r,e);for(t=0,o=n.length;t<o;t++)a[t]=r.call(e,n[t],t,n);return a}function h(n,r){return n=Math.round(Math.abs(n)),isNaN(n)?r:n}function y(n){var r=f.settings.currency.format;return"function"==typeof n&&(n=n()),p(n)&&n.match("%v")?{pos:n,neg:n.replace("-","").replace("%v","-%v"),zero:n}:n&&n.pos&&n.pos.match("%v")?n:p(r)?f.settings.currency.format={pos:r,neg:r.replace("%v","-%v"),zero:r}:r}var t,b=f.unformat=f.parse=function(n,r){if(l(n))return g(n,function(n){return b(n,r)});if("number"==typeof(n=n||0))return n;r=r||f.settings.number.decimal;var e=new RegExp("[^0-9-"+r+"]",["g"]),e=parseFloat((""+n).replace(/\((.*)\)/,"-$1").replace(e,"").replace(r,"."));return isNaN(e)?0:e},s=f.toFixed=function(n,r){r=h(r,f.settings.number.precision);var e=Math.pow(10,r);return(Math.round(f.unformat(n)*e)/e).toFixed(r)},v=f.formatNumber=f.format=function(n,r,e,t){if(l(n))return g(n,function(n){return v(n,r,e,t)});n=b(n);var o=d(m(r)?r:{precision:r,thousand:e,decimal:t},f.settings.number),a=h(o.precision),i=n<0?"-":"",u=parseInt(s(Math.abs(n||0),a),10)+"",c=3<u.length?u.length%3:0;return i+(c?u.substr(0,c)+o.thousand:"")+u.substr(c).replace(/(\d{3})(?=\d)/g,"$1"+o.thousand)+(a?o.decimal+s(Math.abs(n),a).split(".")[1]:"")},c=f.formatMoney=function(n,r,e,t,o,a){if(l(n))return g(n,function(n){return c(n,r,e,t,o,a)});n=b(n);var i=d(m(r)?r:{symbol:r,precision:e,thousand:t,decimal:o,format:a},f.settings.currency),u=y(i.format);return(0<n?u.pos:n<0?u.neg:u.zero).replace("%s",i.symbol).replace("%v",v(Math.abs(n),h(i.precision),i.thousand,i.decimal))};f.formatColumn=function(n,r,e,t,o,a){if(!n)return[];var i=d(m(r)?r:{symbol:r,precision:e,thousand:t,decimal:o,format:a},f.settings.currency),u=y(i.format),c=u.pos.indexOf("%s")<u.pos.indexOf("%v"),s=0,n=g(n,function(n,r){if(l(n))return f.formatColumn(n,i);n=(0<(n=b(n))?u.pos:n<0?u.neg:u.zero).replace("%s",i.symbol).replace("%v",v(Math.abs(n),h(i.precision),i.thousand,i.decimal));return n.length>s&&(s=n.length),n});return g(n,function(n,r){return p(n)&&n.length<s?c?n.replace(i.symbol,i.symbol+new Array(s-n.length+1).join(" ")):new Array(s-n.length+1).join(" ")+n:n})},"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=f),exports.accounting=f):"function"==typeof define&&define.amd?define([],function(){return f}):(f.noConflict=(t=n.accounting,function(){return n.accounting=t,f.noConflict=void 0,f}),n.accounting=f)}(this);

158
assets/js/admin/api-keys.js Normal file
View File

@ -0,0 +1,158 @@
/*global jQuery, Backbone, _, woocommerce_admin_api_keys, wcSetClipboard, wcClearClipboard */
(function( $ ) {
var APIView = Backbone.View.extend({
/**
* Element
*
* @param {Object} '#key-fields'
*/
el: $( '#key-fields' ),
/**
* Events
*
* @type {Object}
*/
events: {
'click input#update_api_key': 'saveKey'
},
/**
* Initialize actions
*/
initialize: function(){
_.bindAll( this, 'saveKey' );
},
/**
* Init jQuery.BlockUI
*/
block: function() {
$( this.el ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
/**
* Remove jQuery.BlockUI
*/
unblock: function() {
$( this.el ).unblock();
},
/**
* Init TipTip
*/
initTipTip: function( css_class ) {
$( document.body )
.on( 'click', css_class, function( evt ) {
evt.preventDefault();
if ( ! document.queryCommandSupported( 'copy' ) ) {
$( css_class ).parent().find( 'input' ).trigger( 'focus' ).trigger( 'select' );
$( '#copy-error' ).text( woocommerce_admin_api_keys.clipboard_failed );
} else {
$( '#copy-error' ).text( '' );
wcClearClipboard();
wcSetClipboard( $( this ).prev( 'input' ).val().trim(), $( css_class ) );
}
} )
.on( 'aftercopy', css_class, function() {
$( '#copy-error' ).text( '' );
$( css_class ).tipTip( {
'attribute': 'data-tip',
'activation': 'focus',
'fadeIn': 50,
'fadeOut': 50,
'delay': 0
} ).trigger( 'focus' );
} )
.on( 'aftercopyerror', css_class, function() {
$( css_class ).parent().find( 'input' ).trigger( 'focus' ).trigger( 'select' );
$( '#copy-error' ).text( woocommerce_admin_api_keys.clipboard_failed );
} );
},
/**
* Create qrcode
*
* @param {string} consumer_key
* @param {string} consumer_secret
*/
createQRCode: function( consumer_key, consumer_secret ) {
$( '#keys-qrcode' ).qrcode({
text: consumer_key + '|' + consumer_secret,
width: 120,
height: 120
});
},
/**
* Save API Key using ajax
*
* @param {Object} e
*/
saveKey: function( e ) {
e.preventDefault();
var self = this;
self.block();
Backbone.ajax({
method: 'POST',
dataType: 'json',
url: woocommerce_admin_api_keys.ajax_url,
data: {
action: 'woocommerce_update_api_key',
security: woocommerce_admin_api_keys.update_api_nonce,
key_id: $( '#key_id', self.el ).val(),
description: $( '#key_description', self.el ).val(),
user: $( '#key_user', self.el ).val(),
permissions: $( '#key_permissions', self.el ).val()
},
success: function( response ) {
$( '.wc-api-message', self.el ).remove();
if ( response.success ) {
var data = response.data;
$( 'h2, h3', self.el ).first().append( '<div class="wc-api-message updated"><p>' + data.message + '</p></div>' );
if ( 0 < data.consumer_key.length && 0 < data.consumer_secret.length ) {
$( '#api-keys-options', self.el ).remove();
$( 'p.submit', self.el ).empty().append( data.revoke_url );
var template = wp.template( 'api-keys-template' );
$( 'p.submit', self.el ).before( template({
consumer_key: data.consumer_key,
consumer_secret: data.consumer_secret
}) );
self.createQRCode( data.consumer_key, data.consumer_secret );
self.initTipTip( '.copy-key' );
self.initTipTip( '.copy-secret' );
} else {
$( '#key_description', self.el ).val( data.description );
$( '#key_user', self.el ).val( data.user_id );
$( '#key_permissions', self.el ).val( data.permissions );
}
} else {
$( 'h2, h3', self.el )
.first()
.append( '<div class="wc-api-message error"><p>' + response.data.message + '</p></div>' );
}
self.unblock();
}
});
}
});
new APIView();
})( jQuery );

1
assets/js/admin/api-keys.min.js vendored Normal file
View File

@ -0,0 +1 @@
!function(r){new(Backbone.View.extend({el:r("#key-fields"),events:{"click input#update_api_key":"saveKey"},initialize:function(){_.bindAll(this,"saveKey")},block:function(){r(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){r(this.el).unblock()},initTipTip:function(i){r(document.body).on("click",i,function(e){e.preventDefault(),document.queryCommandSupported("copy")?(r("#copy-error").text(""),wcClearClipboard(),wcSetClipboard(r(this).prev("input").val().trim(),r(i))):(r(i).parent().find("input").trigger("focus").trigger("select"),r("#copy-error").text(woocommerce_admin_api_keys.clipboard_failed))}).on("aftercopy",i,function(){r("#copy-error").text(""),r(i).tipTip({attribute:"data-tip",activation:"focus",fadeIn:50,fadeOut:50,delay:0}).trigger("focus")}).on("aftercopyerror",i,function(){r(i).parent().find("input").trigger("focus").trigger("select"),r("#copy-error").text(woocommerce_admin_api_keys.clipboard_failed)})},createQRCode:function(e,i){r("#keys-qrcode").qrcode({text:e+"|"+i,width:120,height:120})},saveKey:function(e){e.preventDefault();var o=this;o.block(),Backbone.ajax({method:"POST",dataType:"json",url:woocommerce_admin_api_keys.ajax_url,data:{action:"woocommerce_update_api_key",security:woocommerce_admin_api_keys.update_api_nonce,key_id:r("#key_id",o.el).val(),description:r("#key_description",o.el).val(),user:r("#key_user",o.el).val(),permissions:r("#key_permissions",o.el).val()},success:function(e){var i,t;r(".wc-api-message",o.el).remove(),e.success?(i=e.data,r("h2, h3",o.el).first().append('<div class="wc-api-message updated"><p>'+i.message+"</p></div>"),0<i.consumer_key.length&&0<i.consumer_secret.length?(r("#api-keys-options",o.el).remove(),r("p.submit",o.el).empty().append(i.revoke_url),t=wp.template("api-keys-template"),r("p.submit",o.el).before(t({consumer_key:i.consumer_key,consumer_secret:i.consumer_secret})),o.createQRCode(i.consumer_key,i.consumer_secret),o.initTipTip(".copy-key"),o.initTipTip(".copy-secret")):(r("#key_description",o.el).val(i.description),r("#key_user",o.el).val(i.user_id),r("#key_permissions",o.el).val(i.permissions))):r("h2, h3",o.el).first().append('<div class="wc-api-message error"><p>'+e.data.message+"</p></div>"),o.unblock()}})}}))}(jQuery);

View File

@ -0,0 +1,147 @@
/*global jQuery, Backbone, _ */
( function( $, Backbone, _ ) {
'use strict';
/**
* WooCommerce Backbone Modal plugin
*
* @param {object} options
*/
$.fn.WCBackboneModal = function( options ) {
return this.each( function() {
( new $.WCBackboneModal( $( this ), options ) );
});
};
/**
* Initialize the Backbone Modal
*
* @param {object} element [description]
* @param {object} options [description]
*/
$.WCBackboneModal = function( element, options ) {
// Set settings
var settings = $.extend( {}, $.WCBackboneModal.defaultOptions, options );
if ( settings.template ) {
new $.WCBackboneModal.View({
target: settings.template,
string: settings.variable
});
}
};
/**
* Set default options
*
* @type {object}
*/
$.WCBackboneModal.defaultOptions = {
template: '',
variable: {}
};
/**
* Create the Backbone Modal
*
* @return {null}
*/
$.WCBackboneModal.View = Backbone.View.extend({
tagName: 'div',
id: 'wc-backbone-modal-dialog',
_target: undefined,
_string: undefined,
events: {
'click .modal-close': 'closeButton',
'click #btn-ok' : 'addButton',
'touchstart #btn-ok': 'addButton',
'keydown' : 'keyboardActions'
},
resizeContent: function() {
var $content = $( '.wc-backbone-modal-content' ).find( 'article' );
var max_h = $( window ).height() * 0.75;
$content.css({
'max-height': max_h + 'px'
});
},
initialize: function( data ) {
var view = this;
this._target = data.target;
this._string = data.string;
_.bindAll( this, 'render' );
this.render();
$( window ).on( 'resize', function() {
view.resizeContent();
});
},
render: function() {
var template = wp.template( this._target );
this.$el.append(
template( this._string )
);
$( document.body ).css({
'overflow': 'hidden'
}).append( this.$el );
this.resizeContent();
this.$( '.wc-backbone-modal-content' ).attr( 'tabindex' , '0' ).trigger( 'focus' );
$( document.body ).trigger( 'init_tooltips' );
$( document.body ).trigger( 'wc_backbone_modal_loaded', this._target );
},
closeButton: function( e ) {
e.preventDefault();
$( document.body ).trigger( 'wc_backbone_modal_before_remove', this._target );
this.undelegateEvents();
$( document ).off( 'focusin' );
$( document.body ).css({
'overflow': 'auto'
});
this.remove();
$( document.body ).trigger( 'wc_backbone_modal_removed', this._target );
},
addButton: function( e ) {
$( document.body ).trigger( 'wc_backbone_modal_response', [ this._target, this.getFormData() ] );
this.closeButton( e );
},
getFormData: function() {
var data = {};
$( document.body ).trigger( 'wc_backbone_modal_before_update', this._target );
$.each( $( 'form', this.$el ).serializeArray(), function( index, item ) {
if ( item.name.indexOf( '[]' ) !== -1 ) {
item.name = item.name.replace( '[]', '' );
data[ item.name ] = $.makeArray( data[ item.name ] );
data[ item.name ].push( item.value );
} else {
data[ item.name ] = item.value;
}
});
return data;
},
keyboardActions: function( e ) {
var button = e.keyCode || e.which;
// Enter key
if (
13 === button &&
! ( e.target.tagName && ( e.target.tagName.toLowerCase() === 'input' || e.target.tagName.toLowerCase() === 'textarea' ) )
) {
this.addButton( e );
}
// ESC key
if ( 27 === button ) {
this.closeButton( e );
}
}
});
}( jQuery, Backbone, _ ));

1
assets/js/admin/backbone-modal.min.js vendored Normal file
View File

@ -0,0 +1 @@
!function(o,e,n){"use strict";o.fn.WCBackboneModal=function(e){return this.each(function(){new o.WCBackboneModal(o(this),e)})},o.WCBackboneModal=function(e,t){t=o.extend({},o.WCBackboneModal.defaultOptions,t);t.template&&new o.WCBackboneModal.View({target:t.template,string:t.variable})},o.WCBackboneModal.defaultOptions={template:"",variable:{}},o.WCBackboneModal.View=e.View.extend({tagName:"div",id:"wc-backbone-modal-dialog",_target:undefined,_string:undefined,events:{"click .modal-close":"closeButton","click #btn-ok":"addButton","touchstart #btn-ok":"addButton",keydown:"keyboardActions"},resizeContent:function(){var e=o(".wc-backbone-modal-content").find("article"),t=.75*o(window).height();e.css({"max-height":t+"px"})},initialize:function(e){var t=this;this._target=e.target,this._string=e.string,n.bindAll(this,"render"),this.render(),o(window).on("resize",function(){t.resizeContent()})},render:function(){var e=wp.template(this._target);this.$el.append(e(this._string)),o(document.body).css({overflow:"hidden"}).append(this.$el),this.resizeContent(),this.$(".wc-backbone-modal-content").attr("tabindex","0").trigger("focus"),o(document.body).trigger("init_tooltips"),o(document.body).trigger("wc_backbone_modal_loaded",this._target)},closeButton:function(e){e.preventDefault(),o(document.body).trigger("wc_backbone_modal_before_remove",this._target),this.undelegateEvents(),o(document).off("focusin"),o(document.body).css({overflow:"auto"}),this.remove(),o(document.body).trigger("wc_backbone_modal_removed",this._target)},addButton:function(e){o(document.body).trigger("wc_backbone_modal_response",[this._target,this.getFormData()]),this.closeButton(e)},getFormData:function(){var n={};return o(document.body).trigger("wc_backbone_modal_before_update",this._target),o.each(o("form",this.$el).serializeArray(),function(e,t){-1!==t.name.indexOf("[]")?(t.name=t.name.replace("[]",""),n[t.name]=o.makeArray(n[t.name]),n[t.name].push(t.value)):n[t.name]=t.value}),n},keyboardActions:function(e){var t=e.keyCode||e.which;13!==t||e.target.tagName&&("input"===e.target.tagName.toLowerCase()||"textarea"===e.target.tagName.toLowerCase())||this.addButton(e),27===t&&this.closeButton(e)}})}(jQuery,Backbone,_);

View File

@ -0,0 +1,450 @@
/* global marketplace_suggestions, ajaxurl, Cookies */
( function( $, marketplace_suggestions, ajaxurl ) {
$( function() {
if ( 'undefined' === typeof marketplace_suggestions ) {
return;
}
// Stand-in wcTracks.recordEvent in case tracks is not available (for any reason).
window.wcTracks = window.wcTracks || {};
window.wcTracks.recordEvent = window.wcTracks.recordEvent || function() { };
// Tracks events sent in this file:
// - marketplace_suggestion_displayed
// - marketplace_suggestion_clicked
// - marketplace_suggestion_dismissed
// All are prefixed by {WC_Tracks::PREFIX}.
// All have one property for `suggestionSlug`, to identify the specific suggestion message.
// Dismiss the specified suggestion from the UI, and save the dismissal in settings.
function dismissSuggestion( context, product, promoted, url, suggestionSlug ) {
// hide the suggestion in the UI
var selector = '[data-suggestion-slug=' + suggestionSlug + ']';
$( selector ).fadeOut( function() {
$( this ).remove();
tidyProductEditMetabox();
} );
// save dismissal in user settings
jQuery.post(
ajaxurl,
{
'action': 'woocommerce_add_dismissed_marketplace_suggestion',
'_wpnonce': marketplace_suggestions.dismiss_suggestion_nonce,
'slug': suggestionSlug
}
);
// if this is a high-use area, delay new suggestion that area for a short while
var highUseSuggestionContexts = [ 'products-list-inline' ];
if ( _.contains( highUseSuggestionContexts, context ) ) {
// snooze suggestions in that area for 2 days
var contextSnoozeCookie = 'woocommerce_snooze_suggestions__' + context;
Cookies.set( contextSnoozeCookie, 'true', { expires: 2 } );
// keep track of how often this area gets dismissed in a cookie
var contextDismissalCountCookie = 'woocommerce_dismissed_suggestions__' + context;
var previousDismissalsInThisContext = parseInt( Cookies.get( contextDismissalCountCookie ), 10 ) || 0;
Cookies.set( contextDismissalCountCookie, previousDismissalsInThisContext + 1, { expires: 31 } );
}
window.wcTracks.recordEvent( 'marketplace_suggestion_dismissed', {
suggestion_slug: suggestionSlug,
context: context,
product: product || '',
promoted: promoted || '',
target: url || ''
} );
}
// Render DOM element for suggestion dismiss button.
function renderDismissButton( context, product, promoted, url, suggestionSlug ) {
var dismissButton = document.createElement( 'a' );
dismissButton.classList.add( 'suggestion-dismiss' );
dismissButton.setAttribute( 'title', marketplace_suggestions.i18n_marketplace_suggestions_dismiss_tooltip );
dismissButton.setAttribute( 'href', '#' );
dismissButton.onclick = function( event ) {
event.preventDefault();
dismissSuggestion( context, product, promoted, url, suggestionSlug );
};
return dismissButton;
}
function addURLParameters( context, url ) {
var urlParams = marketplace_suggestions.in_app_purchase_params;
urlParams.utm_source = 'unknown';
urlParams.utm_campaign = 'marketplacesuggestions';
urlParams.utm_medium = 'product';
var sourceContextMap = {
'productstable': [
'products-list-inline'
],
'productsempty': [
'products-list-empty-header',
'products-list-empty-footer',
'products-list-empty-body'
],
'ordersempty': [
'orders-list-empty-header',
'orders-list-empty-footer',
'orders-list-empty-body'
],
'editproduct': [
'product-edit-meta-tab-header',
'product-edit-meta-tab-footer',
'product-edit-meta-tab-body'
]
};
var utmSource = _.findKey( sourceContextMap, function( sourceInfo ) {
return _.contains( sourceInfo, context );
} );
if ( utmSource ) {
urlParams.utm_source = utmSource;
}
return url + '?' + jQuery.param( urlParams );
}
// Render DOM element for suggestion linkout, optionally with button style.
function renderLinkout( context, product, promoted, slug, url, text, isButton ) {
var linkoutButton = document.createElement( 'a' );
var utmUrl = addURLParameters( context, url );
linkoutButton.setAttribute( 'href', utmUrl );
// By default, CTA links should open in same tab (and feel integrated with Woo).
// Exception: when editing products, use new tab. User may have product edits
// that need to be saved.
var newTabContexts = [
'product-edit-meta-tab-header',
'product-edit-meta-tab-footer',
'product-edit-meta-tab-body',
'products-list-empty-footer'
];
if ( _.includes( newTabContexts, context ) ) {
linkoutButton.setAttribute( 'target', 'blank' );
}
linkoutButton.textContent = text;
linkoutButton.onclick = function() {
window.wcTracks.recordEvent( 'marketplace_suggestion_clicked', {
suggestion_slug: slug,
context: context,
product: product || '',
promoted: promoted || '',
target: url || ''
} );
};
if ( isButton ) {
linkoutButton.classList.add( 'button' );
} else {
linkoutButton.classList.add( 'linkout' );
var linkoutIcon = document.createElement( 'span' );
linkoutIcon.classList.add( 'dashicons', 'dashicons-external' );
linkoutButton.appendChild( linkoutIcon );
}
return linkoutButton;
}
// Render DOM element for suggestion icon image.
function renderSuggestionIcon( iconUrl ) {
if ( ! iconUrl ) {
return null;
}
var image = document.createElement( 'img' );
image.src = iconUrl;
image.classList.add( 'marketplace-suggestion-icon' );
return image;
}
// Render DOM elements for suggestion content.
function renderSuggestionContent( slug, title, copy ) {
var container = document.createElement( 'div' );
container.classList.add( 'marketplace-suggestion-container-content' );
if ( title ) {
var titleHeading = document.createElement( 'h4' );
titleHeading.textContent = title;
container.appendChild( titleHeading );
}
if ( copy ) {
var body = document.createElement( 'p' );
body.textContent = copy;
container.appendChild( body );
}
// Conditionally add in a Manage suggestions link to product edit
// metabox footer (based on suggestion slug).
var slugsWithManage = [
'product-edit-empty-footer-browse-all',
'product-edit-meta-tab-footer-browse-all'
];
if ( -1 !== slugsWithManage.indexOf( slug ) ) {
container.classList.add( 'has-manage-link' );
var manageSuggestionsLink = document.createElement( 'a' );
manageSuggestionsLink.classList.add( 'marketplace-suggestion-manage-link', 'linkout' );
manageSuggestionsLink.setAttribute(
'href',
marketplace_suggestions.manage_suggestions_url
);
manageSuggestionsLink.textContent = marketplace_suggestions.i18n_marketplace_suggestions_manage_suggestions;
container.appendChild( manageSuggestionsLink );
}
return container;
}
// Render DOM elements for suggestion call-to-action button or link with dismiss 'x'.
function renderSuggestionCTA( context, product, promoted, slug, url, linkText, linkIsButton, allowDismiss ) {
var container = document.createElement( 'div' );
if ( ! linkText ) {
linkText = marketplace_suggestions.i18n_marketplace_suggestions_default_cta;
}
container.classList.add( 'marketplace-suggestion-container-cta' );
if ( url && linkText ) {
var linkoutElement = renderLinkout( context, product, promoted, slug, url, linkText, linkIsButton );
container.appendChild( linkoutElement );
}
if ( allowDismiss ) {
container.appendChild( renderDismissButton( context, product, promoted, url, slug ) );
}
return container;
}
// Render a "list item" style suggestion.
// These are used in onboarding style contexts, e.g. products list empty state.
function renderListItem( context, product, promoted, slug, iconUrl, title, copy, url, linkText, linkIsButton, allowDismiss ) {
var container = document.createElement( 'div' );
container.classList.add( 'marketplace-suggestion-container' );
container.dataset.suggestionSlug = slug;
var icon = renderSuggestionIcon( iconUrl );
if ( icon ) {
container.appendChild( icon );
}
container.appendChild(
renderSuggestionContent( slug, title, copy )
);
container.appendChild(
renderSuggestionCTA( context, product, promoted, slug, url, linkText, linkIsButton, allowDismiss )
);
return container;
}
// Filter suggestion data to remove less-relevant suggestions.
function getRelevantPromotions( marketplaceSuggestionsApiData, displayContext ) {
// select based on display context
var promos = _.filter( marketplaceSuggestionsApiData, function( promo ) {
if ( _.isArray( promo.context ) ) {
return _.contains( promo.context, displayContext );
}
return ( displayContext === promo.context );
} );
// hide promos the user has dismissed
promos = _.filter( promos, function( promo ) {
return ! _.contains( marketplace_suggestions.dismissed_suggestions, promo.slug );
} );
// hide promos for things the user already has installed
promos = _.filter( promos, function( promo ) {
return ! _.contains( marketplace_suggestions.active_plugins, promo.product );
} );
// hide promos that are not applicable based on user's installed extensions
promos = _.filter( promos, function( promo ) {
if ( ! promo['show-if-active'] ) {
// this promotion is relevant to all
return true;
}
// if the user has any of the prerequisites, show the promo
return ( _.intersection( marketplace_suggestions.active_plugins, promo['show-if-active'] ).length > 0 );
} );
return promos;
}
// Show and hide page elements dependent on suggestion state.
function hidePageElementsForSuggestionState( usedSuggestionsContexts ) {
var showingEmptyStateSuggestions = _.intersection(
usedSuggestionsContexts,
[ 'products-list-empty-body', 'orders-list-empty-body' ]
).length > 0;
// Streamline onboarding UI if we're in 'empty state' welcome mode.
if ( showingEmptyStateSuggestions ) {
$( '#screen-meta-links' ).hide();
$( '#wpfooter' ).hide();
}
// Hide the header & footer, they don't make sense without specific promotion content
if ( ! showingEmptyStateSuggestions ) {
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="products-list-empty-header"]' ).hide();
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="products-list-empty-footer"]' ).hide();
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="orders-list-empty-header"]' ).hide();
$( '.marketplace-suggestions-container[data-marketplace-suggestions-context="orders-list-empty-footer"]' ).hide();
}
}
// Streamline the product edit suggestions tab dependent on what's visible.
function tidyProductEditMetabox() {
var productMetaboxSuggestions = $(
'.marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-body"]'
).children();
if ( 0 >= productMetaboxSuggestions.length ) {
var metaboxSuggestionsUISelector =
'.marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-body"]';
metaboxSuggestionsUISelector +=
', .marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-header"]';
metaboxSuggestionsUISelector +=
', .marketplace-suggestions-container[data-marketplace-suggestions-context="product-edit-meta-tab-footer"]';
$( metaboxSuggestionsUISelector ).fadeOut( {
complete: function() {
$( '.marketplace-suggestions-metabox-nosuggestions-placeholder' ).fadeIn();
}
} );
}
}
function addManageSuggestionsTracksHandler() {
$( 'a.marketplace-suggestion-manage-link' ).on( 'click', function() {
window.wcTracks.recordEvent( 'marketplace_suggestions_manage_clicked' );
} );
}
function isContextHiddenOnPageLoad( context ) {
// Some suggestions are not visible on page load;
// e.g. the user reveals them by selecting a tab.
var revealableSuggestionsContexts = [
'product-edit-meta-tab-header',
'product-edit-meta-tab-body',
'product-edit-meta-tab-footer'
];
return _.includes( revealableSuggestionsContexts, context );
}
// track the current product data tab to avoid over-tracking suggestions
var currentTab = false;
// Render suggestion data in appropriate places in UI.
function displaySuggestions( marketplaceSuggestionsApiData ) {
var usedSuggestionsContexts = [];
// iterate over all suggestions containers, rendering promos
$( '.marketplace-suggestions-container' ).each( function() {
// determine the context / placement we're populating
var context = this.dataset.marketplaceSuggestionsContext;
// find promotions that target this context
var promos = getRelevantPromotions( marketplaceSuggestionsApiData, context );
// shuffle/randomly select five suggestions to display
var suggestionsToDisplay = _.sample( promos, 5 );
// render the promo content
for ( var i in suggestionsToDisplay ) {
var linkText = suggestionsToDisplay[ i ]['link-text'];
var linkoutIsButton = true;
if ( suggestionsToDisplay[ i ]['link-text'] ) {
linkText = suggestionsToDisplay[ i ]['link-text'];
linkoutIsButton = false;
}
// dismiss is allowed by default
var allowDismiss = true;
if ( suggestionsToDisplay[ i ]['allow-dismiss'] === false ) {
allowDismiss = false;
}
var content = renderListItem(
context,
suggestionsToDisplay[ i ].product,
suggestionsToDisplay[ i ].promoted,
suggestionsToDisplay[ i ].slug,
suggestionsToDisplay[ i ].icon,
suggestionsToDisplay[ i ].title,
suggestionsToDisplay[ i ].copy,
suggestionsToDisplay[ i ].url,
linkText,
linkoutIsButton,
allowDismiss
);
$( this ).append( content );
$( this ).addClass( 'showing-suggestion' );
usedSuggestionsContexts.push( context );
if ( ! isContextHiddenOnPageLoad( context ) ) {
// Fire 'displayed' tracks events for immediately visible suggestions.
window.wcTracks.recordEvent( 'marketplace_suggestion_displayed', {
suggestion_slug: suggestionsToDisplay[ i ].slug,
context: context,
product: suggestionsToDisplay[ i ].product || '',
promoted: suggestionsToDisplay[ i ].promoted || '',
target: suggestionsToDisplay[ i ].url || ''
} );
}
}
// Track when suggestions are displayed (and not already visible).
$( 'ul.product_data_tabs li.marketplace-suggestions_options a' ).on( 'click', function( e ) {
e.preventDefault();
if ( '#marketplace_suggestions' === currentTab ) {
return;
}
if ( ! isContextHiddenOnPageLoad( context ) ) {
// We've already fired 'displayed' event above.
return;
}
for ( var i in suggestionsToDisplay ) {
window.wcTracks.recordEvent( 'marketplace_suggestion_displayed', {
suggestion_slug: suggestionsToDisplay[ i ].slug,
context: context,
product: suggestionsToDisplay[ i ].product || '',
promoted: suggestionsToDisplay[ i ].promoted || '',
target: suggestionsToDisplay[ i ].url || ''
} );
}
} );
} );
hidePageElementsForSuggestionState( usedSuggestionsContexts );
tidyProductEditMetabox();
}
if ( marketplace_suggestions.suggestions_data ) {
displaySuggestions( marketplace_suggestions.suggestions_data );
// track the current product data tab to avoid over-reporting suggestion views
$( 'ul.product_data_tabs' ).on( 'click', 'li a', function( e ) {
e.preventDefault();
currentTab = $( this ).attr( 'href' );
} );
}
addManageSuggestionsTracksHandler();
});
})( jQuery, marketplace_suggestions, ajaxurl );

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,70 @@
/* global woocommerce_admin_meta_boxes_coupon */
jQuery(function( $ ) {
/**
* Coupon actions
*/
var wc_meta_boxes_coupon_actions = {
/**
* Initialize variations actions
*/
init: function() {
$( 'select#discount_type' )
.on( 'change', this.type_options )
.trigger( 'change' );
this.insert_generate_coupon_code_button();
$( '.button.generate-coupon-code' ).on( 'click', this.generate_coupon_code );
},
/**
* Show/hide fields by coupon type options
*/
type_options: function() {
// Get value
var select_val = $( this ).val();
if ( 'percent' === select_val ) {
$( '#coupon_amount' ).removeClass( 'wc_input_price' ).addClass( 'wc_input_decimal' );
} else {
$( '#coupon_amount' ).removeClass( 'wc_input_decimal' ).addClass( 'wc_input_price' );
}
if ( select_val !== 'fixed_cart' ) {
$( '.limit_usage_to_x_items_field' ).show();
} else {
$( '.limit_usage_to_x_items_field' ).hide();
}
},
/**
* Insert generate coupon code buttom HTML.
*/
insert_generate_coupon_code_button: function() {
$( '.post-type-shop_coupon' ).find( '#title' ).after(
'<a href="#" class="button generate-coupon-code">' + woocommerce_admin_meta_boxes_coupon.generate_button_text + '</a>'
);
},
/**
* Generate a random coupon code
*/
generate_coupon_code: function( e ) {
e.preventDefault();
var $coupon_code_field = $( '#title' ),
$coupon_code_label = $( '#title-prompt-text' ),
$result = '';
for ( var i = 0; i < woocommerce_admin_meta_boxes_coupon.char_length; i++ ) {
$result += woocommerce_admin_meta_boxes_coupon.characters.charAt(
Math.floor( Math.random() * woocommerce_admin_meta_boxes_coupon.characters.length )
);
}
$result = woocommerce_admin_meta_boxes_coupon.prefix + $result + woocommerce_admin_meta_boxes_coupon.suffix;
$coupon_code_field.trigger( 'focus' ).val( $result );
$coupon_code_label.addClass( 'screen-reader-text' );
}
};
wc_meta_boxes_coupon_actions.init();
});

View File

@ -0,0 +1 @@
jQuery(function(_){({init:function(){_("select#discount_type").on("change",this.type_options).trigger("change"),this.insert_generate_coupon_code_button(),_(".button.generate-coupon-code").on("click",this.generate_coupon_code)},type_options:function(){var e=_(this).val();"percent"===e?_("#coupon_amount").removeClass("wc_input_price").addClass("wc_input_decimal"):_("#coupon_amount").removeClass("wc_input_decimal").addClass("wc_input_price"),"fixed_cart"!==e?_(".limit_usage_to_x_items_field").show():_(".limit_usage_to_x_items_field").hide()},insert_generate_coupon_code_button:function(){_(".post-type-shop_coupon").find("#title").after('<a href="#" class="button generate-coupon-code">'+woocommerce_admin_meta_boxes_coupon.generate_button_text+"</a>")},generate_coupon_code:function(e){e.preventDefault();for(var o=_("#title"),e=_("#title-prompt-text"),t="",n=0;n<woocommerce_admin_meta_boxes_coupon.char_length;n++)t+=woocommerce_admin_meta_boxes_coupon.characters.charAt(Math.floor(Math.random()*woocommerce_admin_meta_boxes_coupon.characters.length));t=woocommerce_admin_meta_boxes_coupon.prefix+t+woocommerce_admin_meta_boxes_coupon.suffix,o.trigger("focus").val(t),e.addClass("screen-reader-text")}}).init()});

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,697 @@
/*global woocommerce_admin_meta_boxes */
jQuery( function( $ ) {
// Scroll to first checked category
// https://github.com/scribu/wp-category-checklist-tree/blob/d1c3c1f449e1144542efa17dde84a9f52ade1739/category-checklist-tree.php
$( function() {
$( '[id$="-all"] > ul.categorychecklist' ).each( function() {
var $list = $( this );
var $firstChecked = $list.find( ':checked' ).first();
if ( ! $firstChecked.length ) {
return;
}
var pos_first = $list.find( 'input' ).position().top;
var pos_checked = $firstChecked.position().top;
$list.closest( '.tabs-panel' ).scrollTop( pos_checked - pos_first + 5 );
});
});
// Prevent enter submitting post form.
$( '#upsell_product_data' ).on( 'keypress', function( e ) {
if ( e.keyCode === 13 ) {
return false;
}
});
// Type box.
if ( $( 'body' ).hasClass( 'wc-wp-version-gte-55' ) ) {
$( '.type_box' ).appendTo( '#woocommerce-product-data .hndle' );
} else {
$( '.type_box' ).appendTo( '#woocommerce-product-data .hndle span' );
}
$( function() {
var woocommerce_product_data = $( '#woocommerce-product-data' );
// Prevent inputs in meta box headings opening/closing contents.
woocommerce_product_data.find( '.hndle' ).off( 'click.postboxes' );
woocommerce_product_data.on( 'click', '.hndle', function( event ) {
// If the user clicks on some form input inside the h3 the box should not be toggled.
if ( $( event.target ).filter( 'input, option, label, select' ).length ) {
return;
}
if ( woocommerce_product_data.hasClass( 'closed' ) ) {
woocommerce_product_data.removeClass( 'closed' );
} else {
woocommerce_product_data.addClass( 'closed' );
}
});
});
// Catalog Visibility.
$( '#catalog-visibility' ).find( '.edit-catalog-visibility' ).on( 'click', function() {
if ( $( '#catalog-visibility-select' ).is( ':hidden' ) ) {
$( '#catalog-visibility-select' ).slideDown( 'fast' );
$( this ).hide();
}
return false;
});
$( '#catalog-visibility' ).find( '.save-post-visibility' ).on( 'click', function() {
$( '#catalog-visibility-select' ).slideUp( 'fast' );
$( '#catalog-visibility' ).find( '.edit-catalog-visibility' ).show();
var label = $( 'input[name=_visibility]:checked' ).attr( 'data-label' );
if ( $( 'input[name=_featured]' ).is( ':checked' ) ) {
label = label + ', ' + woocommerce_admin_meta_boxes.featured_label;
$( 'input[name=_featured]' ).attr( 'checked', 'checked' );
}
$( '#catalog-visibility-display' ).text( label );
return false;
});
$( '#catalog-visibility' ).find( '.cancel-post-visibility' ).on( 'click', function() {
$( '#catalog-visibility-select' ).slideUp( 'fast' );
$( '#catalog-visibility' ).find( '.edit-catalog-visibility' ).show();
var current_visibility = $( '#current_visibility' ).val();
var current_featured = $( '#current_featured' ).val();
$( 'input[name=_visibility]' ).prop( 'checked', false );
$( 'input[name=_visibility][value=' + current_visibility + ']' ).attr( 'checked', 'checked' );
var label = $( 'input[name=_visibility]:checked' ).attr( 'data-label' );
if ( 'yes' === current_featured ) {
label = label + ', ' + woocommerce_admin_meta_boxes.featured_label;
$( 'input[name=_featured]' ).attr( 'checked', 'checked' );
} else {
$( 'input[name=_featured]' ).prop( 'checked', false );
}
$( '#catalog-visibility-display' ).text( label );
return false;
});
// Product type specific options.
$( 'select#product-type' ).on( 'change', function() {
// Get value.
var select_val = $( this ).val();
if ( 'variable' === select_val ) {
$( 'input#_manage_stock' ).trigger( 'change' );
$( 'input#_downloadable' ).prop( 'checked', false );
$( 'input#_virtual' ).prop( 'checked', false );
} else if ( 'grouped' === select_val ) {
$( 'input#_downloadable' ).prop( 'checked', false );
$( 'input#_virtual' ).prop( 'checked', false );
} else if ( 'external' === select_val ) {
$( 'input#_downloadable' ).prop( 'checked', false );
$( 'input#_virtual' ).prop( 'checked', false );
}
show_and_hide_panels();
$( 'ul.wc-tabs li:visible' ).eq( 0 ).find( 'a' ).trigger( 'click' );
$( document.body ).trigger( 'woocommerce-product-type-change', select_val, $( this ) );
}).trigger( 'change' );
$( 'input#_downloadable, input#_virtual' ).on( 'change', function() {
show_and_hide_panels();
});
function show_and_hide_panels() {
var product_type = $( 'select#product-type' ).val();
var is_virtual = $( 'input#_virtual:checked' ).length;
var is_downloadable = $( 'input#_downloadable:checked' ).length;
// Hide/Show all with rules.
var hide_classes = '.hide_if_downloadable, .hide_if_virtual';
var show_classes = '.show_if_downloadable, .show_if_virtual';
$.each( woocommerce_admin_meta_boxes.product_types, function( index, value ) {
hide_classes = hide_classes + ', .hide_if_' + value;
show_classes = show_classes + ', .show_if_' + value;
});
$( hide_classes ).show();
$( show_classes ).hide();
// Shows rules.
if ( is_downloadable ) {
$( '.show_if_downloadable' ).show();
}
if ( is_virtual ) {
$( '.show_if_virtual' ).show();
// If user enables virtual while on shipping tab, switch to general tab.
if ( $( '.shipping_options.shipping_tab' ).hasClass( 'active' ) ) {
$( '.general_options.general_tab > a' ).trigger( 'click' );
}
}
$( '.show_if_' + product_type ).show();
// Hide rules.
if ( is_downloadable ) {
$( '.hide_if_downloadable' ).hide();
}
if ( is_virtual ) {
$( '.hide_if_virtual' ).hide();
}
$( '.hide_if_' + product_type ).hide();
$( 'input#_manage_stock' ).trigger( 'change' );
// Hide empty panels/tabs after display.
$( '.woocommerce_options_panel' ).each( function() {
var $children = $( this ).children( '.options_group' );
if ( 0 === $children.length ) {
return;
}
var $invisble = $children.filter( function() {
return 'none' === $( this ).css( 'display' );
});
// Hide panel.
if ( $invisble.length === $children.length ) {
var $id = $( this ).prop( 'id' );
$( '.product_data_tabs' ).find( 'li a[href="#' + $id + '"]' ).parent().hide();
}
});
}
// Sale price schedule.
$( '.sale_price_dates_fields' ).each( function() {
var $these_sale_dates = $( this );
var sale_schedule_set = false;
var $wrap = $these_sale_dates.closest( 'div, table' );
$these_sale_dates.find( 'input' ).each( function() {
if ( '' !== $( this ).val() ) {
sale_schedule_set = true;
}
});
if ( sale_schedule_set ) {
$wrap.find( '.sale_schedule' ).hide();
$wrap.find( '.sale_price_dates_fields' ).show();
} else {
$wrap.find( '.sale_schedule' ).show();
$wrap.find( '.sale_price_dates_fields' ).hide();
}
});
$( '#woocommerce-product-data' ).on( 'click', '.sale_schedule', function() {
var $wrap = $( this ).closest( 'div, table' );
$( this ).hide();
$wrap.find( '.cancel_sale_schedule' ).show();
$wrap.find( '.sale_price_dates_fields' ).show();
return false;
});
$( '#woocommerce-product-data' ).on( 'click', '.cancel_sale_schedule', function() {
var $wrap = $( this ).closest( 'div, table' );
$( this ).hide();
$wrap.find( '.sale_schedule' ).show();
$wrap.find( '.sale_price_dates_fields' ).hide();
$wrap.find( '.sale_price_dates_fields' ).find( 'input' ).val('');
return false;
});
// File inputs.
$( '#woocommerce-product-data' ).on( 'click','.downloadable_files a.insert', function() {
$( this ).closest( '.downloadable_files' ).find( 'tbody' ).append( $( this ).data( 'row' ) );
return false;
});
$( '#woocommerce-product-data' ).on( 'click','.downloadable_files a.delete',function() {
$( this ).closest( 'tr' ).remove();
return false;
});
// Stock options.
$( 'input#_manage_stock' ).on( 'change', function() {
if ( $( this ).is( ':checked' ) ) {
$( 'div.stock_fields' ).show();
$( 'p.stock_status_field' ).hide();
} else {
var product_type = $( 'select#product-type' ).val();
$( 'div.stock_fields' ).hide();
$( 'p.stock_status_field:not( .hide_if_' + product_type + ' )' ).show();
}
$( 'input.variable_manage_stock' ).trigger( 'change' );
}).trigger( 'change' );
// Date picker fields.
function date_picker_select( datepicker ) {
var option = $( datepicker ).next().is( '.hasDatepicker' ) ? 'minDate' : 'maxDate',
otherDateField = 'minDate' === option ? $( datepicker ).next() : $( datepicker ).prev(),
date = $( datepicker ).datepicker( 'getDate' );
$( otherDateField ).datepicker( 'option', option, date );
$( datepicker ).trigger( 'change' );
}
$( '.sale_price_dates_fields' ).each( function() {
$( this ).find( 'input' ).datepicker({
defaultDate: '',
dateFormat: 'yy-mm-dd',
numberOfMonths: 1,
showButtonPanel: true,
onSelect: function() {
date_picker_select( $( this ) );
}
});
$( this ).find( 'input' ).each( function() { date_picker_select( $( this ) ); } );
});
// Attribute Tables.
// Initial order.
var woocommerce_attribute_items = $( '.product_attributes' ).find( '.woocommerce_attribute' ).get();
woocommerce_attribute_items.sort( function( a, b ) {
var compA = parseInt( $( a ).attr( 'rel' ), 10 );
var compB = parseInt( $( b ).attr( 'rel' ), 10 );
return ( compA < compB ) ? -1 : ( compA > compB ) ? 1 : 0;
});
$( woocommerce_attribute_items ).each( function( index, el ) {
$( '.product_attributes' ).append( el );
});
function attribute_row_indexes() {
$( '.product_attributes .woocommerce_attribute' ).each( function( index, el ) {
$( '.attribute_position', el ).val( parseInt( $( el ).index( '.product_attributes .woocommerce_attribute' ), 10 ) );
});
}
$( '.product_attributes .woocommerce_attribute' ).each( function( index, el ) {
if ( $( el ).css( 'display' ) !== 'none' && $( el ).is( '.taxonomy' ) ) {
$( 'select.attribute_taxonomy' ).find( 'option[value="' + $( el ).data( 'taxonomy' ) + '"]' ).attr( 'disabled', 'disabled' );
}
});
// Add rows.
$( 'button.add_attribute' ).on( 'click', function() {
var size = $( '.product_attributes .woocommerce_attribute' ).length;
var attribute = $( 'select.attribute_taxonomy' ).val();
var $wrapper = $( this ).closest( '#product_attributes' );
var $attributes = $wrapper.find( '.product_attributes' );
var product_type = $( 'select#product-type' ).val();
var data = {
action: 'woocommerce_add_attribute',
taxonomy: attribute,
i: size,
security: woocommerce_admin_meta_boxes.add_attribute_nonce
};
$wrapper.block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
$attributes.append( response );
if ( 'variable' !== product_type ) {
$attributes.find( '.enable_variation' ).hide();
}
$( document.body ).trigger( 'wc-enhanced-select-init' );
attribute_row_indexes();
$attributes.find( '.woocommerce_attribute' ).last().find( 'h3' ).trigger( 'click' );
$wrapper.unblock();
$( document.body ).trigger( 'woocommerce_added_attribute' );
});
if ( attribute ) {
$( 'select.attribute_taxonomy' ).find( 'option[value="' + attribute + '"]' ).attr( 'disabled','disabled' );
$( 'select.attribute_taxonomy' ).val( '' );
}
return false;
});
$( '.product_attributes' ).on( 'blur', 'input.attribute_name', function() {
$( this ).closest( '.woocommerce_attribute' ).find( 'strong.attribute_name' ).text( $( this ).val() );
});
$( '.product_attributes' ).on( 'click', 'button.select_all_attributes', function() {
$( this ).closest( 'td' ).find( 'select option' ).prop( 'selected', 'selected' );
$( this ).closest( 'td' ).find( 'select' ).trigger( 'change' );
return false;
});
$( '.product_attributes' ).on( 'click', 'button.select_no_attributes', function() {
$( this ).closest( 'td' ).find( 'select option' ).prop( 'selected', false );
$( this ).closest( 'td' ).find( 'select' ).trigger( 'change' );
return false;
});
$( '.product_attributes' ).on( 'click', '.remove_row', function() {
if ( window.confirm( woocommerce_admin_meta_boxes.remove_attribute ) ) {
var $parent = $( this ).parent().parent();
if ( $parent.is( '.taxonomy' ) ) {
$parent.find( 'select, input[type=text]' ).val( '' );
$parent.hide();
$( 'select.attribute_taxonomy' ).find( 'option[value="' + $parent.data( 'taxonomy' ) + '"]' ).prop( 'disabled', false );
} else {
$parent.find( 'select, input[type=text]' ).val( '' );
$parent.hide();
attribute_row_indexes();
}
}
return false;
});
// Attribute ordering.
$( '.product_attributes' ).sortable({
items: '.woocommerce_attribute',
cursor: 'move',
axis: 'y',
handle: 'h3',
scrollSensitivity: 40,
forcePlaceholderSize: true,
helper: 'clone',
opacity: 0.65,
placeholder: 'wc-metabox-sortable-placeholder',
start: function( event, ui ) {
ui.item.css( 'background-color', '#f6f6f6' );
},
stop: function( event, ui ) {
ui.item.removeAttr( 'style' );
attribute_row_indexes();
}
});
// Add a new attribute (via ajax).
$( '.product_attributes' ).on( 'click', 'button.add_new_attribute', function() {
$( '.product_attributes' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var $wrapper = $( this ).closest( '.woocommerce_attribute' );
var attribute = $wrapper.data( 'taxonomy' );
var new_attribute_name = window.prompt( woocommerce_admin_meta_boxes.new_attribute_prompt );
if ( new_attribute_name ) {
var data = {
action: 'woocommerce_add_new_attribute',
taxonomy: attribute,
term: new_attribute_name,
security: woocommerce_admin_meta_boxes.add_attribute_nonce
};
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.error ) {
// Error.
window.alert( response.error );
} else if ( response.slug ) {
// Success.
$wrapper.find( 'select.attribute_values' )
.append( '<option value="' + response.term_id + '" selected="selected">' + response.name + '</option>' );
$wrapper.find( 'select.attribute_values' ).trigger( 'change' );
}
$( '.product_attributes' ).unblock();
});
} else {
$( '.product_attributes' ).unblock();
}
return false;
});
// Save attributes and update variations.
$( '.save_attributes' ).on( 'click', function() {
$( '.product_attributes' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var original_data = $( '.product_attributes' ).find( 'input, select, textarea' );
var data = {
post_id : woocommerce_admin_meta_boxes.post_id,
product_type: $( '#product-type' ).val(),
data : original_data.serialize(),
action : 'woocommerce_save_attributes',
security : woocommerce_admin_meta_boxes.save_attributes_nonce
};
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.error ) {
// Error.
window.alert( response.error );
} else if ( response.data ) {
// Success.
$( '.product_attributes' ).html( response.data.html );
$( '.product_attributes' ).unblock();
// Hide the 'Used for variations' checkbox if not viewing a variable product
show_and_hide_panels();
// Make sure the dropdown is not disabled for empty value attributes.
$( 'select.attribute_taxonomy' ).find( 'option' ).prop( 'disabled', false );
$( '.product_attributes .woocommerce_attribute' ).each( function( index, el ) {
if ( $( el ).css( 'display' ) !== 'none' && $( el ).is( '.taxonomy' ) ) {
$( 'select.attribute_taxonomy' )
.find( 'option[value="' + $( el ).data( 'taxonomy' ) + '"]' )
.prop( 'disabled', true );
}
});
// Reload variations panel.
var this_page = window.location.toString();
this_page = this_page.replace( 'post-new.php?', 'post.php?post=' + woocommerce_admin_meta_boxes.post_id + '&action=edit&' );
$( '#variable_product_options' ).load( this_page + ' #variable_product_options_inner', function() {
$( '#variable_product_options' ).trigger( 'reload' );
} );
}
});
});
// Uploading files.
var downloadable_file_frame;
var file_path_field;
$( document.body ).on( 'click', '.upload_file_button', function( event ) {
var $el = $( this );
file_path_field = $el.closest( 'tr' ).find( 'td.file_url input' );
event.preventDefault();
// If the media frame already exists, reopen it.
if ( downloadable_file_frame ) {
downloadable_file_frame.open();
return;
}
var downloadable_file_states = [
// Main states.
new wp.media.controller.Library({
library: wp.media.query(),
multiple: true,
title: $el.data('choose'),
priority: 20,
filterable: 'uploaded'
})
];
// Create the media frame.
downloadable_file_frame = wp.media.frames.downloadable_file = wp.media({
// Set the title of the modal.
title: $el.data('choose'),
library: {
type: ''
},
button: {
text: $el.data('update')
},
multiple: true,
states: downloadable_file_states
});
// When an image is selected, run a callback.
downloadable_file_frame.on( 'select', function() {
var file_path = '';
var selection = downloadable_file_frame.state().get( 'selection' );
selection.map( function( attachment ) {
attachment = attachment.toJSON();
if ( attachment.url ) {
file_path = attachment.url;
}
});
file_path_field.val( file_path ).trigger( 'change' );
});
// Set post to 0 and set our custom type.
downloadable_file_frame.on( 'ready', function() {
downloadable_file_frame.uploader.options.uploader.params = {
type: 'downloadable_product'
};
});
// Finally, open the modal.
downloadable_file_frame.open();
});
// Download ordering.
$( '.downloadable_files tbody' ).sortable({
items: 'tr',
cursor: 'move',
axis: 'y',
handle: 'td.sort',
scrollSensitivity: 40,
forcePlaceholderSize: true,
helper: 'clone',
opacity: 0.65
});
// Product gallery file uploads.
var product_gallery_frame;
var $image_gallery_ids = $( '#product_image_gallery' );
var $product_images = $( '#product_images_container' ).find( 'ul.product_images' );
$( '.add_product_images' ).on( 'click', 'a', function( event ) {
var $el = $( this );
event.preventDefault();
// If the media frame already exists, reopen it.
if ( product_gallery_frame ) {
product_gallery_frame.open();
return;
}
// Create the media frame.
product_gallery_frame = wp.media.frames.product_gallery = wp.media({
// Set the title of the modal.
title: $el.data( 'choose' ),
button: {
text: $el.data( 'update' )
},
states: [
new wp.media.controller.Library({
title: $el.data( 'choose' ),
filterable: 'all',
multiple: true
})
]
});
// When an image is selected, run a callback.
product_gallery_frame.on( 'select', function() {
var selection = product_gallery_frame.state().get( 'selection' );
var attachment_ids = $image_gallery_ids.val();
selection.map( function( attachment ) {
attachment = attachment.toJSON();
if ( attachment.id ) {
attachment_ids = attachment_ids ? attachment_ids + ',' + attachment.id : attachment.id;
var attachment_image = attachment.sizes && attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
$product_images.append(
'<li class="image" data-attachment_id="' + attachment.id + '"><img src="' + attachment_image +
'" /><ul class="actions"><li><a href="#" class="delete" title="' + $el.data('delete') + '">' +
$el.data('text') + '</a></li></ul></li>'
);
}
});
$image_gallery_ids.val( attachment_ids );
});
// Finally, open the modal.
product_gallery_frame.open();
});
// Image ordering.
$product_images.sortable({
items: 'li.image',
cursor: 'move',
scrollSensitivity: 40,
forcePlaceholderSize: true,
forceHelperSize: false,
helper: 'clone',
opacity: 0.65,
placeholder: 'wc-metabox-sortable-placeholder',
start: function( event, ui ) {
ui.item.css( 'background-color', '#f6f6f6' );
},
stop: function( event, ui ) {
ui.item.removeAttr( 'style' );
},
update: function() {
var attachment_ids = '';
$( '#product_images_container' ).find( 'ul li.image' ).css( 'cursor', 'default' ).each( function() {
var attachment_id = $( this ).attr( 'data-attachment_id' );
attachment_ids = attachment_ids + attachment_id + ',';
});
$image_gallery_ids.val( attachment_ids );
}
});
// Remove images.
$( '#product_images_container' ).on( 'click', 'a.delete', function() {
$( this ).closest( 'li.image' ).remove();
var attachment_ids = '';
$( '#product_images_container' ).find( 'ul li.image' ).css( 'cursor', 'default' ).each( function() {
var attachment_id = $( this ).attr( 'data-attachment_id' );
attachment_ids = attachment_ids + attachment_id + ',';
});
$image_gallery_ids.val( attachment_ids );
// Remove any lingering tooltips.
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
return false;
});
});

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,80 @@
jQuery( function ( $ ) {
// Run tipTip
function runTipTip() {
// Remove any lingering tooltips
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
$( '.tips' ).tipTip({
'attribute': 'data-tip',
'fadeIn': 50,
'fadeOut': 50,
'delay': 200,
'keepAlive': true
});
}
runTipTip();
$( '.wc-metaboxes-wrapper' ).on( 'click', '.wc-metabox > h3', function() {
var metabox = $( this ).parent( '.wc-metabox' );
if ( metabox.hasClass( 'closed' ) ) {
metabox.removeClass( 'closed' );
} else {
metabox.addClass( 'closed' );
}
if ( metabox.hasClass( 'open' ) ) {
metabox.removeClass( 'open' );
} else {
metabox.addClass( 'open' );
}
});
// Tabbed Panels
$( document.body ).on( 'wc-init-tabbed-panels', function() {
$( 'ul.wc-tabs' ).show();
$( 'ul.wc-tabs a' ).on( 'click', function( e ) {
e.preventDefault();
var panel_wrap = $( this ).closest( 'div.panel-wrap' );
$( 'ul.wc-tabs li', panel_wrap ).removeClass( 'active' );
$( this ).parent().addClass( 'active' );
$( 'div.panel', panel_wrap ).hide();
$( $( this ).attr( 'href' ) ).show();
});
$( 'div.panel-wrap' ).each( function() {
$( this ).find( 'ul.wc-tabs li' ).eq( 0 ).find( 'a' ).trigger( 'click' );
});
}).trigger( 'wc-init-tabbed-panels' );
// Date Picker
$( document.body ).on( 'wc-init-datepickers', function() {
$( '.date-picker-field, .date-picker' ).datepicker({
dateFormat: 'yy-mm-dd',
numberOfMonths: 1,
showButtonPanel: true
});
}).trigger( 'wc-init-datepickers' );
// Meta-Boxes - Open/close
$( '.wc-metaboxes-wrapper' ).on( 'click', '.wc-metabox h3', function( event ) {
// If the user clicks on some form input inside the h3, like a select list (for variations), the box should not be toggled
if ( $( event.target ).filter( ':input, option, .sort' ).length ) {
return;
}
$( this ).next( '.wc-metabox-content' ).stop().slideToggle();
})
.on( 'click', '.expand_all', function() {
$( this ).closest( '.wc-metaboxes-wrapper' ).find( '.wc-metabox > .wc-metabox-content' ).show();
return false;
})
.on( 'click', '.close_all', function() {
$( this ).closest( '.wc-metaboxes-wrapper' ).find( '.wc-metabox > .wc-metabox-content' ).hide();
return false;
});
$( '.wc-metabox.closed' ).each( function() {
$( this ).find( '.wc-metabox-content' ).hide();
});
});

1
assets/js/admin/meta-boxes.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(t){t("#tiptip_holder").removeAttr("style"),t("#tiptip_arrow").removeAttr("style"),t(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200,keepAlive:!0}),t(".wc-metaboxes-wrapper").on("click",".wc-metabox > h3",function(){var e=t(this).parent(".wc-metabox");e.hasClass("closed")?e.removeClass("closed"):e.addClass("closed"),e.hasClass("open")?e.removeClass("open"):e.addClass("open")}),t(document.body).on("wc-init-tabbed-panels",function(){t("ul.wc-tabs").show(),t("ul.wc-tabs a").on("click",function(e){e.preventDefault();e=t(this).closest("div.panel-wrap");t("ul.wc-tabs li",e).removeClass("active"),t(this).parent().addClass("active"),t("div.panel",e).hide(),t(t(this).attr("href")).show()}),t("div.panel-wrap").each(function(){t(this).find("ul.wc-tabs li").eq(0).find("a").trigger("click")})}).trigger("wc-init-tabbed-panels"),t(document.body).on("wc-init-datepickers",function(){t(".date-picker-field, .date-picker").datepicker({dateFormat:"yy-mm-dd",numberOfMonths:1,showButtonPanel:!0})}).trigger("wc-init-datepickers"),t(".wc-metaboxes-wrapper").on("click",".wc-metabox h3",function(e){t(e.target).filter(":input, option, .sort").length||t(this).next(".wc-metabox-content").stop().slideToggle()}).on("click",".expand_all",function(){return t(this).closest(".wc-metaboxes-wrapper").find(".wc-metabox > .wc-metabox-content").show(),!1}).on("click",".close_all",function(){return t(this).closest(".wc-metaboxes-wrapper").find(".wc-metabox > .wc-metabox-content").hide(),!1}),t(".wc-metabox.closed").each(function(){t(this).find(".wc-metabox-content").hide()})});

View File

@ -0,0 +1,90 @@
/*global woocommerce_network_orders */
(function( $, _, undefined ) {
if ( 'undefined' === typeof woocommerce_network_orders ) {
return;
}
var orders = [],
promises = [], // Track completion (pass or fail) of ajax requests.
deferred = [], // Tracks the ajax deferreds.
$tbody = $( document.getElementById( 'network-orders-tbody' ) ),
template = _.template( $( document.getElementById( 'network-orders-row-template') ).text() ),
$loadingIndicator = $( document.getElementById( 'woocommerce-network-order-table-loading' ) ),
$orderTable = $( document.getElementById( 'woocommerce-network-order-table' ) ),
$noneFound = $( document.getElementById( 'woocommerce-network-orders-no-orders' ) );
// No sites, so bail.
if ( ! woocommerce_network_orders.sites.length ) {
$loadingIndicator.removeClass( 'is-active' );
$orderTable.removeClass( 'is-active' );
$noneFound.addClass( 'is-active' );
return;
}
$.each( woocommerce_network_orders.sites, function( index, value ) {
promises[ index ] = $.Deferred();
deferred.push( $.ajax( {
url : woocommerce_network_orders.order_endpoint,
data: {
_wpnonce: woocommerce_network_orders.nonce,
network_orders: true,
blog_id: value
},
type: 'GET'
} ).success(function( response ) {
var orderindex;
for ( orderindex in response ) {
orders.push( response[ orderindex ] );
}
promises[ index ].resolve();
}).fail(function (){
promises[ index ].resolve();
}) );
} );
if ( promises.length > 0 ) {
$.when.apply( $, promises ).done( function() {
var orderindex,
currentOrder;
// Sort orders, newest first
orders.sort(function( a, b ) {
var adate, bdate;
adate = Date.parse( a.date_created_gmt );
bdate = Date.parse( b.date_created_gmt );
if ( adate === bdate ) {
return 0;
}
if ( adate < bdate ) {
return 1;
} else {
return -1;
}
});
if ( orders.length > 0 ) {
for ( orderindex in orders ) {
currentOrder = orders[ orderindex ];
$tbody.append( template( currentOrder ) );
}
$noneFound.removeClass( 'is-active' );
$loadingIndicator.removeClass( 'is-active' );
$orderTable.addClass( 'is-active' );
} else {
$noneFound.addClass( 'is-active' );
$loadingIndicator.removeClass( 'is-active' );
$orderTable.removeClass( 'is-active' );
}
} );
}
})( jQuery, _ );

1
assets/js/admin/network-orders.min.js vendored Normal file
View File

@ -0,0 +1 @@
!function(o,e){if("undefined"!=typeof woocommerce_network_orders){var t=[],n=[],s=[],r=o(document.getElementById("network-orders-tbody")),a=e.template(o(document.getElementById("network-orders-row-template")).text()),c=o(document.getElementById("woocommerce-network-order-table-loading")),d=o(document.getElementById("woocommerce-network-order-table")),i=o(document.getElementById("woocommerce-network-orders-no-orders"));if(!woocommerce_network_orders.sites.length)return c.removeClass("is-active"),d.removeClass("is-active"),i.addClass("is-active");o.each(woocommerce_network_orders.sites,function(r,e){n[r]=o.Deferred(),s.push(o.ajax({url:woocommerce_network_orders.order_endpoint,data:{_wpnonce:woocommerce_network_orders.nonce,network_orders:!0,blog_id:e},type:"GET"}).success(function(e){for(var o in e)t.push(e[o]);n[r].resolve()}).fail(function(){n[r].resolve()}))}),0<n.length&&o.when.apply(o,n).done(function(){var e,o;if(t.sort(function(e,o){e=Date.parse(e.date_created_gmt),o=Date.parse(o.date_created_gmt);return e===o?0:e<o?1:-1}),0<t.length){for(e in t)o=t[e],r.append(a(o));i.removeClass("is-active"),c.removeClass("is-active"),d.addClass("is-active")}else i.addClass("is-active"),c.removeClass("is-active"),d.removeClass("is-active")})}}(jQuery,_);

View File

@ -0,0 +1,71 @@
/*global ajaxurl */
/**
* Based on Simple Page Ordering by 10up (https://wordpress.org/plugins/simple-page-ordering/)
*
* Modified - products have no children (non hierarchical)
*/
jQuery( function( $ ) {
$( 'table.widefat tbody th, table.widefat tbody td' ).css( 'cursor', 'move' );
$( 'table.widefat tbody' ).sortable({
items: 'tr:not(.inline-edit-row)',
cursor: 'move',
axis: 'y',
containment: 'table.widefat',
scrollSensitivity: 40,
helper: function( event, ui ) {
ui.each( function() {
$( this ).width( $( this ).width() );
});
return ui;
},
start: function( event, ui ) {
ui.item.css( 'background-color', '#ffffff' );
ui.item.children( 'td, th' ).css( 'border-bottom-width', '0' );
ui.item.css( 'outline', '1px solid #dfdfdf' );
},
stop: function( event, ui ) {
ui.item.removeAttr( 'style' );
ui.item.children( 'td,th' ).css( 'border-bottom-width', '1px' );
},
update: function( event, ui ) {
$( 'table.widefat tbody th, table.widefat tbody td' ).css( 'cursor', 'default' );
$( 'table.widefat tbody' ).sortable( 'disable' );
var postid = ui.item.find( '.check-column input' ).val();
var prevpostid = ui.item.prev().find( '.check-column input' ).val();
var nextpostid = ui.item.next().find( '.check-column input' ).val();
// Show Spinner
ui.item
.find( '.check-column input' )
.hide()
.after( '<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />' );
// Go do the sorting stuff via ajax
$.post(
ajaxurl,
{ action: 'woocommerce_product_ordering', id: postid, previd: prevpostid, nextid: nextpostid },
function( response ) {
$.each( response, function( key, value ) {
$( '#inline_' + key + ' .menu_order' ).html( value );
});
ui.item.find( '.check-column input' ).show().siblings( 'img' ).remove();
$( 'table.widefat tbody th, table.widefat tbody td' ).css( 'cursor', 'move' );
$( 'table.widefat tbody' ).sortable( 'enable' );
}
);
// fix cell colors
$( 'table.widefat tbody tr' ).each( function() {
var i = $( 'table.widefat tbody tr' ).index( this );
if ( i%2 === 0 ) {
$( this ).addClass( 'alternate' );
} else {
$( this ).removeClass( 'alternate' );
}
});
}
});
});

View File

@ -0,0 +1 @@
jQuery(function(d){d("table.widefat tbody th, table.widefat tbody td").css("cursor","move"),d("table.widefat tbody").sortable({items:"tr:not(.inline-edit-row)",cursor:"move",axis:"y",containment:"table.widefat",scrollSensitivity:40,helper:function(t,e){return e.each(function(){d(this).width(d(this).width())}),e},start:function(t,e){e.item.css("background-color","#ffffff"),e.item.children("td, th").css("border-bottom-width","0"),e.item.css("outline","1px solid #dfdfdf")},stop:function(t,e){e.item.removeAttr("style"),e.item.children("td,th").css("border-bottom-width","1px")},update:function(t,e){d("table.widefat tbody th, table.widefat tbody td").css("cursor","default"),d("table.widefat tbody").sortable("disable");var i=e.item.find(".check-column input").val(),o=e.item.prev().find(".check-column input").val(),n=e.item.next().find(".check-column input").val();e.item.find(".check-column input").hide().after('<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />'),d.post(ajaxurl,{action:"woocommerce_product_ordering",id:i,previd:o,nextid:n},function(t){d.each(t,function(t,e){d("#inline_"+t+" .menu_order").html(e)}),e.item.find(".check-column input").show().siblings("img").remove(),d("table.widefat tbody th, table.widefat tbody td").css("cursor","move"),d("table.widefat tbody").sortable("enable")}),d("table.widefat tbody tr").each(function(){d("table.widefat tbody tr").index(this)%2==0?d(this).addClass("alternate"):d(this).removeClass("alternate")})}})});

View File

@ -0,0 +1,167 @@
/*global inlineEditPost, woocommerce_admin, woocommerce_quick_edit */
jQuery(
function( $ ) {
$( '#the-list' ).on(
'click',
'.editinline',
function() {
inlineEditPost.revert();
var post_id = $( this ).closest( 'tr' ).attr( 'id' );
post_id = post_id.replace( 'post-', '' );
var $wc_inline_data = $( '#woocommerce_inline_' + post_id );
var sku = $wc_inline_data.find( '.sku' ).text(),
regular_price = $wc_inline_data.find( '.regular_price' ).text(),
sale_price = $wc_inline_data.find( '.sale_price ' ).text(),
weight = $wc_inline_data.find( '.weight' ).text(),
length = $wc_inline_data.find( '.length' ).text(),
width = $wc_inline_data.find( '.width' ).text(),
height = $wc_inline_data.find( '.height' ).text(),
shipping_class = $wc_inline_data.find( '.shipping_class' ).text(),
visibility = $wc_inline_data.find( '.visibility' ).text(),
stock_status = $wc_inline_data.find( '.stock_status' ).text(),
stock = $wc_inline_data.find( '.stock' ).text(),
featured = $wc_inline_data.find( '.featured' ).text(),
manage_stock = $wc_inline_data.find( '.manage_stock' ).text(),
menu_order = $wc_inline_data.find( '.menu_order' ).text(),
tax_status = $wc_inline_data.find( '.tax_status' ).text(),
tax_class = $wc_inline_data.find( '.tax_class' ).text(),
backorders = $wc_inline_data.find( '.backorders' ).text(),
product_type = $wc_inline_data.find( '.product_type' ).text();
var formatted_regular_price = regular_price.replace( '.', woocommerce_admin.mon_decimal_point ),
formatted_sale_price = sale_price.replace( '.', woocommerce_admin.mon_decimal_point );
$( 'input[name="_sku"]', '.inline-edit-row' ).val( sku );
$( 'input[name="_regular_price"]', '.inline-edit-row' ).val( formatted_regular_price );
$( 'input[name="_sale_price"]', '.inline-edit-row' ).val( formatted_sale_price );
$( 'input[name="_weight"]', '.inline-edit-row' ).val( weight );
$( 'input[name="_length"]', '.inline-edit-row' ).val( length );
$( 'input[name="_width"]', '.inline-edit-row' ).val( width );
$( 'input[name="_height"]', '.inline-edit-row' ).val( height );
$( 'select[name="_shipping_class"] option:selected', '.inline-edit-row' ).attr( 'selected', false ).trigger( 'change' );
$( 'select[name="_shipping_class"] option[value="' + shipping_class + '"]' ).attr( 'selected', 'selected' )
.trigger( 'change' );
$( 'input[name="_stock"]', '.inline-edit-row' ).val( stock );
$( 'input[name="menu_order"]', '.inline-edit-row' ).val( menu_order );
$(
'select[name="_tax_status"] option, ' +
'select[name="_tax_class"] option, ' +
'select[name="_visibility"] option, ' +
'select[name="_stock_status"] option, ' +
'select[name="_backorders"] option'
).prop( 'selected', false ).removeAttr( 'selected' );
var is_variable_product = 'variable' === product_type;
$( 'select[name="_stock_status"] ~ .wc-quick-edit-warning', '.inline-edit-row' ).toggle( is_variable_product );
$( 'select[name="_stock_status"] option[value="' + (is_variable_product ? '' : stock_status) + '"]', '.inline-edit-row' )
.attr( 'selected', 'selected' );
$( 'select[name="_tax_status"] option[value="' + tax_status + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
$( 'select[name="_tax_class"] option[value="' + tax_class + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
$( 'select[name="_visibility"] option[value="' + visibility + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
$( 'select[name="_backorders"] option[value="' + backorders + '"]', '.inline-edit-row' ).attr( 'selected', 'selected' );
if ( 'yes' === featured ) {
$( 'input[name="_featured"]', '.inline-edit-row' ).prop( 'checked', true );
} else {
$( 'input[name="_featured"]', '.inline-edit-row' ).prop( 'checked', false );
}
// Conditional display.
var product_is_virtual = $wc_inline_data.find( '.product_is_virtual' ).text();
var product_supports_stock_status = 'external' !== product_type;
var product_supports_stock_fields = 'external' !== product_type && 'grouped' !== product_type;
$( '.stock_fields, .manage_stock_field, .stock_status_field, .backorder_field' ).show();
if ( product_supports_stock_fields ) {
if ( 'yes' === manage_stock ) {
$( '.stock_qty_field, .backorder_field', '.inline-edit-row' ).show().removeAttr( 'style' );
$( '.stock_status_field' ).hide();
$( '.manage_stock_field input' ).prop( 'checked', true );
} else {
$( '.stock_qty_field, .backorder_field', '.inline-edit-row' ).hide();
$( '.stock_status_field' ).show().removeAttr( 'style' );
$( '.manage_stock_field input' ).prop( 'checked', false );
}
} else if ( product_supports_stock_status ) {
$( '.stock_fields, .manage_stock_field, .backorder_field' ).hide();
} else {
$( '.stock_fields, .manage_stock_field, .stock_status_field, .backorder_field' ).hide();
}
if ( 'simple' === product_type || 'external' === product_type ) {
$( '.price_fields', '.inline-edit-row' ).show().removeAttr( 'style' );
} else {
$( '.price_fields', '.inline-edit-row' ).hide();
}
if ( 'yes' === product_is_virtual ) {
$( '.dimension_fields', '.inline-edit-row' ).hide();
} else {
$( '.dimension_fields', '.inline-edit-row' ).show().removeAttr( 'style' );
}
// Rename core strings.
$( 'input[name="comment_status"]' ).parent().find( '.checkbox-title' ).text( woocommerce_quick_edit.strings.allow_reviews );
}
);
$( '#the-list' ).on(
'change',
'.inline-edit-row input[name="_manage_stock"]',
function() {
if ( $( this ).is( ':checked' ) ) {
$( '.stock_qty_field, .backorder_field', '.inline-edit-row' ).show().removeAttr( 'style' );
$( '.stock_status_field' ).hide();
} else {
$( '.stock_qty_field, .backorder_field', '.inline-edit-row' ).hide();
$( '.stock_status_field' ).show().removeAttr( 'style' );
}
}
);
$( '#wpbody' ).on(
'click',
'#doaction, #doaction2',
function() {
$( 'input.text', '.inline-edit-row' ).val( '' );
$( '#woocommerce-fields' ).find( 'select' ).prop( 'selectedIndex', 0 );
$( '#woocommerce-fields-bulk' ).find( '.inline-edit-group .change-input' ).hide();
}
);
$( '#wpbody' ).on(
'change',
'#woocommerce-fields-bulk .inline-edit-group .change_to',
function() {
if ( 0 < $( this ).val() ) {
$( this ).closest( 'div' ).find( '.change-input' ).show();
} else {
$( this ).closest( 'div' ).find( '.change-input' ).hide();
}
}
);
$( '#wpbody' ).on(
'click',
'.trash-product',
function() {
return window.confirm( woocommerce_admin.i18n_delete_product_notice );
}
);
}
);

1
assets/js/admin/quick-edit.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(g){g("#the-list").on("click",".editinline",function(){inlineEditPost.revert();var e=(e=g(this).closest("tr").attr("id")).replace("post-",""),t=g("#woocommerce_inline_"+e),i=t.find(".sku").text(),n=t.find(".regular_price").text(),o=t.find(".sale_price ").text(),d=t.find(".weight").text(),s=t.find(".length").text(),l=t.find(".width").text(),c=t.find(".height").text(),a=t.find(".shipping_class").text(),r=t.find(".visibility").text(),_=t.find(".stock_status").text(),p=t.find(".stock").text(),m=t.find(".featured").text(),u=t.find(".manage_stock").text(),f=t.find(".menu_order").text(),w=t.find(".tax_status").text(),h=t.find(".tax_class").text(),k=t.find(".backorders").text(),e=t.find(".product_type").text(),n=n.replace(".",woocommerce_admin.mon_decimal_point),o=o.replace(".",woocommerce_admin.mon_decimal_point);g('input[name="_sku"]',".inline-edit-row").val(i),g('input[name="_regular_price"]',".inline-edit-row").val(n),g('input[name="_sale_price"]',".inline-edit-row").val(o),g('input[name="_weight"]',".inline-edit-row").val(d),g('input[name="_length"]',".inline-edit-row").val(s),g('input[name="_width"]',".inline-edit-row").val(l),g('input[name="_height"]',".inline-edit-row").val(c),g('select[name="_shipping_class"] option:selected',".inline-edit-row").attr("selected",!1).trigger("change"),g('select[name="_shipping_class"] option[value="'+a+'"]').attr("selected","selected").trigger("change"),g('input[name="_stock"]',".inline-edit-row").val(p),g('input[name="menu_order"]',".inline-edit-row").val(f),g('select[name="_tax_status"] option, select[name="_tax_class"] option, select[name="_visibility"] option, select[name="_stock_status"] option, select[name="_backorders"] option').prop("selected",!1).removeAttr("selected");f="variable"===e;g('select[name="_stock_status"] ~ .wc-quick-edit-warning',".inline-edit-row").toggle(f),g('select[name="_stock_status"] option[value="'+(f?"":_)+'"]',".inline-edit-row").attr("selected","selected"),g('select[name="_tax_status"] option[value="'+w+'"]',".inline-edit-row").attr("selected","selected"),g('select[name="_tax_class"] option[value="'+h+'"]',".inline-edit-row").attr("selected","selected"),g('select[name="_visibility"] option[value="'+r+'"]',".inline-edit-row").attr("selected","selected"),g('select[name="_backorders"] option[value="'+k+'"]',".inline-edit-row").attr("selected","selected"),"yes"===m?g('input[name="_featured"]',".inline-edit-row").prop("checked",!0):g('input[name="_featured"]',".inline-edit-row").prop("checked",!1);k=t.find(".product_is_virtual").text(),m="external"!==e,t="external"!==e&&"grouped"!==e;g(".stock_fields, .manage_stock_field, .stock_status_field, .backorder_field").show(),t?"yes"===u?(g(".stock_qty_field, .backorder_field",".inline-edit-row").show().removeAttr("style"),g(".stock_status_field").hide(),g(".manage_stock_field input").prop("checked",!0)):(g(".stock_qty_field, .backorder_field",".inline-edit-row").hide(),g(".stock_status_field").show().removeAttr("style"),g(".manage_stock_field input").prop("checked",!1)):g(m?".stock_fields, .manage_stock_field, .backorder_field":".stock_fields, .manage_stock_field, .stock_status_field, .backorder_field").hide(),"simple"===e||"external"===e?g(".price_fields",".inline-edit-row").show().removeAttr("style"):g(".price_fields",".inline-edit-row").hide(),"yes"===k?g(".dimension_fields",".inline-edit-row").hide():g(".dimension_fields",".inline-edit-row").show().removeAttr("style"),g('input[name="comment_status"]').parent().find(".checkbox-title").text(woocommerce_quick_edit.strings.allow_reviews)}),g("#the-list").on("change",'.inline-edit-row input[name="_manage_stock"]',function(){g(this).is(":checked")?(g(".stock_qty_field, .backorder_field",".inline-edit-row").show().removeAttr("style"),g(".stock_status_field").hide()):(g(".stock_qty_field, .backorder_field",".inline-edit-row").hide(),g(".stock_status_field").show().removeAttr("style"))}),g("#wpbody").on("click","#doaction, #doaction2",function(){g("input.text",".inline-edit-row").val(""),g("#woocommerce-fields").find("select").prop("selectedIndex",0),g("#woocommerce-fields-bulk").find(".inline-edit-group .change-input").hide()}),g("#wpbody").on("change","#woocommerce-fields-bulk .inline-edit-group .change_to",function(){0<g(this).val()?g(this).closest("div").find(".change-input").show():g(this).closest("div").find(".change-input").hide()}),g("#wpbody").on("click",".trash-product",function(){return window.confirm(woocommerce_admin.i18n_delete_product_notice)})});

257
assets/js/admin/reports.js Normal file
View File

@ -0,0 +1,257 @@
jQuery(function( $ ) {
function showTooltip( x, y, contents ) {
$( '<div class="chart-tooltip">' + contents + '</div>' ).css( {
top: y - 16,
left: x + 20
}).appendTo( 'body' ).fadeIn( 200 );
}
var prev_data_index = null;
var prev_series_index = null;
$( '.chart-placeholder' ).on( 'plothover', function ( event, pos, item ) {
if ( item ) {
if ( prev_data_index !== item.dataIndex || prev_series_index !== item.seriesIndex ) {
prev_data_index = item.dataIndex;
prev_series_index = item.seriesIndex;
$( '.chart-tooltip' ).remove();
if ( item.series.points.show || item.series.enable_tooltip ) {
var y = item.series.data[item.dataIndex][1],
tooltip_content = '';
if ( item.series.prepend_label ) {
tooltip_content = tooltip_content + item.series.label + ': ';
}
if ( item.series.prepend_tooltip ) {
tooltip_content = tooltip_content + item.series.prepend_tooltip;
}
tooltip_content = tooltip_content + y;
if ( item.series.append_tooltip ) {
tooltip_content = tooltip_content + item.series.append_tooltip;
}
if ( item.series.pie.show ) {
showTooltip( pos.pageX, pos.pageY, tooltip_content );
} else {
showTooltip( item.pageX, item.pageY, tooltip_content );
}
}
}
} else {
$( '.chart-tooltip' ).remove();
prev_data_index = null;
}
});
$( '.wc_sparkline.bars' ).each( function() {
var chart_data = $( this ).data( 'sparkline' );
var options = {
grid: {
show: false
}
};
// main series
var series = [{
data: chart_data,
color: $( this ).data( 'color' ),
bars: {
fillColor: $( this ).data( 'color' ),
fill: true,
show: true,
lineWidth: 1,
barWidth: $( this ).data( 'barwidth' ),
align: 'center'
},
shadowSize: 0
}];
// draw the sparkline
$.plot( $( this ), series, options );
});
$( '.wc_sparkline.lines' ).each( function() {
var chart_data = $( this ).data( 'sparkline' );
var options = {
grid: {
show: false
}
};
// main series
var series = [{
data: chart_data,
color: $( this ).data( 'color' ),
lines: {
fill: false,
show: true,
lineWidth: 1,
align: 'center'
},
shadowSize: 0
}];
// draw the sparkline
$.plot( $( this ), series, options );
});
var dates = $( '.range_datepicker' ).datepicker({
changeMonth: true,
changeYear: true,
defaultDate: '',
dateFormat: 'yy-mm-dd',
numberOfMonths: 1,
minDate: '-20Y',
maxDate: '+1D',
showButtonPanel: true,
showOn: 'focus',
buttonImageOnly: true,
onSelect: function() {
var option = $( this ).is( '.from' ) ? 'minDate' : 'maxDate',
date = $( this ).datepicker( 'getDate' );
dates.not( this ).datepicker( 'option', option, date );
}
});
var a = document.createElement( 'a' );
if ( typeof a.download === 'undefined' ) {
$( '.export_csv' ).hide();
}
// Export
$( '.export_csv' ).on( 'click', function() {
var exclude_series = $( this ).data( 'exclude_series' ) || '';
exclude_series = exclude_series.toString();
exclude_series = exclude_series.split( ',' );
var xaxes_label = $( this ).data( 'xaxes' );
var groupby = $( this ) .data( 'groupby' );
var index_type = $( this ).data( 'index_type' );
var export_format = $( this ).data( 'export' );
var csv_data = '';
var s, series_data, d;
if ( 'table' === export_format ) {
$( this ).offsetParent().find( 'thead tr,tbody tr' ).each( function() {
$( this ).find( 'th, td' ).each( function() {
var value = $( this ).text();
value = value.replace( '[?]', '' ).replace( '#', '' );
csv_data += '"' + value + '"' + ',';
});
csv_data = csv_data.substring( 0, csv_data.length - 1 );
csv_data += '\n';
});
$( this ).offsetParent().find( 'tfoot tr' ).each( function() {
$( this ).find( 'th, td' ).each( function() {
var value = $( this ).text();
value = value.replace( '[?]', '' ).replace( '#', '' );
csv_data += '"' + value + '"' + ',';
if ( $( this ).attr( 'colspan' ) > 0 ) {
for ( i = 1; i < $(this).attr('colspan'); i++ ) {
csv_data += '"",';
}
}
});
csv_data = csv_data.substring( 0, csv_data.length - 1 );
csv_data += '\n';
});
} else {
if ( ! window.main_chart ) {
return false;
}
var the_series = window.main_chart.getData();
var series = [];
csv_data += '"' + xaxes_label + '",';
$.each( the_series, function( index, value ) {
if ( ! exclude_series || $.inArray( index.toString(), exclude_series ) === -1 ) {
series.push( value );
}
});
// CSV Headers
for ( s = 0; s < series.length; ++s ) {
csv_data += '"' + series[s].label + '",';
}
csv_data = csv_data.substring( 0, csv_data.length - 1 );
csv_data += '\n';
// Get x axis values
var xaxis = {};
for ( s = 0; s < series.length; ++s ) {
series_data = series[s].data;
for ( d = 0; d < series_data.length; ++d ) {
xaxis[series_data[d][0]] = [];
// Zero values to start
for ( var i = 0; i < series.length; ++i ) {
xaxis[series_data[d][0]].push(0);
}
}
}
// Add chart data
for ( s = 0; s < series.length; ++s ) {
series_data = series[s].data;
for ( d = 0; d < series_data.length; ++d ) {
xaxis[series_data[d][0]][s] = series_data[d][1];
}
}
// Loop data and output to csv string
$.each( xaxis, function( index, value ) {
var date = new Date( parseInt( index, 10 ) );
if ( 'none' === index_type ) {
csv_data += '"' + index + '",';
} else {
if ( groupby === 'day' ) {
csv_data += '"' +
date.getUTCFullYear() +
'-' +
parseInt( date.getUTCMonth() + 1, 10 ) +
'-' +
date.getUTCDate() +
'",';
} else {
csv_data += '"' + date.getUTCFullYear() + '-' + parseInt( date.getUTCMonth() + 1, 10 ) + '",';
}
}
for ( var d = 0; d < value.length; ++d ) {
var val = value[d];
if ( Math.round( val ) !== val ) {
val = parseFloat( val );
val = val.toFixed( 2 );
}
csv_data += '"' + val + '",';
}
csv_data = csv_data.substring( 0, csv_data.length - 1 );
csv_data += '\n';
} );
}
csv_data = 'data:text/csv;charset=utf-8,\uFEFF' + encodeURIComponent( csv_data );
// Set data as href and return
$( this ).attr( 'href', csv_data );
return true;
});
});

1
assets/js/admin/reports.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(p){function r(t,e,a){p('<div class="chart-tooltip">'+a+"</div>").css({top:e-16,left:t+20}).appendTo("body").fadeIn(200)}var o=null,s=null;p(".chart-placeholder").on("plothover",function(t,e,a){var n,i;a?o===a.dataIndex&&s===a.seriesIndex||(o=a.dataIndex,s=a.seriesIndex,p(".chart-tooltip").remove(),(a.series.points.show||a.series.enable_tooltip)&&(n=a.series.data[a.dataIndex][1],i="",a.series.prepend_label&&(i=i+a.series.label+": "),a.series.prepend_tooltip&&(i+=a.series.prepend_tooltip),i+=n,a.series.append_tooltip&&(i+=a.series.append_tooltip),a.series.pie.show?r(e.pageX,e.pageY,i):r(a.pageX,a.pageY,i))):(p(".chart-tooltip").remove(),o=null)}),p(".wc_sparkline.bars").each(function(){var t=[{data:p(this).data("sparkline"),color:p(this).data("color"),bars:{fillColor:p(this).data("color"),fill:!0,show:!0,lineWidth:1,barWidth:p(this).data("barwidth"),align:"center"},shadowSize:0}];p.plot(p(this),t,{grid:{show:!1}})}),p(".wc_sparkline.lines").each(function(){var t=[{data:p(this).data("sparkline"),color:p(this).data("color"),lines:{fill:!1,show:!0,lineWidth:1,align:"center"},shadowSize:0}];p.plot(p(this),t,{grid:{show:!1}})});var a=p(".range_datepicker").datepicker({changeMonth:!0,changeYear:!0,defaultDate:"",dateFormat:"yy-mm-dd",numberOfMonths:1,minDate:"-20Y",maxDate:"+1D",showButtonPanel:!0,showOn:"focus",buttonImageOnly:!0,onSelect:function(){var t=p(this).is(".from")?"minDate":"maxDate",e=p(this).datepicker("getDate");a.not(this).datepicker("option",t,e)}});"undefined"==typeof document.createElement("a").download&&p(".export_csv").hide(),p(".export_csv").on("click",function(){var a=p(this).data("exclude_series")||"";a=(a=a.toString()).split(",");var t,e,n=p(this).data("xaxes"),r=p(this).data("groupby"),o=p(this).data("index_type"),i=p(this).data("export"),s="";if("table"===i)p(this).offsetParent().find("thead tr,tbody tr").each(function(){p(this).find("th, td").each(function(){var t=(t=p(this).text()).replace("[?]","").replace("#","");s+='"'+t+'",'}),s=s.substring(0,s.length-1),s+="\n"}),p(this).offsetParent().find("tfoot tr").each(function(){p(this).find("th, td").each(function(){var t=(t=p(this).text()).replace("[?]","").replace("#","");if(s+='"'+t+'",',0<p(this).attr("colspan"))for(c=1;c<p(this).attr("colspan");c++)s+='"",'}),s=s.substring(0,s.length-1),s+="\n"});else{if(!window.main_chart)return!1;var i=window.main_chart.getData(),l=[];for(s+='"'+n+'",',p.each(i,function(t,e){a&&-1!==p.inArray(t.toString(),a)||l.push(e)}),d=0;d<l.length;++d)s+='"'+l[d].label+'",';s=s.substring(0,s.length-1),s+="\n";for(var h={},d=0;d<l.length;++d)for(t=l[d].data,e=0;e<t.length;++e){h[t[e][0]]=[];for(var c=0;c<l.length;++c)h[t[e][0]].push(0)}for(d=0;d<l.length;++d)for(t=l[d].data,e=0;e<t.length;++e)h[t[e][0]][d]=t[e][1];p.each(h,function(t,e){var a=new Date(parseInt(t,10));s+="none"===o?'"'+t+'",':"day"===r?'"'+a.getUTCFullYear()+"-"+parseInt(a.getUTCMonth()+1,10)+"-"+a.getUTCDate()+'",':'"'+a.getUTCFullYear()+"-"+parseInt(a.getUTCMonth()+1,10)+'",';for(var n=0;n<e.length;++n){var i=e[n];Math.round(i)!==i&&(i=(i=parseFloat(i)).toFixed(2)),s+='"'+i+'",'}s=s.substring(0,s.length-1),s+="\n"})}return s="data:text/csv;charset=utf-8,\ufeff"+encodeURIComponent(s),p(this).attr("href",s),!0})});

View File

@ -0,0 +1,383 @@
/* global htmlSettingsTaxLocalizeScript, ajaxurl */
/**
* Used by woocommerce/includes/admin/settings/views/html-settings-tax.php
*/
( function( $, data, wp, ajaxurl ) {
$( function() {
if ( ! String.prototype.trim ) {
String.prototype.trim = function () {
return this.replace( /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '' );
};
}
var rowTemplate = wp.template( 'wc-tax-table-row' ),
rowTemplateEmpty = wp.template( 'wc-tax-table-row-empty' ),
paginationTemplate = wp.template( 'wc-tax-table-pagination' ),
$table = $( '.wc_tax_rates' ),
$tbody = $( '#rates' ),
$save_button = $( ':input[name="save"]' ),
$pagination = $( '#rates-pagination' ),
$search_field = $( '#rates-search .wc-tax-rates-search-field' ),
$submit = $( '.submit .button-primary[type=submit]' ),
WCTaxTableModelConstructor = Backbone.Model.extend({
changes: {},
setRateAttribute: function( rateID, attribute, value ) {
var rates = _.indexBy( this.get( 'rates' ), 'tax_rate_id' ),
changes = {};
if ( rates[ rateID ][ attribute ] !== value ) {
changes[ rateID ] = {};
changes[ rateID ][ attribute ] = value;
rates[ rateID ][ attribute ] = value;
}
this.logChanges( changes );
},
logChanges: function( changedRows ) {
var changes = this.changes || {};
_.each( changedRows, function( row, id ) {
changes[ id ] = _.extend( changes[ id ] || {
tax_rate_id : id
}, row );
} );
this.changes = changes;
this.trigger( 'change:rates' );
},
getFilteredRates: function() {
var rates = this.get( 'rates' ),
search = $search_field.val().toLowerCase();
if ( search.length ) {
rates = _.filter( rates, function( rate ) {
var search_text = _.toArray( rate ).join( ' ' ).toLowerCase();
return ( -1 !== search_text.indexOf( search ) );
} );
}
rates = _.sortBy( rates, function( rate ) {
return parseInt( rate.tax_rate_order, 10 );
} );
return rates;
},
block: function() {
$( '.wc_tax_rates' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
unblock: function() {
$( '.wc_tax_rates' ).unblock();
},
save: function() {
var self = this;
self.block();
Backbone.ajax({
method: 'POST',
dataType: 'json',
url: ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_tax_rates_save_changes',
data: {
current_class: data.current_class,
wc_tax_nonce: data.wc_tax_nonce,
changes: self.changes
},
success: function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
WCTaxTableModelInstance.set( 'rates', response.data.rates );
WCTaxTableModelInstance.trigger( 'change:rates' );
WCTaxTableModelInstance.changes = {};
WCTaxTableModelInstance.trigger( 'saved:rates' );
// Reload view.
WCTaxTableInstance.render();
}
self.unblock();
}
});
}
} ),
WCTaxTableViewConstructor = Backbone.View.extend({
rowTemplate: rowTemplate,
per_page: data.limit,
page: data.page,
initialize: function() {
var qty_pages = Math.ceil( _.toArray( this.model.get( 'rates' ) ).length / this.per_page );
this.qty_pages = 0 === qty_pages ? 1 : qty_pages;
this.page = this.sanitizePage( data.page );
this.listenTo( this.model, 'change:rates', this.setUnloadConfirmation );
this.listenTo( this.model, 'saved:rates', this.clearUnloadConfirmation );
$tbody.on( 'change autocompletechange', ':input', { view: this }, this.updateModelOnChange );
$search_field.on( 'keyup search', { view: this }, this.onSearchField );
$pagination.on( 'click', 'a', { view: this }, this.onPageChange );
$pagination.on( 'change', 'input', { view: this }, this.onPageChange );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$submit.on( 'click', { view: this }, this.onSubmit );
$save_button.prop( 'disabled', true );
// Can bind these directly to the buttons, as they won't get overwritten.
$table.find( '.insert' ).on( 'click', { view: this }, this.onAddNewRow );
$table.find( '.remove_tax_rates' ).on( 'click', { view: this }, this.onDeleteRow );
$table.find( '.export' ).on( 'click', { view: this }, this.onExport );
},
render: function() {
var rates = this.model.getFilteredRates(),
qty_rates = _.size( rates ),
qty_pages = Math.ceil( qty_rates / this.per_page ),
first_index = 0 === qty_rates ? 0 : this.per_page * ( this.page - 1 ),
last_index = this.per_page * this.page,
paged_rates = _.toArray( rates ).slice( first_index, last_index ),
view = this;
// Blank out the contents.
this.$el.empty();
if ( paged_rates.length ) {
// Populate $tbody with the current page of results.
$.each( paged_rates, function( id, rowData ) {
view.$el.append( view.rowTemplate( rowData ) );
} );
} else {
view.$el.append( rowTemplateEmpty() );
}
// Initialize autocomplete for countries.
this.$el.find( 'td.country input' ).autocomplete({
source: data.countries,
minLength: 2
});
// Initialize autocomplete for states.
this.$el.find( 'td.state input' ).autocomplete({
source: data.states,
minLength: 3
});
// Postcode and city don't have `name` values by default.
// They're only created if the contents changes, to save on database queries (I think)
this.$el.find( 'td.postcode input, td.city input' ).on( 'change', function() {
$( this ).attr( 'name', $( this ).data( 'name' ) );
});
if ( qty_pages > 1 ) {
// We've now displayed our initial page, time to render the pagination box.
$pagination.html( paginationTemplate( {
qty_rates: qty_rates,
current_page: this.page,
qty_pages: qty_pages
} ) );
} else {
$pagination.empty();
view.page = 1;
}
},
updateUrl: function() {
if ( ! window.history.replaceState ) {
return;
}
var url = data.base_url,
search = $search_field.val();
if ( 1 < this.page ) {
url += '&p=' + encodeURIComponent( this.page );
}
if ( search.length ) {
url += '&s=' + encodeURIComponent( search );
}
window.history.replaceState( {}, '', url );
},
onSubmit: function( event ) {
event.data.view.model.save();
event.preventDefault();
},
onAddNewRow: function( event ) {
var view = event.data.view,
model = view.model,
rates = _.indexBy( model.get( 'rates' ), 'tax_rate_id' ),
changes = {},
size = _.size( rates ),
newRow = _.extend( {}, data.default_rate, {
tax_rate_id: 'new-' + size + '-' + Date.now(),
newRow: true
} ),
$current, current_id, current_order, rates_to_reorder, reordered_rates;
$current = $tbody.children( '.current' );
if ( $current.length ) {
current_id = $current.last().data( 'id' );
current_order = parseInt( rates[ current_id ].tax_rate_order, 10 );
newRow.tax_rate_order = 1 + current_order;
rates_to_reorder = _.filter( rates, function( rate ) {
if ( parseInt( rate.tax_rate_order, 10 ) > current_order ) {
return true;
}
return false;
} );
reordered_rates = _.map( rates_to_reorder, function( rate ) {
rate.tax_rate_order++;
changes[ rate.tax_rate_id ] = _.extend(
changes[ rate.tax_rate_id ] || {}, { tax_rate_order : rate.tax_rate_order }
);
return rate;
} );
} else {
newRow.tax_rate_order = 1 + _.max(
_.pluck( rates, 'tax_rate_order' ),
function ( val ) {
// Cast them all to integers, because strings compare funky. Sighhh.
return parseInt( val, 10 );
}
);
// Move the last page
view.page = view.qty_pages;
}
rates[ newRow.tax_rate_id ] = newRow;
changes[ newRow.tax_rate_id ] = newRow;
model.set( 'rates', rates );
model.logChanges( changes );
view.render();
},
onDeleteRow: function( event ) {
var view = event.data.view,
model = view.model,
rates = _.indexBy( model.get( 'rates' ), 'tax_rate_id' ),
changes = {},
$current, current_id;
event.preventDefault();
if ( $current = $tbody.children( '.current' ) ) {
$current.each(function(){
current_id = $( this ).data('id');
delete rates[ current_id ];
changes[ current_id ] = _.extend( changes[ current_id ] || {}, { deleted : 'deleted' } );
});
model.set( 'rates', rates );
model.logChanges( changes );
view.render();
} else {
window.alert( data.strings.no_rows_selected );
}
},
onSearchField: function( event ){
event.data.view.updateUrl();
event.data.view.render();
},
onPageChange: function( event ) {
var $target = $( event.currentTarget );
event.preventDefault();
event.data.view.page = $target.data( 'goto' ) ? $target.data( 'goto' ) : $target.val();
event.data.view.render();
event.data.view.updateUrl();
},
onExport: function( event ) {
var csv_data = 'data:application/csv;charset=utf-8,' + data.strings.csv_data_cols.join(',') + '\n';
$.each( event.data.view.model.getFilteredRates(), function( id, rowData ) {
var row = '';
row += rowData.tax_rate_country + ',';
row += rowData.tax_rate_state + ',';
row += ( rowData.postcode ? rowData.postcode.join( '; ' ) : '' ) + ',';
row += ( rowData.city ? rowData.city.join( '; ' ) : '' ) + ',';
row += rowData.tax_rate + ',';
row += rowData.tax_rate_name + ',';
row += rowData.tax_rate_priority + ',';
row += rowData.tax_rate_compound + ',';
row += rowData.tax_rate_shipping + ',';
row += data.current_class;
csv_data += row + '\n';
});
$( this ).attr( 'href', encodeURI( csv_data ) );
return true;
},
setUnloadConfirmation: function() {
this.needsUnloadConfirm = true;
$save_button.prop( 'disabled', false );
},
clearUnloadConfirmation: function() {
this.needsUnloadConfirm = false;
$save_button.prop( 'disabled', true );
},
unloadConfirmation: function( event ) {
if ( event.data.view.needsUnloadConfirm ) {
event.returnValue = data.strings.unload_confirmation_msg;
window.event.returnValue = data.strings.unload_confirmation_msg;
return data.strings.unload_confirmation_msg;
}
},
updateModelOnChange: function( event ) {
var model = event.data.view.model,
$target = $( event.target ),
id = $target.closest( 'tr' ).data( 'id' ),
attribute = $target.data( 'attribute' ),
val = $target.val();
if ( 'city' === attribute || 'postcode' === attribute ) {
val = val.split( ';' );
val = $.map( val, function( thing ) {
return thing.trim();
});
}
if ( 'tax_rate_compound' === attribute || 'tax_rate_shipping' === attribute ) {
if ( $target.is( ':checked' ) ) {
val = 1;
} else {
val = 0;
}
}
model.setRateAttribute( id, attribute, val );
},
sanitizePage: function( page_num ) {
page_num = parseInt( page_num, 10 );
if ( page_num < 1 ) {
page_num = 1;
} else if ( page_num > this.qty_pages ) {
page_num = this.qty_pages;
}
return page_num;
}
} ),
WCTaxTableModelInstance = new WCTaxTableModelConstructor({
rates: data.rates
} ),
WCTaxTableInstance = new WCTaxTableViewConstructor({
model: WCTaxTableModelInstance,
el: '#rates'
} );
WCTaxTableInstance.render();
});
})( jQuery, htmlSettingsTaxLocalizeScript, wp, ajaxurl );

File diff suppressed because one or more lines are too long

183
assets/js/admin/settings.js Normal file
View File

@ -0,0 +1,183 @@
/* global woocommerce_settings_params, wp */
( function( $, params, wp ) {
$( function() {
// Sell Countries
$( 'select#woocommerce_allowed_countries' ).on( 'change', function() {
if ( 'specific' === $( this ).val() ) {
$( this ).closest('tr').next( 'tr' ).hide();
$( this ).closest('tr').next().next( 'tr' ).show();
} else if ( 'all_except' === $( this ).val() ) {
$( this ).closest('tr').next( 'tr' ).show();
$( this ).closest('tr').next().next( 'tr' ).hide();
} else {
$( this ).closest('tr').next( 'tr' ).hide();
$( this ).closest('tr').next().next( 'tr' ).hide();
}
}).trigger( 'change' );
// Ship Countries
$( 'select#woocommerce_ship_to_countries' ).on( 'change', function() {
if ( 'specific' === $( this ).val() ) {
$( this ).closest('tr').next( 'tr' ).show();
} else {
$( this ).closest('tr').next( 'tr' ).hide();
}
}).trigger( 'change' );
// Stock management
$( 'input#woocommerce_manage_stock' ).on( 'change', function() {
if ( $( this ).is(':checked') ) {
$( this ).closest('tbody').find( '.manage_stock_field' ).closest( 'tr' ).show();
} else {
$( this ).closest('tbody').find( '.manage_stock_field' ).closest( 'tr' ).hide();
}
}).trigger( 'change' );
// Color picker
$( '.colorpick' )
.iris({
change: function( event, ui ) {
$( this ).parent().find( '.colorpickpreview' ).css({ backgroundColor: ui.color.toString() });
},
hide: true,
border: true
})
.on( 'click focus', function( event ) {
event.stopPropagation();
$( '.iris-picker' ).hide();
$( this ).closest( 'td' ).find( '.iris-picker' ).show();
$( this ).data( 'originalValue', $( this ).val() );
})
.on( 'change', function() {
if ( $( this ).is( '.iris-error' ) ) {
var original_value = $( this ).data( 'originalValue' );
if ( original_value.match( /^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/ ) ) {
$( this ).val( $( this ).data( 'originalValue' ) ).trigger( 'change' );
} else {
$( this ).val( '' ).trigger( 'change' );
}
}
});
$( 'body' ).on( 'click', function() {
$( '.iris-picker' ).hide();
});
// Edit prompt
$( function() {
var changed = false;
$( 'input, textarea, select, checkbox' ).on( 'change', function() {
if ( ! changed ) {
window.onbeforeunload = function() {
return params.i18n_nav_warning;
};
changed = true;
}
});
$( '.submit :input' ).on( 'click', function() {
window.onbeforeunload = '';
});
});
// Sorting
$( 'table.wc_gateways tbody, table.wc_shipping tbody' ).sortable({
items: 'tr',
cursor: 'move',
axis: 'y',
handle: 'td.sort',
scrollSensitivity: 40,
helper: function( event, ui ) {
ui.children().each( function() {
$( this ).width( $( this ).width() );
});
ui.css( 'left', '0' );
return ui;
},
start: function( event, ui ) {
ui.item.css( 'background-color', '#f6f6f6' );
},
stop: function( event, ui ) {
ui.item.removeAttr( 'style' );
ui.item.trigger( 'updateMoveButtons' );
}
});
// Select all/none
$( '.woocommerce' ).on( 'click', '.select_all', function() {
$( this ).closest( 'td' ).find( 'select option' ).prop( 'selected', true );
$( this ).closest( 'td' ).find( 'select' ).trigger( 'change' );
return false;
});
$( '.woocommerce' ).on( 'click', '.select_none', function() {
$( this ).closest( 'td' ).find( 'select option' ).prop( 'selected', false );
$( this ).closest( 'td' ).find( 'select' ).trigger( 'change' );
return false;
});
// Re-order buttons.
$( '.wc-item-reorder-nav').find( '.wc-move-up, .wc-move-down' ).on( 'click', function() {
var moveBtn = $( this ),
$row = moveBtn.closest( 'tr' );
moveBtn.trigger( 'focus' );
var isMoveUp = moveBtn.is( '.wc-move-up' ),
isMoveDown = moveBtn.is( '.wc-move-down' );
if ( isMoveUp ) {
var $previewRow = $row.prev( 'tr' );
if ( $previewRow && $previewRow.length ) {
$previewRow.before( $row );
wp.a11y.speak( params.i18n_moved_up );
}
} else if ( isMoveDown ) {
var $nextRow = $row.next( 'tr' );
if ( $nextRow && $nextRow.length ) {
$nextRow.after( $row );
wp.a11y.speak( params.i18n_moved_down );
}
}
moveBtn.trigger( 'focus' ); // Re-focus after the container was moved.
moveBtn.closest( 'table' ).trigger( 'updateMoveButtons' );
} );
$( '.wc-item-reorder-nav').closest( 'table' ).on( 'updateMoveButtons', function() {
var table = $( this ),
lastRow = $( this ).find( 'tbody tr:last' ),
firstRow = $( this ).find( 'tbody tr:first' );
table.find( '.wc-item-reorder-nav .wc-move-disabled' ).removeClass( 'wc-move-disabled' )
.attr( { 'tabindex': '0', 'aria-hidden': 'false' } );
firstRow.find( '.wc-item-reorder-nav .wc-move-up' ).addClass( 'wc-move-disabled' )
.attr( { 'tabindex': '-1', 'aria-hidden': 'true' } );
lastRow.find( '.wc-item-reorder-nav .wc-move-down' ).addClass( 'wc-move-disabled' )
.attr( { 'tabindex': '-1', 'aria-hidden': 'true' } );
} );
$( '.wc-item-reorder-nav').closest( 'table' ).trigger( 'updateMoveButtons' );
$( '.submit button' ).on( 'click', function() {
if (
$( 'select#woocommerce_allowed_countries' ).val() === 'specific' &&
! $( '[name="woocommerce_specific_allowed_countries[]"]' ).val()
) {
if ( window.confirm( woocommerce_settings_params.i18n_no_specific_countries_selected ) ) {
return true;
}
return false;
}
} );
});
})( jQuery, woocommerce_settings_params, wp );

1
assets/js/admin/settings.min.js vendored Normal file
View File

@ -0,0 +1 @@
!function(n,c,s){n(function(){n("select#woocommerce_allowed_countries").on("change",function(){"specific"===n(this).val()?(n(this).closest("tr").next("tr").hide(),n(this).closest("tr").next().next("tr").show()):("all_except"===n(this).val()?n(this).closest("tr").next("tr").show():n(this).closest("tr").next("tr").hide(),n(this).closest("tr").next().next("tr").hide())}).trigger("change"),n("select#woocommerce_ship_to_countries").on("change",function(){"specific"===n(this).val()?n(this).closest("tr").next("tr").show():n(this).closest("tr").next("tr").hide()}).trigger("change"),n("input#woocommerce_manage_stock").on("change",function(){n(this).is(":checked")?n(this).closest("tbody").find(".manage_stock_field").closest("tr").show():n(this).closest("tbody").find(".manage_stock_field").closest("tr").hide()}).trigger("change"),n(".colorpick").iris({change:function(e,t){n(this).parent().find(".colorpickpreview").css({backgroundColor:t.color.toString()})},hide:!0,border:!0}).on("click focus",function(e){e.stopPropagation(),n(".iris-picker").hide(),n(this).closest("td").find(".iris-picker").show(),n(this).data("originalValue",n(this).val())}).on("change",function(){n(this).is(".iris-error")&&(n(this).data("originalValue").match(/^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/)?n(this).val(n(this).data("originalValue")):n(this).val("")).trigger("change")}),n("body").on("click",function(){n(".iris-picker").hide()}),n(function(){var e=!1;n("input, textarea, select, checkbox").on("change",function(){e||(window.onbeforeunload=function(){return c.i18n_nav_warning},e=!0)}),n(".submit :input").on("click",function(){window.onbeforeunload=""})}),n("table.wc_gateways tbody, table.wc_shipping tbody").sortable({items:"tr",cursor:"move",axis:"y",handle:"td.sort",scrollSensitivity:40,helper:function(e,t){return t.children().each(function(){n(this).width(n(this).width())}),t.css("left","0"),t},start:function(e,t){t.item.css("background-color","#f6f6f6")},stop:function(e,t){t.item.removeAttr("style"),t.item.trigger("updateMoveButtons")}}),n(".woocommerce").on("click",".select_all",function(){return n(this).closest("td").find("select option").prop("selected",!0),n(this).closest("td").find("select").trigger("change"),!1}),n(".woocommerce").on("click",".select_none",function(){return n(this).closest("td").find("select option").prop("selected",!1),n(this).closest("td").find("select").trigger("change"),!1}),n(".wc-item-reorder-nav").find(".wc-move-up, .wc-move-down").on("click",function(){var e=n(this),t=e.closest("tr");e.trigger("focus");var i=e.is(".wc-move-up"),o=e.is(".wc-move-down");i?(i=t.prev("tr"))&&i.length&&(i.before(t),s.a11y.speak(c.i18n_moved_up)):!o||(o=t.next("tr"))&&o.length&&(o.after(t),s.a11y.speak(c.i18n_moved_down)),e.trigger("focus"),e.closest("table").trigger("updateMoveButtons")}),n(".wc-item-reorder-nav").closest("table").on("updateMoveButtons",function(){var e=n(this),t=n(this).find("tbody tr:last"),i=n(this).find("tbody tr:first");e.find(".wc-item-reorder-nav .wc-move-disabled").removeClass("wc-move-disabled").attr({tabindex:"0","aria-hidden":"false"}),i.find(".wc-item-reorder-nav .wc-move-up").addClass("wc-move-disabled").attr({tabindex:"-1","aria-hidden":"true"}),t.find(".wc-item-reorder-nav .wc-move-down").addClass("wc-move-disabled").attr({tabindex:"-1","aria-hidden":"true"})}),n(".wc-item-reorder-nav").closest("table").trigger("updateMoveButtons"),n(".submit button").on("click",function(){if("specific"===n("select#woocommerce_allowed_countries").val()&&!n('[name="woocommerce_specific_allowed_countries[]"]').val())return!!window.confirm(woocommerce_settings_params.i18n_no_specific_countries_selected)})})}(jQuery,woocommerce_settings_params,wp);

View File

@ -0,0 +1,126 @@
/* global jQuery, woocommerce_admin_system_status, wcSetClipboard, wcClearClipboard */
jQuery( function ( $ ) {
/**
* Users country and state fields
*/
var wcSystemStatus = {
init: function() {
$( document.body )
.on( 'click', 'a.help_tip, a.woocommerce-help-tip', this.preventTipTipClick )
.on( 'click', 'a.debug-report', this.generateReport )
.on( 'click', '#copy-for-support', this.copyReport )
.on( 'aftercopy', '#copy-for-support', this.copySuccess )
.on( 'aftercopyfailure', '#copy-for-support', this.copyFail );
},
/**
* Prevent anchor behavior when click on TipTip.
*
* @return {Bool}
*/
preventTipTipClick: function() {
return false;
},
/**
* Generate system status report.
*
* @return {Bool}
*/
generateReport: function() {
var report = '';
$( '.wc_status_table thead, .wc_status_table tbody' ).each( function() {
if ( $( this ).is( 'thead' ) ) {
var label = $( this ).find( 'th:eq(0)' ).data( 'exportLabel' ) || $( this ).text();
report = report + '\n### ' + label.trim() + ' ###\n\n';
} else {
$( 'tr', $( this ) ).each( function() {
var label = $( this ).find( 'td:eq(0)' ).data( 'exportLabel' ) || $( this ).find( 'td:eq(0)' ).text();
var the_name = label.trim().replace( /(<([^>]+)>)/ig, '' ); // Remove HTML.
// Find value
var $value_html = $( this ).find( 'td:eq(2)' ).clone();
$value_html.find( '.private' ).remove();
$value_html.find( '.dashicons-yes' ).replaceWith( '&#10004;' );
$value_html.find( '.dashicons-no-alt, .dashicons-warning' ).replaceWith( '&#10060;' );
// Format value
var the_value = $value_html.text().trim();
var value_array = the_value.split( ', ' );
if ( value_array.length > 1 ) {
// If value have a list of plugins ','.
// Split to add new line.
var temp_line ='';
$.each( value_array, function( key, line ) {
temp_line = temp_line + line + '\n';
});
the_value = temp_line;
}
report = report + '' + the_name + ': ' + the_value + '\n';
});
}
});
try {
$( '#debug-report' ).slideDown();
$( '#debug-report' ).find( 'textarea' ).val( '`' + report + '`' ).trigger( 'focus' ).trigger( 'select' );
$( this ).fadeOut();
return false;
} catch ( e ) {
/* jshint devel: true */
console.log( e );
}
return false;
},
/**
* Copy for report.
*
* @param {Object} evt Copy event.
*/
copyReport: function( evt ) {
wcClearClipboard();
wcSetClipboard( $( '#debug-report' ).find( 'textarea' ).val(), $( this ) );
evt.preventDefault();
},
/**
* Display a "Copied!" tip when success copying
*/
copySuccess: function() {
$( '#copy-for-support' ).tipTip({
'attribute': 'data-tip',
'activation': 'focus',
'fadeIn': 50,
'fadeOut': 50,
'delay': 0
}).trigger( 'focus' );
},
/**
* Displays the copy error message when failure copying.
*/
copyFail: function() {
$( '.copy-error' ).removeClass( 'hidden' );
$( '#debug-report' ).find( 'textarea' ).trigger( 'focus' ).trigger( 'select' );
}
};
wcSystemStatus.init();
$( '.wc_status_table' ).on( 'click', '.run-tool .button', function( evt ) {
evt.stopImmediatePropagation();
return window.confirm( woocommerce_admin_system_status.run_tool_confirmation );
});
$( '#log-viewer-select' ).on( 'click', 'h2 a.page-title-action', function( evt ) {
evt.stopImmediatePropagation();
return window.confirm( woocommerce_admin_system_status.delete_log_confirmation );
});
});

1
assets/js/admin/system-status.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(n){({init:function(){n(document.body).on("click","a.help_tip, a.woocommerce-help-tip",this.preventTipTipClick).on("click","a.debug-report",this.generateReport).on("click","#copy-for-support",this.copyReport).on("aftercopy","#copy-for-support",this.copySuccess).on("aftercopyfailure","#copy-for-support",this.copyFail)},preventTipTipClick:function(){return!1},generateReport:function(){var r="";n(".wc_status_table thead, .wc_status_table tbody").each(function(){var t;n(this).is("thead")?(t=n(this).find("th:eq(0)").data("exportLabel")||n(this).text(),r=r+"\n### "+t.trim()+" ###\n\n"):n("tr",n(this)).each(function(){var t=(n(this).find("td:eq(0)").data("exportLabel")||n(this).find("td:eq(0)").text()).trim().replace(/(<([^>]+)>)/gi,""),e=n(this).find("td:eq(2)").clone();e.find(".private").remove(),e.find(".dashicons-yes").replaceWith("&#10004;"),e.find(".dashicons-no-alt, .dashicons-warning").replaceWith("&#10060;");var o,i=e.text().trim(),e=i.split(", ");1<e.length&&(o="",n.each(e,function(t,e){o=o+e+"\n"}),i=o),r=r+""+t+": "+i+"\n"})});try{return n("#debug-report").slideDown(),n("#debug-report").find("textarea").val("`"+r+"`").trigger("focus").trigger("select"),n(this).fadeOut(),!1}catch(t){console.log(t)}return!1},copyReport:function(t){wcClearClipboard(),wcSetClipboard(n("#debug-report").find("textarea").val(),n(this)),t.preventDefault()},copySuccess:function(){n("#copy-for-support").tipTip({attribute:"data-tip",activation:"focus",fadeIn:50,fadeOut:50,delay:0}).trigger("focus")},copyFail:function(){n(".copy-error").removeClass("hidden"),n("#debug-report").find("textarea").trigger("focus").trigger("select")}}).init(),n(".wc_status_table").on("click",".run-tool .button",function(t){return t.stopImmediatePropagation(),window.confirm(woocommerce_admin_system_status.run_tool_confirmation)}),n("#log-viewer-select").on("click","h2 a.page-title-action",function(t){return t.stopImmediatePropagation(),window.confirm(woocommerce_admin_system_status.delete_log_confirmation)})});

View File

@ -0,0 +1,137 @@
/*global ajaxurl, woocommerce_term_ordering_params */
/* Modifided script from the simple-page-ordering plugin */
jQuery( function( $ ) {
var table_selector = 'table.wp-list-table',
item_selector = 'tbody tr:not(.inline-edit-row)',
term_id_selector = '.column-handle input[name="term_id"]',
column_handle = '<td class="column-handle"></td>';
if ( 0 === $( table_selector ).find( '.column-handle' ).length ) {
$( table_selector ).find( 'tr:not(.inline-edit-row)' ).append( column_handle );
term_id_selector = '.check-column input';
}
$( table_selector ).find( '.column-handle' ).show();
$.wc_add_missing_sort_handles = function() {
var all_table_rows = $( table_selector ).find('tbody > tr');
var rows_with_handle = $( table_selector ).find('tbody > tr > td.column-handle').parent();
if ( all_table_rows.length !== rows_with_handle.length ) {
all_table_rows.each(function(index, elem){
if ( ! rows_with_handle.is( elem ) ) {
$( elem ).append( column_handle );
}
});
}
$( table_selector ).find( '.column-handle' ).show();
};
$( document ).ajaxComplete( function( event, request, options ) {
if (
request &&
4 === request.readyState &&
200 === request.status &&
options.data &&
( 0 <= options.data.indexOf( '_inline_edit' ) || 0 <= options.data.indexOf( 'add-tag' ) )
) {
$.wc_add_missing_sort_handles();
$( document.body ).trigger( 'init_tooltips' );
}
} );
$( table_selector ).sortable({
items: item_selector,
cursor: 'move',
handle: '.column-handle',
axis: 'y',
forcePlaceholderSize: true,
helper: 'clone',
opacity: 0.65,
placeholder: 'product-cat-placeholder',
scrollSensitivity: 40,
start: function( event, ui ) {
if ( ! ui.item.hasClass( 'alternate' ) ) {
ui.item.css( 'background-color', '#ffffff' );
}
ui.item.children( 'td, th' ).css( 'border-bottom-width', '0' );
ui.item.css( 'outline', '1px solid #aaa' );
},
stop: function( event, ui ) {
ui.item.removeAttr( 'style' );
ui.item.children( 'td, th' ).css( 'border-bottom-width', '1px' );
},
update: function( event, ui ) {
var termid = ui.item.find( term_id_selector ).val(); // this post id
var termparent = ui.item.find( '.parent' ).html(); // post parent
var prevtermid = ui.item.prev().find( term_id_selector ).val();
var nexttermid = ui.item.next().find( term_id_selector ).val();
// Can only sort in same tree
var prevtermparent, nexttermparent;
if ( prevtermid !== undefined ) {
prevtermparent = ui.item.prev().find( '.parent' ).html();
if ( prevtermparent !== termparent) {
prevtermid = undefined;
}
}
if ( nexttermid !== undefined ) {
nexttermparent = ui.item.next().find( '.parent' ).html();
if ( nexttermparent !== termparent) {
nexttermid = undefined;
}
}
// If previous and next not at same tree level, or next not at same tree level and
// the previous is the parent of the next, or just moved item beneath its own children.
if (
( prevtermid === undefined && nexttermid === undefined ) ||
( nexttermid === undefined && nexttermparent === prevtermid ) ||
( nexttermid !== undefined && prevtermparent === termid )
) {
$( table_selector ).sortable( 'cancel' );
return;
}
// Show Spinner
ui.item.find( '.check-column input' ).hide();
ui.item
.find( '.check-column' )
.append( '<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />' );
// Go do the sorting stuff via ajax.
$.post(
ajaxurl,
{
action: 'woocommerce_term_ordering',
id: termid,
nextid: nexttermid,
thetaxonomy: woocommerce_term_ordering_params.taxonomy
},
function(response) {
if ( response === 'children' ) {
window.location.reload();
} else {
ui.item.find( '.check-column input' ).show();
ui.item.find( '.check-column' ).find( 'img' ).remove();
}
}
);
// Fix cell colors
$( 'table.widefat tbody tr' ).each( function() {
var i = jQuery( 'table.widefat tbody tr' ).index( this );
if ( i%2 === 0 ) {
jQuery( this ).addClass( 'alternate' );
} else {
jQuery( this ).removeClass( 'alternate' );
}
});
}
});
});

1
assets/js/admin/term-ordering.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(r){var c="table.wp-list-table",m='.column-handle input[name="term_id"]',i='<td class="column-handle"></td>';0===r(c).find(".column-handle").length&&(r(c).find("tr:not(.inline-edit-row)").append(i),m=".check-column input"),r(c).find(".column-handle").show(),r.wc_add_missing_sort_handles=function(){var e=r(c).find("tbody > tr"),n=r(c).find("tbody > tr > td.column-handle").parent();e.length!==n.length&&e.each(function(e,t){n.is(t)||r(t).append(i)}),r(c).find(".column-handle").show()},r(document).ajaxComplete(function(e,t,n){t&&4===t.readyState&&200===t.status&&n.data&&(0<=n.data.indexOf("_inline_edit")||0<=n.data.indexOf("add-tag"))&&(r.wc_add_missing_sort_handles(),r(document.body).trigger("init_tooltips"))}),r(c).sortable({items:"tbody tr:not(.inline-edit-row)",cursor:"move",handle:".column-handle",axis:"y",forcePlaceholderSize:!0,helper:"clone",opacity:.65,placeholder:"product-cat-placeholder",scrollSensitivity:40,start:function(e,t){t.item.hasClass("alternate")||t.item.css("background-color","#ffffff"),t.item.children("td, th").css("border-bottom-width","0"),t.item.css("outline","1px solid #aaa")},stop:function(e,t){t.item.removeAttr("style"),t.item.children("td, th").css("border-bottom-width","1px")},update:function(e,t){var n,i,d=t.item.find(m).val(),a=t.item.find(".parent").html(),o=t.item.prev().find(m).val(),l=t.item.next().find(m).val();o!==undefined&&(n=t.item.prev().find(".parent").html())!==a&&(o=undefined),l!==undefined&&(i=t.item.next().find(".parent").html())!==a&&(l=undefined),o===undefined&&l===undefined||l===undefined&&i===o||l!==undefined&&n===d?r(c).sortable("cancel"):(t.item.find(".check-column input").hide(),t.item.find(".check-column").append('<img alt="processing" src="images/wpspin_light.gif" class="waiting" style="margin-left: 6px;" />'),r.post(ajaxurl,{action:"woocommerce_term_ordering",id:d,nextid:l,thetaxonomy:woocommerce_term_ordering_params.taxonomy},function(e){"children"===e?window.location.reload():(t.item.find(".check-column input").show(),t.item.find(".check-column").find("img").remove())}),r("table.widefat tbody tr").each(function(){jQuery("table.widefat tbody tr").index(this)%2==0?jQuery(this).addClass("alternate"):jQuery(this).removeClass("alternate")}))}})});

120
assets/js/admin/users.js Normal file
View File

@ -0,0 +1,120 @@
/*global wc_users_params */
jQuery( function ( $ ) {
/**
* Users country and state fields
*/
var wc_users_fields = {
states: null,
init: function() {
if ( typeof wc_users_params.countries !== 'undefined' ) {
/* State/Country select boxes */
this.states = JSON.parse( wc_users_params.countries.replace( /&quot;/g, '"' ) );
}
$( '.js_field-country' ).selectWoo().on( 'change', this.change_country );
$( '.js_field-country' ).trigger( 'change', [ true ] );
$( document.body ).on( 'change', 'select.js_field-state', this.change_state );
$( document.body ).on( 'click', 'button.js_copy-billing', this.copy_billing );
},
change_country: function( e, stickValue ) {
// Check for stickValue before using it
if ( typeof stickValue === 'undefined' ) {
stickValue = false;
}
// Prevent if we don't have the metabox data
if ( wc_users_fields.states === null ) {
return;
}
var $this = $( this ),
country = $this.val(),
$state = $this.parents( '.form-table' ).find( ':input.js_field-state' ),
$parent = $state.parent(),
input_name = $state.attr( 'name' ),
input_id = $state.attr( 'id' ),
stickstatefield = 'woocommerce.stickState-' + country,
value = $this.data( stickstatefield ) ? $this.data( stickstatefield ) : $state.val(),
placeholder = $state.attr( 'placeholder' ),
$newstate;
if ( stickValue ){
$this.data( 'woocommerce.stickState-' + country, value );
}
// Remove the previous DOM element
$parent.show().find( '.select2-container' ).remove();
if ( ! $.isEmptyObject( wc_users_fields.states[ country ] ) ) {
var state = wc_users_fields.states[ country ],
$defaultOption = $( '<option value=""></option>' )
.text( wc_users_fields.i18n_select_state_text );
$newstate = $( '<select style="width: 25em;"></select>' )
.prop( 'id', input_id )
.prop( 'name', input_name )
.prop( 'placeholder', placeholder )
.addClass( 'js_field-state' )
.append( $defaultOption );
$.each( state, function( index ) {
var $option = $( '<option></option>' )
.prop( 'value', index )
.text( state[ index ] );
$newstate.append( $option );
} );
$newstate.val( value );
$state.replaceWith( $newstate );
$newstate.show().selectWoo().hide().trigger( 'change' );
} else {
$newstate = $( '<input type="text" />' )
.prop( 'id', input_id )
.prop( 'name', input_name )
.prop( 'placeholder', placeholder )
.addClass( 'js_field-state regular-text' )
.val( value );
$state.replaceWith( $newstate );
}
// This event has a typo - deprecated in 2.5.0
$( document.body ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] );
$( document.body ).trigger( 'country-change.woocommerce', [country, $( this ).closest( 'div' )] );
},
change_state: function() {
// Here we will find if state value on a select has changed and stick it to the country data
var $this = $( this ),
state = $this.val(),
$country = $this.parents( '.form-table' ).find( ':input.js_field-country' ),
country = $country.val();
$country.data( 'woocommerce.stickState-' + country, state );
},
copy_billing: function( event ) {
event.preventDefault();
$( '#fieldset-billing' ).find( 'input, select' ).each( function( i, el ) {
// The address keys match up, except for the prefix
var shipName = el.name.replace( /^billing_/, 'shipping_' );
// Swap prefix, then check if there are any elements
var shipEl = $( '[name="' + shipName + '"]' );
// No corresponding shipping field, skip this item
if ( ! shipEl.length ) {
return;
}
// Found a matching shipping element, update the value
shipEl.val( el.value ).trigger( 'change' );
} );
}
};
wc_users_fields.init();
});

1
assets/js/admin/users.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(u){var h={states:null,init:function(){"undefined"!=typeof wc_users_params.countries&&(this.states=JSON.parse(wc_users_params.countries.replace(/&quot;/g,'"'))),u(".js_field-country").selectWoo().on("change",this.change_country),u(".js_field-country").trigger("change",[!0]),u(document.body).on("change","select.js_field-state",this.change_state),u(document.body).on("click","button.js_copy-billing",this.copy_billing)},change_country:function(e,t){var a,n,o,i,c,s,l,r,p,d;void 0===t&&(t=!1),null!==h.states&&(n=(a=u(this)).val(),p=(o=a.parents(".form-table").find(":input.js_field-state")).parent(),i=o.attr("name"),c=o.attr("id"),l="woocommerce.stickState-"+n,s=a.data(l)?a.data(l):o.val(),l=o.attr("placeholder"),t&&a.data("woocommerce.stickState-"+n,s),p.show().find(".select2-container").remove(),u.isEmptyObject(h.states[n])?(d=u('<input type="text" />').prop("id",c).prop("name",i).prop("placeholder",l).addClass("js_field-state regular-text").val(s),o.replaceWith(d)):(r=h.states[n],p=u('<option value=""></option>').text(h.i18n_select_state_text),d=u('<select style="width: 25em;"></select>').prop("id",c).prop("name",i).prop("placeholder",l).addClass("js_field-state").append(p),u.each(r,function(e){e=u("<option></option>").prop("value",e).text(r[e]);d.append(e)}),d.val(s),o.replaceWith(d),d.show().selectWoo().hide().trigger("change")),u(document.body).trigger("contry-change.woocommerce",[n,u(this).closest("div")]),u(document.body).trigger("country-change.woocommerce",[n,u(this).closest("div")]))},change_state:function(){var e=u(this),t=e.val(),a=e.parents(".form-table").find(":input.js_field-country"),e=a.val();a.data("woocommerce.stickState-"+e,t)},copy_billing:function(e){e.preventDefault(),u("#fieldset-billing").find("input, select").each(function(e,t){var a=t.name.replace(/^billing_/,"shipping_"),a=u('[name="'+a+'"]');a.length&&a.val(t.value).trigger("change")})}};h.init()});

View File

@ -0,0 +1,38 @@
/* exported wcSetClipboard, wcClearClipboard */
/**
* Simple text copy functions using native browser clipboard capabilities.
* @since 3.2.0
*/
/**
* Set the user's clipboard contents.
*
* @param string data: Text to copy to clipboard.
* @param object $el: jQuery element to trigger copy events on. (Default: document)
*/
function wcSetClipboard( data, $el ) {
if ( 'undefined' === typeof $el ) {
$el = jQuery( document );
}
var $temp_input = jQuery( '<textarea style="opacity:0">' );
jQuery( 'body' ).append( $temp_input );
$temp_input.val( data ).trigger( 'select' );
$el.trigger( 'beforecopy' );
try {
document.execCommand( 'copy' );
$el.trigger( 'aftercopy' );
} catch ( err ) {
$el.trigger( 'aftercopyfailure' );
}
$temp_input.remove();
}
/**
* Clear the user's clipboard.
*/
function wcClearClipboard() {
wcSetClipboard( '' );
}

1
assets/js/admin/wc-clipboard.min.js vendored Normal file
View File

@ -0,0 +1 @@
function wcSetClipboard(e,r){void 0===r&&(r=jQuery(document));var t=jQuery('<textarea style="opacity:0">');jQuery("body").append(t),t.val(e).trigger("select"),r.trigger("beforecopy");try{document.execCommand("copy"),r.trigger("aftercopy")}catch(o){r.trigger("aftercopyfailure")}t.remove()}function wcClearClipboard(){wcSetClipboard("")}

View File

@ -0,0 +1,325 @@
/*global wc_enhanced_select_params */
jQuery( function( $ ) {
function getEnhancedSelectFormatString() {
return {
'language': {
errorLoading: function() {
// Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error.
return wc_enhanced_select_params.i18n_searching;
},
inputTooLong: function( args ) {
var overChars = args.input.length - args.maximum;
if ( 1 === overChars ) {
return wc_enhanced_select_params.i18n_input_too_long_1;
}
return wc_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', overChars );
},
inputTooShort: function( args ) {
var remainingChars = args.minimum - args.input.length;
if ( 1 === remainingChars ) {
return wc_enhanced_select_params.i18n_input_too_short_1;
}
return wc_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars );
},
loadingMore: function() {
return wc_enhanced_select_params.i18n_load_more;
},
maximumSelected: function( args ) {
if ( args.maximum === 1 ) {
return wc_enhanced_select_params.i18n_selection_too_long_1;
}
return wc_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum );
},
noResults: function() {
return wc_enhanced_select_params.i18n_no_matches;
},
searching: function() {
return wc_enhanced_select_params.i18n_searching;
}
}
};
}
try {
$( document.body )
.on( 'wc-enhanced-select-init', function() {
// Regular select boxes
$( ':input.wc-enhanced-select, :input.chosen_select' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = $.extend({
minimumResultsForSearch: 10,
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
placeholder: $( this ).data( 'placeholder' )
}, getEnhancedSelectFormatString() );
$( this ).selectWoo( select2_args ).addClass( 'enhanced' );
});
$( ':input.wc-enhanced-select-nostd, :input.chosen_select_nostd' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = $.extend({
minimumResultsForSearch: 10,
allowClear: true,
placeholder: $( this ).data( 'placeholder' )
}, getEnhancedSelectFormatString() );
$( this ).selectWoo( select2_args ).addClass( 'enhanced' );
});
function display_result( self, select2_args ) {
select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
$( self ).selectWoo( select2_args ).addClass( 'enhanced' );
if ( $( self ).data( 'sortable' ) ) {
var $select = $(self);
var $list = $( self ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' );
$list.sortable({
placeholder : 'ui-state-highlight select2-selection__choice',
forcePlaceholderSize: true,
items : 'li:not(.select2-search__field)',
tolerance : 'pointer',
stop: function() {
$( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() {
var id = $( this ).data( 'data' ).id;
var option = $select.find( 'option[value="' + id + '"]' )[0];
$select.prepend( option );
} );
}
});
// Keep multiselects ordered alphabetically if they are not sortable.
} else if ( $( self ).prop( 'multiple' ) ) {
$( self ).on( 'change', function(){
var $children = $( self ).children();
$children.sort(function(a, b){
var atext = a.text.toLowerCase();
var btext = b.text.toLowerCase();
if ( atext > btext ) {
return 1;
}
if ( atext < btext ) {
return -1;
}
return 0;
});
$( self ).html( $children );
});
}
}
// Ajax product search box
$( ':input.wc-product-search' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = {
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
placeholder: $( this ).data( 'placeholder' ),
minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',
escapeMarkup: function( m ) {
return m;
},
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
delay: 250,
data: function( params ) {
return {
term : params.term,
action : $( this ).data( 'action' ) || 'woocommerce_json_search_products_and_variations',
security : wc_enhanced_select_params.search_products_nonce,
exclude : $( this ).data( 'exclude' ),
exclude_type : $( this ).data( 'exclude_type' ),
include : $( this ).data( 'include' ),
limit : $( this ).data( 'limit' ),
display_stock: $( this ).data( 'display_stock' )
};
},
processResults: function( data ) {
var terms = [];
if ( data ) {
$.each( data, function( id, text ) {
terms.push( { id: id, text: text } );
});
}
return {
results: terms
};
},
cache: true
}
};
display_result( this, select2_args );
});
// Ajax Page Search.
$( ':input.wc-page-search' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = {
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
placeholder: $( this ).data( 'placeholder' ),
minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '3',
escapeMarkup: function( m ) {
return m;
},
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
delay: 250,
data: function( params ) {
return {
term : params.term,
action : $( this ).data( 'action' ) || 'woocommerce_json_search_pages',
security : wc_enhanced_select_params.search_pages_nonce,
exclude : $( this ).data( 'exclude' ),
post_status : $( this ).data( 'post_status' ),
limit : $( this ).data( 'limit' ),
};
},
processResults: function( data ) {
var terms = [];
if ( data ) {
$.each( data, function( id, text ) {
terms.push( { id: id, text: text } );
} );
}
return {
results: terms
};
},
cache: true
}
};
$( this ).selectWoo( select2_args ).addClass( 'enhanced' );
});
// Ajax customer search boxes
$( ':input.wc-customer-search' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = {
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
placeholder: $( this ).data( 'placeholder' ),
minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : '1',
escapeMarkup: function( m ) {
return m;
},
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
delay: 1000,
data: function( params ) {
return {
term: params.term,
action: 'woocommerce_json_search_customers',
security: wc_enhanced_select_params.search_customers_nonce,
exclude: $( this ).data( 'exclude' )
};
},
processResults: function( data ) {
var terms = [];
if ( data ) {
$.each( data, function( id, text ) {
terms.push({
id: id,
text: text
});
});
}
return {
results: terms
};
},
cache: true
}
};
select2_args = $.extend( select2_args, getEnhancedSelectFormatString() );
$( this ).selectWoo( select2_args ).addClass( 'enhanced' );
if ( $( this ).data( 'sortable' ) ) {
var $select = $(this);
var $list = $( this ).next( '.select2-container' ).find( 'ul.select2-selection__rendered' );
$list.sortable({
placeholder : 'ui-state-highlight select2-selection__choice',
forcePlaceholderSize: true,
items : 'li:not(.select2-search__field)',
tolerance : 'pointer',
stop: function() {
$( $list.find( '.select2-selection__choice' ).get().reverse() ).each( function() {
var id = $( this ).data( 'data' ).id;
var option = $select.find( 'option[value="' + id + '"]' )[0];
$select.prepend( option );
} );
}
});
}
});
// Ajax category search boxes
$( ':input.wc-category-search' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = $.extend( {
allowClear : $( this ).data( 'allow_clear' ) ? true : false,
placeholder : $( this ).data( 'placeholder' ),
minimumInputLength: $( this ).data( 'minimum_input_length' ) ? $( this ).data( 'minimum_input_length' ) : 3,
escapeMarkup : function( m ) {
return m;
},
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
delay: 250,
data: function( params ) {
return {
term: params.term,
action: 'woocommerce_json_search_categories',
security: wc_enhanced_select_params.search_categories_nonce
};
},
processResults: function( data ) {
var terms = [];
if ( data ) {
$.each( data, function( id, term ) {
terms.push({
id: term.slug,
text: term.formatted_name
});
});
}
return {
results: terms
};
},
cache: true
}
}, getEnhancedSelectFormatString() );
$( this ).selectWoo( select2_args ).addClass( 'enhanced' );
});
})
// WooCommerce Backbone Modal
.on( 'wc_backbone_modal_before_remove', function() {
$( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' )
.selectWoo( 'close' );
})
.trigger( 'wc-enhanced-select-init' );
$( 'html' ).on( 'click', function( event ) {
if ( this === event.target ) {
$( '.wc-enhanced-select, :input.wc-product-search, :input.wc-customer-search' ).filter( '.select2-hidden-accessible' )
.selectWoo( 'close' );
}
} );
} catch( err ) {
// If select2 failed (conflict?) log the error but don't stop other scripts breaking.
window.console.log( err );
}
});

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,88 @@
/* global wc_orders_params */
jQuery( function( $ ) {
if ( typeof wc_orders_params === 'undefined' ) {
return false;
}
/**
* WCOrdersTable class.
*/
var WCOrdersTable = function() {
$( document )
.on( 'click', '.post-type-shop_order .wp-list-table tbody td', this.onRowClick )
.on( 'click', '.order-preview:not(.disabled)', this.onPreview );
};
/**
* Click a row.
*/
WCOrdersTable.prototype.onRowClick = function( e ) {
if ( $( e.target ).filter( 'a, a *, .no-link, .no-link *, button, button *' ).length ) {
return true;
}
if ( window.getSelection && window.getSelection().toString().length ) {
return true;
}
var $row = $( this ).closest( 'tr' ),
href = $row.find( 'a.order-view' ).attr( 'href' );
if ( href && href.length ) {
e.preventDefault();
if ( e.metaKey || e.ctrlKey ) {
window.open( href, '_blank' );
} else {
window.location = href;
}
}
};
/**
* Preview an order.
*/
WCOrdersTable.prototype.onPreview = function() {
var $previewButton = $( this ),
$order_id = $previewButton.data( 'orderId' );
if ( $previewButton.data( 'order-data' ) ) {
$( this ).WCBackboneModal({
template: 'wc-modal-view-order',
variable : $previewButton.data( 'orderData' )
});
} else {
$previewButton.addClass( 'disabled' );
$.ajax({
url: wc_orders_params.ajax_url,
data: {
order_id: $order_id,
action : 'woocommerce_get_order_details',
security: wc_orders_params.preview_nonce
},
type: 'GET',
success: function( response ) {
$( '.order-preview' ).removeClass( 'disabled' );
if ( response.success ) {
$previewButton.data( 'orderData', response.data );
$( this ).WCBackboneModal({
template: 'wc-modal-view-order',
variable : response.data
});
}
}
});
}
return false;
};
/**
* Init WCOrdersTable.
*/
new WCOrdersTable();
} );

1
assets/js/admin/wc-orders.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(r){if("undefined"==typeof wc_orders_params)return!1;var e=function(){r(document).on("click",".post-type-shop_order .wp-list-table tbody td",this.onRowClick).on("click",".order-preview:not(.disabled)",this.onPreview)};e.prototype.onRowClick=function(e){if(r(e.target).filter("a, a *, .no-link, .no-link *, button, button *").length)return!0;if(window.getSelection&&window.getSelection().toString().length)return!0;var t=r(this).closest("tr").find("a.order-view").attr("href");t&&t.length&&(e.preventDefault(),e.metaKey||e.ctrlKey?window.open(t,"_blank"):window.location=t)},e.prototype.onPreview=function(){var t=r(this),e=t.data("orderId");return t.data("order-data")?r(this).WCBackboneModal({template:"wc-modal-view-order",variable:t.data("orderData")}):(t.addClass("disabled"),r.ajax({url:wc_orders_params.ajax_url,data:{order_id:e,action:"woocommerce_get_order_details",security:wc_orders_params.preview_nonce},type:"GET",success:function(e){r(".order-preview").removeClass("disabled"),e.success&&(t.data("orderData",e.data),r(this).WCBackboneModal({template:"wc-modal-view-order",variable:e.data}))}})),!1},new e});

View File

@ -0,0 +1,112 @@
/*global ajaxurl, wc_product_export_params */
;(function ( $, window ) {
/**
* productExportForm handles the export process.
*/
var productExportForm = function( $form ) {
this.$form = $form;
this.xhr = false;
// Initial state.
this.$form.find('.woocommerce-exporter-progress').val( 0 );
// Methods.
this.processStep = this.processStep.bind( this );
// Events.
$form.on( 'submit', { productExportForm: this }, this.onSubmit );
$form.find( '.woocommerce-exporter-types' ).on( 'change', { productExportForm: this }, this.exportTypeFields );
};
/**
* Handle export form submission.
*/
productExportForm.prototype.onSubmit = function( event ) {
event.preventDefault();
var currentDate = new Date(),
day = currentDate.getDate(),
month = currentDate.getMonth() + 1,
year = currentDate.getFullYear(),
timestamp = currentDate.getTime(),
filename = 'wc-product-export-' + day + '-' + month + '-' + year + '-' + timestamp + '.csv';
event.data.productExportForm.$form.addClass( 'woocommerce-exporter__exporting' );
event.data.productExportForm.$form.find('.woocommerce-exporter-progress').val( 0 );
event.data.productExportForm.$form.find('.woocommerce-exporter-button').prop( 'disabled', true );
event.data.productExportForm.processStep( 1, $( this ).serialize(), '', filename );
};
/**
* Process the current export step.
*/
productExportForm.prototype.processStep = function( step, data, columns, filename ) {
var $this = this,
selected_columns = $( '.woocommerce-exporter-columns' ).val(),
export_meta = $( '#woocommerce-exporter-meta:checked' ).length ? 1: 0,
export_types = $( '.woocommerce-exporter-types' ).val(),
export_category = $( '.woocommerce-exporter-category' ).val();
$.ajax( {
type: 'POST',
url: ajaxurl,
data: {
form : data,
action : 'woocommerce_do_ajax_product_export',
step : step,
columns : columns,
selected_columns : selected_columns,
export_meta : export_meta,
export_types : export_types,
export_category : export_category,
filename : filename,
security : wc_product_export_params.export_nonce
},
dataType: 'json',
success: function( response ) {
if ( response.success ) {
if ( 'done' === response.data.step ) {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
window.location = response.data.url;
setTimeout( function() {
$this.$form.removeClass( 'woocommerce-exporter__exporting' );
$this.$form.find('.woocommerce-exporter-button').prop( 'disabled', false );
}, 2000 );
} else {
$this.$form.find('.woocommerce-exporter-progress').val( response.data.percentage );
$this.processStep( parseInt( response.data.step, 10 ), data, response.data.columns, filename );
}
}
}
} ).fail( function( response ) {
window.console.log( response );
} );
};
/**
* Handle fields per export type.
*/
productExportForm.prototype.exportTypeFields = function() {
var exportCategory = $( '.woocommerce-exporter-category' );
if ( -1 !== $.inArray( 'variation', $( this ).val() ) ) {
exportCategory.closest( 'tr' ).hide();
exportCategory.val( '' ).trigger( 'change' ); // Reset WooSelect selected value.
} else {
exportCategory.closest( 'tr' ).show();
}
};
/**
* Function to call productExportForm on jquery selector.
*/
$.fn.wc_product_export_form = function() {
new productExportForm( this );
return this;
};
$( '.woocommerce-exporter' ).wc_product_export_form();
})( jQuery, window );

View File

@ -0,0 +1 @@
!function(n,i){var o=function(o){this.$form=o,this.xhr=!1,this.$form.find(".woocommerce-exporter-progress").val(0),this.processStep=this.processStep.bind(this),o.on("submit",{productExportForm:this},this.onSubmit),o.find(".woocommerce-exporter-types").on("change",{productExportForm:this},this.exportTypeFields)};o.prototype.onSubmit=function(o){o.preventDefault();var e=new Date,e="wc-product-export-"+e.getDate()+"-"+(e.getMonth()+1)+"-"+e.getFullYear()+"-"+e.getTime()+".csv";o.data.productExportForm.$form.addClass("woocommerce-exporter__exporting"),o.data.productExportForm.$form.find(".woocommerce-exporter-progress").val(0),o.data.productExportForm.$form.find(".woocommerce-exporter-button").prop("disabled",!0),o.data.productExportForm.processStep(1,n(this).serialize(),"",e)},o.prototype.processStep=function(o,e,r,t){var c=this,p=n(".woocommerce-exporter-columns").val(),a=n("#woocommerce-exporter-meta:checked").length?1:0,s=n(".woocommerce-exporter-types").val(),m=n(".woocommerce-exporter-category").val();n.ajax({type:"POST",url:ajaxurl,data:{form:e,action:"woocommerce_do_ajax_product_export",step:o,columns:r,selected_columns:p,export_meta:a,export_types:s,export_category:m,filename:t,security:wc_product_export_params.export_nonce},dataType:"json",success:function(o){o.success&&("done"===o.data.step?(c.$form.find(".woocommerce-exporter-progress").val(o.data.percentage),i.location=o.data.url,setTimeout(function(){c.$form.removeClass("woocommerce-exporter__exporting"),c.$form.find(".woocommerce-exporter-button").prop("disabled",!1)},2e3)):(c.$form.find(".woocommerce-exporter-progress").val(o.data.percentage),c.processStep(parseInt(o.data.step,10),e,o.data.columns,t)))}}).fail(function(o){i.console.log(o)})},o.prototype.exportTypeFields=function(){var o=n(".woocommerce-exporter-category");-1!==n.inArray("variation",n(this).val())?(o.closest("tr").hide(),o.val("").trigger("change")):o.closest("tr").show()},n.fn.wc_product_export_form=function(){return new o(this),this},n(".woocommerce-exporter").wc_product_export_form()}(jQuery,window);

View File

@ -0,0 +1,93 @@
/*global ajaxurl, wc_product_import_params */
;(function ( $, window ) {
/**
* productImportForm handles the import process.
*/
var productImportForm = function( $form ) {
this.$form = $form;
this.xhr = false;
this.mapping = wc_product_import_params.mapping;
this.position = 0;
this.file = wc_product_import_params.file;
this.update_existing = wc_product_import_params.update_existing;
this.delimiter = wc_product_import_params.delimiter;
this.security = wc_product_import_params.import_nonce;
// Number of import successes/failures.
this.imported = 0;
this.failed = 0;
this.updated = 0;
this.skipped = 0;
// Initial state.
this.$form.find('.woocommerce-importer-progress').val( 0 );
this.run_import = this.run_import.bind( this );
// Start importing.
this.run_import();
};
/**
* Run the import in batches until finished.
*/
productImportForm.prototype.run_import = function() {
var $this = this;
$.ajax( {
type: 'POST',
url: ajaxurl,
data: {
action : 'woocommerce_do_ajax_product_import',
position : $this.position,
mapping : $this.mapping,
file : $this.file,
update_existing : $this.update_existing,
delimiter : $this.delimiter,
security : $this.security
},
dataType: 'json',
success: function( response ) {
if ( response.success ) {
$this.position = response.data.position;
$this.imported += response.data.imported;
$this.failed += response.data.failed;
$this.updated += response.data.updated;
$this.skipped += response.data.skipped;
$this.$form.find('.woocommerce-importer-progress').val( response.data.percentage );
if ( 'done' === response.data.position ) {
var file_name = wc_product_import_params.file.split( '/' ).pop();
window.location = response.data.url +
'&products-imported=' +
parseInt( $this.imported, 10 ) +
'&products-failed=' +
parseInt( $this.failed, 10 ) +
'&products-updated=' +
parseInt( $this.updated, 10 ) +
'&products-skipped=' +
parseInt( $this.skipped, 10 ) +
'&file-name=' +
file_name;
} else {
$this.run_import();
}
}
}
} ).fail( function( response ) {
window.console.log( response );
} );
};
/**
* Function to call productImportForm on jQuery selector.
*/
$.fn.wc_product_importer = function() {
new productImportForm( this );
return this;
};
$( '.woocommerce-importer' ).wc_product_importer();
})( jQuery, window );

View File

@ -0,0 +1 @@
!function(t,r){var i=function(t){this.$form=t,this.xhr=!1,this.mapping=wc_product_import_params.mapping,this.position=0,this.file=wc_product_import_params.file,this.update_existing=wc_product_import_params.update_existing,this.delimiter=wc_product_import_params.delimiter,this.security=wc_product_import_params.import_nonce,this.imported=0,this.failed=0,this.updated=0,this.skipped=0,this.$form.find(".woocommerce-importer-progress").val(0),this.run_import=this.run_import.bind(this),this.run_import()};i.prototype.run_import=function(){var p=this;t.ajax({type:"POST",url:ajaxurl,data:{action:"woocommerce_do_ajax_product_import",position:p.position,mapping:p.mapping,file:p.file,update_existing:p.update_existing,delimiter:p.delimiter,security:p.security},dataType:"json",success:function(t){var i;t.success&&(p.position=t.data.position,p.imported+=t.data.imported,p.failed+=t.data.failed,p.updated+=t.data.updated,p.skipped+=t.data.skipped,p.$form.find(".woocommerce-importer-progress").val(t.data.percentage),"done"===t.data.position?(i=wc_product_import_params.file.split("/").pop(),r.location=t.data.url+"&products-imported="+parseInt(p.imported,10)+"&products-failed="+parseInt(p.failed,10)+"&products-updated="+parseInt(p.updated,10)+"&products-skipped="+parseInt(p.skipped,10)+"&file-name="+i):p.run_import())}}).fail(function(t){r.console.log(t)})},t.fn.wc_product_importer=function(){return new i(this),this},t(".woocommerce-importer").wc_product_importer()}(jQuery,window);

322
assets/js/admin/wc-setup.js Normal file
View File

@ -0,0 +1,322 @@
/*global wc_setup_params */
/*global wc_setup_currencies */
/*global wc_base_state */
/* @deprecated 4.6.0 */
jQuery( function( $ ) {
function blockWizardUI() {
$('.wc-setup-content').block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
}
$( '.button-next' ).on( 'click', function() {
var form = $( this ).parents( 'form' ).get( 0 );
if ( ( 'function' !== typeof form.checkValidity ) || form.checkValidity() ) {
blockWizardUI();
}
return true;
} );
$( 'form.address-step' ).on( 'submit', function( e ) {
var form = $( this );
if ( ( 'function' !== typeof form.checkValidity ) || form.checkValidity() ) {
blockWizardUI();
}
e.preventDefault();
$('.wc-setup-content').unblock();
$( this ).WCBackboneModal( {
template: 'wc-modal-tracking-setup'
} );
$( document.body ).on( 'wc_backbone_modal_response', function() {
form.off( 'submit' ).trigger( 'submit' );
} );
$( '#wc_tracker_checkbox_dialog' ).on( 'change', function( e ) {
var eventTarget = $( e.target );
$( '#wc_tracker_checkbox' ).prop( 'checked', eventTarget.prop( 'checked' ) );
} );
$( '#wc_tracker_submit' ).on( 'click', function () {
form.off( 'submit' ).trigger( 'submit' );
} );
return true;
} );
$( '#store_country' ).on( 'change', function() {
// Prevent if we don't have the metabox data
if ( wc_setup_params.states === null ){
return;
}
var $this = $( this ),
country = $this.val(),
$state_select = $( '#store_state' );
if ( ! $.isEmptyObject( wc_setup_params.states[ country ] ) ) {
var states = wc_setup_params.states[ country ];
$state_select.empty();
$.each( states, function( index ) {
$state_select.append( $( '<option value="' + index + '">' + states[ index ] + '</option>' ) );
} );
$( '.store-state-container' ).show();
$state_select.selectWoo().val( wc_base_state ).trigger( 'change' ).prop( 'required', true );
} else {
$( '.store-state-container' ).hide();
$state_select.empty().val( '' ).trigger( 'change' ).prop( 'required', false );
}
$( '#currency_code' ).val( wc_setup_currencies[ country ] ).trigger( 'change' );
} );
/* Setup postcode field and validations */
$( '#store_country' ).on( 'change', function() {
if ( ! wc_setup_params.postcodes ) {
return;
}
var $this = $( this ),
country = $this.val(),
$store_postcode_input = $( '#store_postcode' ),
country_postcode_obj = wc_setup_params.postcodes[ country ];
// Default to required, if its unknown whether postcode is required or not.
if ( $.isEmptyObject( country_postcode_obj ) || country_postcode_obj.required ) {
$store_postcode_input.attr( 'required', 'true' );
} else {
$store_postcode_input.prop( 'required', false );
}
} );
$( '#store_country' ).trigger( 'change' );
$( '.wc-wizard-services' ).on( 'change', '.wc-wizard-service-enable input', function() {
if ( $( this ).is( ':checked' ) ) {
$( this ).closest( '.wc-wizard-service-toggle' ).removeClass( 'disabled' );
$( this ).closest( '.wc-wizard-service-item' ).addClass( 'checked' );
$( this ).closest( '.wc-wizard-service-item' )
.find( '.wc-wizard-service-settings' ).removeClass( 'hide' );
} else {
$( this ).closest( '.wc-wizard-service-toggle' ).addClass( 'disabled' );
$( this ).closest( '.wc-wizard-service-item' ).removeClass( 'checked' );
$( this ).closest( '.wc-wizard-service-item' )
.find( '.wc-wizard-service-settings' ).addClass( 'hide' );
}
} );
$( '.wc-wizard-services' ).on( 'keyup', function( e ) {
var code = e.keyCode || e.which,
$focused = $( document.activeElement );
if ( $focused.is( '.wc-wizard-service-toggle, .wc-wizard-service-enable' ) && ( 13 === code || 32 === code ) ) {
$focused.find( ':input' ).trigger( 'click' );
}
} );
$( '.wc-wizard-services' ).on( 'click', '.wc-wizard-service-enable', function( e ) {
var eventTarget = $( e.target );
if ( eventTarget.is( 'input' ) ) {
e.stopPropagation();
return;
}
var $checkbox = $( this ).find( 'input[type="checkbox"]' );
$checkbox.prop( 'checked', ! $checkbox.prop( 'checked' ) ).trigger( 'change' );
} );
$( '.wc-wizard-services-list-toggle' ).on( 'click', function() {
var listToggle = $( this ).closest( '.wc-wizard-services-list-toggle' );
if ( listToggle.hasClass( 'closed' ) ) {
listToggle.removeClass( 'closed' );
} else {
listToggle.addClass( 'closed' );
}
$( this ).closest( '.wc-wizard-services' ).find( '.wc-wizard-service-item' )
.slideToggle()
.css( 'display', 'flex' );
} );
$( '.wc-wizard-services' ).on( 'change', '.wc-wizard-shipping-method-select .method', function( e ) {
var zone = $( this ).closest( '.wc-wizard-service-description' );
var selectedMethod = e.target.value;
var description = zone.find( '.shipping-method-descriptions' );
description.find( '.shipping-method-description' ).addClass( 'hide' );
description.find( '.' + selectedMethod ).removeClass( 'hide' );
var $checkbox = zone.parent().find( 'input[type="checkbox"]' );
var settings = zone.find( '.shipping-method-settings' );
settings
.find( '.shipping-method-setting' )
.addClass( 'hide' )
.find( '.shipping-method-required-field' )
.prop( 'required', false );
settings
.find( '.' + selectedMethod )
.removeClass( 'hide' )
.find( '.shipping-method-required-field' )
.prop( 'required', $checkbox.prop( 'checked' ) );
} ).find( '.wc-wizard-shipping-method-select .method' ).trigger( 'change' );
$( '.wc-wizard-services' ).on( 'change', '.wc-wizard-shipping-method-enable', function() {
var checked = $( this ).is( ':checked' );
var selectedMethod = $( this )
.closest( '.wc-wizard-service-item' )
.find( '.wc-wizard-shipping-method-select .method' )
.val();
$( this )
.closest( '.wc-wizard-service-item' )
.find( '.' + selectedMethod )
.find( '.shipping-method-required-field' )
.prop( 'required', checked );
} );
function submitActivateForm() {
$( 'form.activate-jetpack' ).trigger( 'submit' );
}
function waitForJetpackInstall() {
wp.ajax.post( 'setup_wizard_check_jetpack' )
.then( function( result ) {
// If we receive success, or an unexpected result
// let the form submit.
if (
! result ||
! result.is_active ||
'yes' === result.is_active
) {
return submitActivateForm();
}
// Wait until checking the status again
setTimeout( waitForJetpackInstall, 3000 );
} )
.fail( function() {
// Submit the form as normal if the request fails
submitActivateForm();
} );
}
// Wait for a pending Jetpack install to finish before triggering a "save"
// on the activate step, which launches the Jetpack connection flow.
$( '.activate-jetpack' ).on( 'click', '.button-primary', function( e ) {
blockWizardUI();
if ( 'no' === wc_setup_params.pending_jetpack_install ) {
return true;
}
e.preventDefault();
waitForJetpackInstall();
} );
$( '.activate-new-onboarding' ).on( 'click', '.button-primary', function() {
// Show pending spinner while activate happens.
blockWizardUI();
} );
$( '.wc-wizard-services' ).on( 'change', 'input#stripe_create_account, input#ppec_paypal_reroute_requests', function() {
if ( $( this ).is( ':checked' ) ) {
$( this ).closest( '.wc-wizard-service-settings' )
.find( 'input.payment-email-input' )
.attr( 'type', 'email' )
.prop( 'disabled', false )
.prop( 'required', true );
} else {
$( this ).closest( '.wc-wizard-service-settings' )
.find( 'input.payment-email-input' )
.attr( 'type', null )
.prop( 'disabled', true )
.prop( 'required', false );
}
} ).find( 'input#stripe_create_account, input#ppec_paypal_reroute_requests' ).trigger( 'change' );
function addPlugins( bySlug, $el, hover ) {
var plugins = $el.data( 'plugins' );
for ( var i in Array.isArray( plugins ) ? plugins : [] ) {
var slug = plugins[ i ].slug;
bySlug[ slug ] = bySlug[ slug ] ||
$( '<span class="plugin-install-info-list-item">' )
.append( '<a href="https://wordpress.org/plugins/' + slug + '/" target="_blank">' + plugins[ i ].name + '</a>' );
bySlug[ slug ].find( 'a' )
.on( 'mouseenter mouseleave', ( function( $hover, event ) {
$hover.toggleClass( 'plugin-install-source', 'mouseenter' === event.type );
} ).bind( null, hover ? $el.closest( hover ) : $el ) );
}
}
function updatePluginInfo() {
var pluginLinkBySlug = {};
var extraPlugins = [];
$( '.wc-wizard-service-enable input:checked' ).each( function() {
addPlugins( pluginLinkBySlug, $( this ), '.wc-wizard-service-item' );
var $container = $( this ).closest( '.wc-wizard-service-item' );
$container.find( 'input.payment-checkbox-input:checked' ).each( function() {
extraPlugins.push( $( this ).attr( 'id' ) );
addPlugins( pluginLinkBySlug, $( this ), '.wc-wizard-service-settings' );
} );
$container.find( '.wc-wizard-shipping-method-select .method' ).each( function() {
var $this = $( this );
if ( 'live_rates' === $this.val() ) {
addPlugins( pluginLinkBySlug, $this, '.wc-wizard-service-item' );
}
} );
} );
$( '.recommended-item input:checked' ).each( function() {
addPlugins( pluginLinkBySlug, $( this ), '.recommended-item' );
} );
var $list = $( 'span.plugin-install-info-list' ).empty();
for ( var slug in pluginLinkBySlug ) {
$list.append( pluginLinkBySlug[ slug ] );
}
if (
extraPlugins &&
wc_setup_params.current_step &&
wc_setup_params.i18n.extra_plugins[ wc_setup_params.current_step ] &&
wc_setup_params.i18n.extra_plugins[ wc_setup_params.current_step ][ extraPlugins.join( ',' ) ]
) {
$list.append(
wc_setup_params.i18n.extra_plugins[ wc_setup_params.current_step ][ extraPlugins.join( ',' ) ]
);
}
$( 'span.plugin-install-info' ).toggle( $list.children().length > 0 );
}
updatePluginInfo();
$( '.wc-setup-content' ).on( 'change', '[data-plugins]', updatePluginInfo );
$( document.body ).on( 'init_tooltips', function() {
$( '.help_tip' ).tipTip( {
'attribute': 'data-tip',
'fadeIn': 50,
'fadeOut': 50,
'delay': 200,
'defaultPosition': 'top'
} );
} ).trigger( 'init_tooltips' );
} );

1
assets/js/admin/wc-setup.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,245 @@
/* global shippingClassesLocalizeScript, ajaxurl */
( function( $, data, wp, ajaxurl ) {
$( function() {
var $tbody = $( '.wc-shipping-class-rows' ),
$save_button = $( '.wc-shipping-class-save' ),
$row_template = wp.template( 'wc-shipping-class-row' ),
$blank_template = wp.template( 'wc-shipping-class-row-blank' ),
// Backbone model
ShippingClass = Backbone.Model.extend({
changes: {},
logChanges: function( changedRows ) {
var changes = this.changes || {};
_.each( changedRows, function( row, id ) {
changes[ id ] = _.extend( changes[ id ] || { term_id : id }, row );
} );
this.changes = changes;
this.trigger( 'change:classes' );
},
save: function() {
if ( _.size( this.changes ) ) {
$.post( ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_classes_save_changes', {
wc_shipping_classes_nonce : data.wc_shipping_classes_nonce,
changes : this.changes
}, this.onSaveResponse, 'json' );
} else {
shippingClass.trigger( 'saved:classes' );
}
},
discardChanges: function( id ) {
var changes = this.changes || {};
// Delete all changes
delete changes[ id ];
// No changes? Disable save button.
if ( 0 === _.size( this.changes ) ) {
shippingClassView.clearUnloadConfirmation();
}
},
onSaveResponse: function( response, textStatus ) {
if ( 'success' === textStatus ) {
if ( response.success ) {
shippingClass.set( 'classes', response.data.shipping_classes );
shippingClass.trigger( 'change:classes' );
shippingClass.changes = {};
shippingClass.trigger( 'saved:classes' );
} else if ( response.data ) {
window.alert( response.data );
} else {
window.alert( data.strings.save_failed );
}
}
shippingClassView.unblock();
}
} ),
// Backbone view
ShippingClassView = Backbone.View.extend({
rowTemplate: $row_template,
initialize: function() {
this.listenTo( this.model, 'change:classes', this.setUnloadConfirmation );
this.listenTo( this.model, 'saved:classes', this.clearUnloadConfirmation );
this.listenTo( this.model, 'saved:classes', this.render );
$tbody.on( 'change', { view: this }, this.updateModelOnChange );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$save_button.on( 'click', { view: this }, this.onSubmit );
$( document.body ).on( 'click', '.wc-shipping-class-add', { view: this }, this.onAddNewRow );
$( document.body ).on( 'click', '.wc-shipping-class-save-changes', { view: this }, this.onSubmit );
},
block: function() {
$( this.el ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
unblock: function() {
$( this.el ).unblock();
},
render: function() {
var classes = _.indexBy( this.model.get( 'classes' ), 'term_id' ),
view = this;
this.$el.empty();
this.unblock();
if ( _.size( classes ) ) {
// Sort classes
classes = _.sortBy( classes, function( shipping_class ) {
return shipping_class.name;
} );
// Populate $tbody with the current classes
$.each( classes, function( id, rowData ) {
view.renderRow( rowData );
} );
} else {
view.$el.append( $blank_template );
}
},
renderRow: function( rowData ) {
var view = this;
view.$el.append( view.rowTemplate( rowData ) );
view.initRow( rowData );
},
initRow: function( rowData ) {
var view = this;
var $tr = view.$el.find( 'tr[data-id="' + rowData.term_id + '"]');
// Support select boxes
$tr.find( 'select' ).each( function() {
var attribute = $( this ).data( 'attribute' );
$( this ).find( 'option[value="' + rowData[ attribute ] + '"]' ).prop( 'selected', true );
} );
// Make the rows function
$tr.find( '.view' ).show();
$tr.find( '.edit' ).hide();
$tr.find( '.wc-shipping-class-edit' ).on( 'click', { view: this }, this.onEditRow );
$tr.find( '.wc-shipping-class-delete' ).on( 'click', { view: this }, this.onDeleteRow );
$tr.find( '.editing .wc-shipping-class-edit' ).trigger('click');
$tr.find( '.wc-shipping-class-cancel-edit' ).on( 'click', { view: this }, this.onCancelEditRow );
// Editing?
if ( true === rowData.editing ) {
$tr.addClass( 'editing' );
$tr.find( '.wc-shipping-class-edit' ).trigger( 'click' );
}
},
onSubmit: function( event ) {
event.data.view.block();
event.data.view.model.save();
event.preventDefault();
},
onAddNewRow: function( event ) {
event.preventDefault();
var view = event.data.view,
model = view.model,
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
changes = {},
size = _.size( classes ),
newRow = _.extend( {}, data.default_shipping_class, {
term_id: 'new-' + size + '-' + Date.now(),
editing: true,
newRow : true
} );
changes[ newRow.term_id ] = newRow;
model.logChanges( changes );
view.renderRow( newRow );
$( '.wc-shipping-classes-blank-state' ).remove();
},
onEditRow: function( event ) {
event.preventDefault();
$( this ).closest('tr').addClass('editing');
$( this ).closest('tr').find('.view').hide();
$( this ).closest('tr').find('.edit').show();
event.data.view.model.trigger( 'change:classes' );
},
onDeleteRow: function( event ) {
var view = event.data.view,
model = view.model,
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
changes = {},
term_id = $( this ).closest('tr').data('id');
event.preventDefault();
if ( classes[ term_id ] ) {
delete classes[ term_id ];
changes[ term_id ] = _.extend( changes[ term_id ] || {}, { deleted : 'deleted' } );
model.set( 'classes', classes );
model.logChanges( changes );
}
view.render();
},
onCancelEditRow: function( event ) {
var view = event.data.view,
model = view.model,
row = $( this ).closest('tr'),
term_id = $( this ).closest('tr').data('id'),
classes = _.indexBy( model.get( 'classes' ), 'term_id' );
event.preventDefault();
model.discardChanges( term_id );
if ( classes[ term_id ] ) {
classes[ term_id ].editing = false;
row.after( view.rowTemplate( classes[ term_id ] ) );
view.initRow( classes[ term_id ] );
}
row.remove();
},
setUnloadConfirmation: function() {
this.needsUnloadConfirm = true;
$save_button.prop( 'disabled', false );
},
clearUnloadConfirmation: function() {
this.needsUnloadConfirm = false;
$save_button.attr( 'disabled', 'disabled' );
},
unloadConfirmation: function( event ) {
if ( event.data.view.needsUnloadConfirm ) {
event.returnValue = data.strings.unload_confirmation_msg;
window.event.returnValue = data.strings.unload_confirmation_msg;
return data.strings.unload_confirmation_msg;
}
},
updateModelOnChange: function( event ) {
var model = event.data.view.model,
$target = $( event.target ),
term_id = $target.closest( 'tr' ).data( 'id' ),
attribute = $target.data( 'attribute' ),
value = $target.val(),
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
changes = {};
if ( ! classes[ term_id ] || classes[ term_id ][ attribute ] !== value ) {
changes[ term_id ] = {};
changes[ term_id ][ attribute ] = value;
}
model.logChanges( changes );
}
} ),
shippingClass = new ShippingClass({
classes: data.classes
} ),
shippingClassView = new ShippingClassView({
model: shippingClass,
el: $tbody
} );
shippingClassView.render();
});
})( jQuery, shippingClassesLocalizeScript, wp, ajaxurl );

View File

@ -0,0 +1 @@
!function(d,c,l,r){d(function(){var e=d(".wc-shipping-class-rows"),i=d(".wc-shipping-class-save"),s=l.template("wc-shipping-class-row"),n=l.template("wc-shipping-class-row-blank"),t=Backbone.Model.extend({changes:{},logChanges:function(e){var s=this.changes||{};_.each(e,function(e,i){s[i]=_.extend(s[i]||{term_id:i},e)}),this.changes=s,this.trigger("change:classes")},save:function(){_.size(this.changes)?d.post(r+(0<r.indexOf("?")?"&":"?")+"action=woocommerce_shipping_classes_save_changes",{wc_shipping_classes_nonce:c.wc_shipping_classes_nonce,changes:this.changes},this.onSaveResponse,"json"):a.trigger("saved:classes")},discardChanges:function(e){delete(this.changes||{})[e],0===_.size(this.changes)&&o.clearUnloadConfirmation()},onSaveResponse:function(e,i){"success"===i&&(e.success?(a.set("classes",e.data.shipping_classes),a.trigger("change:classes"),a.changes={},a.trigger("saved:classes")):e.data?window.alert(e.data):window.alert(c.strings.save_failed)),o.unblock()}}),s=Backbone.View.extend({rowTemplate:s,initialize:function(){this.listenTo(this.model,"change:classes",this.setUnloadConfirmation),this.listenTo(this.model,"saved:classes",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:classes",this.render),e.on("change",{view:this},this.updateModelOnChange),d(window).on("beforeunload",{view:this},this.unloadConfirmation),i.on("click",{view:this},this.onSubmit),d(document.body).on("click",".wc-shipping-class-add",{view:this},this.onAddNewRow),d(document.body).on("click",".wc-shipping-class-save-changes",{view:this},this.onSubmit)},block:function(){d(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){d(this.el).unblock()},render:function(){var e=_.indexBy(this.model.get("classes"),"term_id"),s=this;this.$el.empty(),this.unblock(),_.size(e)?(e=_.sortBy(e,function(e){return e.name}),d.each(e,function(e,i){s.renderRow(i)})):s.$el.append(n)},renderRow:function(e){var i=this;i.$el.append(i.rowTemplate(e)),i.initRow(e)},initRow:function(i){var e=this.$el.find('tr[data-id="'+i.term_id+'"]');e.find("select").each(function(){var e=d(this).data("attribute");d(this).find('option[value="'+i[e]+'"]').prop("selected",!0)}),e.find(".view").show(),e.find(".edit").hide(),e.find(".wc-shipping-class-edit").on("click",{view:this},this.onEditRow),e.find(".wc-shipping-class-delete").on("click",{view:this},this.onDeleteRow),e.find(".editing .wc-shipping-class-edit").trigger("click"),e.find(".wc-shipping-class-cancel-edit").on("click",{view:this},this.onCancelEditRow),!0===i.editing&&(e.addClass("editing"),e.find(".wc-shipping-class-edit").trigger("click"))},onSubmit:function(e){e.data.view.block(),e.data.view.model.save(),e.preventDefault()},onAddNewRow:function(e){e.preventDefault();var i=e.data.view,s=i.model,n=_.indexBy(s.get("classes"),"term_id"),e={},n=_.size(n),n=_.extend({},c.default_shipping_class,{term_id:"new-"+n+"-"+Date.now(),editing:!0,newRow:!0});e[n.term_id]=n,s.logChanges(e),i.renderRow(n),d(".wc-shipping-classes-blank-state").remove()},onEditRow:function(e){e.preventDefault(),d(this).closest("tr").addClass("editing"),d(this).closest("tr").find(".view").hide(),d(this).closest("tr").find(".edit").show(),e.data.view.model.trigger("change:classes")},onDeleteRow:function(e){var i=e.data.view,s=i.model,n=_.indexBy(s.get("classes"),"term_id"),t={},a=d(this).closest("tr").data("id");e.preventDefault(),n[a]&&(delete n[a],t[a]=_.extend(t[a]||{},{deleted:"deleted"}),s.set("classes",n),s.logChanges(t)),i.render()},onCancelEditRow:function(e){var i=e.data.view,s=i.model,n=d(this).closest("tr"),t=d(this).closest("tr").data("id"),a=_.indexBy(s.get("classes"),"term_id");e.preventDefault(),s.discardChanges(t),a[t]&&(a[t].editing=!1,n.after(i.rowTemplate(a[t])),i.initRow(a[t])),n.remove()},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,i.prop("disabled",!1)},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,i.attr("disabled","disabled")},unloadConfirmation:function(e){if(e.data.view.needsUnloadConfirm)return e.returnValue=c.strings.unload_confirmation_msg,window.event.returnValue=c.strings.unload_confirmation_msg,c.strings.unload_confirmation_msg},updateModelOnChange:function(e){var i=e.data.view.model,s=d(e.target),n=s.closest("tr").data("id"),t=s.data("attribute"),a=s.val(),e=_.indexBy(i.get("classes"),"term_id"),s={};e[n]&&e[n][t]===a||(s[n]={},s[n][t]=a),i.logChanges(s)}}),a=new t({classes:c.classes}),o=new s({model:a,el:e});o.render()})}(jQuery,shippingClassesLocalizeScript,wp,ajaxurl);

View File

@ -0,0 +1,429 @@
/* global shippingZoneMethodsLocalizeScript, ajaxurl */
( function( $, data, wp, ajaxurl ) {
$( function() {
var $table = $( '.wc-shipping-zone-methods' ),
$tbody = $( '.wc-shipping-zone-method-rows' ),
$save_button = $( '.wc-shipping-zone-method-save' ),
$row_template = wp.template( 'wc-shipping-zone-method-row' ),
$blank_template = wp.template( 'wc-shipping-zone-method-row-blank' ),
// Backbone model
ShippingMethod = Backbone.Model.extend({
changes: {},
logChanges: function( changedRows ) {
var changes = this.changes || {};
_.each( changedRows.methods, function( row, id ) {
changes.methods = changes.methods || { methods : {} };
changes.methods[ id ] = _.extend( changes.methods[ id ] || { instance_id : id }, row );
} );
if ( typeof changedRows.zone_name !== 'undefined' ) {
changes.zone_name = changedRows.zone_name;
}
if ( typeof changedRows.zone_locations !== 'undefined' ) {
changes.zone_locations = changedRows.zone_locations;
}
if ( typeof changedRows.zone_postcodes !== 'undefined' ) {
changes.zone_postcodes = changedRows.zone_postcodes;
}
this.changes = changes;
this.trigger( 'change:methods' );
},
save: function() {
$.post(
ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_methods_save_changes',
{
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
changes : this.changes,
zone_id : data.zone_id
},
this.onSaveResponse,
'json'
);
},
onSaveResponse: function( response, textStatus ) {
if ( 'success' === textStatus ) {
if ( response.success ) {
if ( response.data.zone_id !== data.zone_id ) {
data.zone_id = response.data.zone_id;
if ( window.history.pushState ) {
window.history.pushState(
{},
'',
'admin.php?page=wc-settings&tab=shipping&zone_id=' + response.data.zone_id
);
}
}
shippingMethod.set( 'methods', response.data.methods );
shippingMethod.trigger( 'change:methods' );
shippingMethod.changes = {};
shippingMethod.trigger( 'saved:methods' );
// Overrides the onbeforeunload callback added by settings.js.
window.onbeforeunload = null;
} else {
window.alert( data.strings.save_failed );
}
}
}
} ),
// Backbone view
ShippingMethodView = Backbone.View.extend({
rowTemplate: $row_template,
initialize: function() {
this.listenTo( this.model, 'change:methods', this.setUnloadConfirmation );
this.listenTo( this.model, 'saved:methods', this.clearUnloadConfirmation );
this.listenTo( this.model, 'saved:methods', this.render );
$tbody.on( 'change', { view: this }, this.updateModelOnChange );
$tbody.on( 'sortupdate', { view: this }, this.updateModelOnSort );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$save_button.on( 'click', { view: this }, this.onSubmit );
$( document.body ).on(
'input change',
'#zone_name, #zone_locations, #zone_postcodes',
{ view: this },
this.onUpdateZone
);
$( document.body ).on( 'click', '.wc-shipping-zone-method-settings', { view: this }, this.onConfigureShippingMethod );
$( document.body ).on( 'click', '.wc-shipping-zone-add-method', { view: this }, this.onAddShippingMethod );
$( document.body ).on( 'wc_backbone_modal_response', this.onConfigureShippingMethodSubmitted );
$( document.body ).on( 'wc_backbone_modal_response', this.onAddShippingMethodSubmitted );
$( document.body ).on( 'change', '.wc-shipping-zone-method-selector select', this.onChangeShippingMethodSelector );
$( document.body ).on( 'click', '.wc-shipping-zone-postcodes-toggle', this.onTogglePostcodes );
},
onUpdateZone: function( event ) {
var view = event.data.view,
model = view.model,
value = $( this ).val(),
$target = $( event.target ),
attribute = $target.data( 'attribute' ),
changes = {};
event.preventDefault();
changes[ attribute ] = value;
model.set( attribute, value );
model.logChanges( changes );
view.render();
},
block: function() {
$( this.el ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
unblock: function() {
$( this.el ).unblock();
},
render: function() {
var methods = _.indexBy( this.model.get( 'methods' ), 'instance_id' ),
zone_name = this.model.get( 'zone_name' ),
view = this;
// Set name.
$('.wc-shipping-zone-name').text( zone_name ? zone_name : data.strings.default_zone_name );
// Blank out the contents.
this.$el.empty();
this.unblock();
if ( _.size( methods ) ) {
// Sort methods
methods = _.sortBy( methods, function( method ) {
return parseInt( method.method_order, 10 );
} );
// Populate $tbody with the current methods
$.each( methods, function( id, rowData ) {
if ( 'yes' === rowData.enabled ) {
rowData.enabled_icon = '<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled">' +
data.strings.yes +
'</span>';
} else {
rowData.enabled_icon = '<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled">' +
data.strings.no +
'</span>';
}
view.$el.append( view.rowTemplate( rowData ) );
var $tr = view.$el.find( 'tr[data-id="' + rowData.instance_id + '"]');
if ( ! rowData.has_settings ) {
$tr
.find( '.wc-shipping-zone-method-title > a' )
.replaceWith('<span>' + $tr.find( '.wc-shipping-zone-method-title > a' ).text() + '</span>' );
var $del = $tr.find( '.wc-shipping-zone-method-delete' );
$tr.find( '.wc-shipping-zone-method-title .row-actions' ).empty().html($del);
}
} );
// Make the rows function
this.$el.find( '.wc-shipping-zone-method-delete' ).on( 'click', { view: this }, this.onDeleteRow );
this.$el.find( '.wc-shipping-zone-method-enabled a').on( 'click', { view: this }, this.onToggleEnabled );
} else {
view.$el.append( $blank_template );
}
this.initTooltips();
},
initTooltips: function() {
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
$( '.tips' ).tipTip({ 'attribute': 'data-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 });
},
onSubmit: function( event ) {
event.data.view.block();
event.data.view.model.save();
event.preventDefault();
},
onDeleteRow: function( event ) {
var view = event.data.view,
model = view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
changes = {},
instance_id = $( this ).closest('tr').data('id');
event.preventDefault();
delete methods[ instance_id ];
changes.methods = changes.methods || { methods : {} };
changes.methods[ instance_id ] = _.extend( changes.methods[ instance_id ] || {}, { deleted : 'deleted' } );
model.set( 'methods', methods );
model.logChanges( changes );
view.render();
},
onToggleEnabled: function( event ) {
var view = event.data.view,
$target = $( event.target ),
model = view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
instance_id = $target.closest( 'tr' ).data( 'id' ),
enabled = $target.closest( 'tr' ).data( 'enabled' ) === 'yes' ? 'no' : 'yes',
changes = {};
event.preventDefault();
methods[ instance_id ].enabled = enabled;
changes.methods = changes.methods || { methods : {} };
changes.methods[ instance_id ] = _.extend( changes.methods[ instance_id ] || {}, { enabled : enabled } );
model.set( 'methods', methods );
model.logChanges( changes );
view.render();
},
setUnloadConfirmation: function() {
this.needsUnloadConfirm = true;
$save_button.prop( 'disabled', false );
},
clearUnloadConfirmation: function() {
this.needsUnloadConfirm = false;
$save_button.attr( 'disabled', 'disabled' );
},
unloadConfirmation: function( event ) {
if ( event.data.view.needsUnloadConfirm ) {
event.returnValue = data.strings.unload_confirmation_msg;
window.event.returnValue = data.strings.unload_confirmation_msg;
return data.strings.unload_confirmation_msg;
}
},
updateModelOnChange: function( event ) {
var model = event.data.view.model,
$target = $( event.target ),
instance_id = $target.closest( 'tr' ).data( 'id' ),
attribute = $target.data( 'attribute' ),
value = $target.val(),
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
changes = {};
if ( methods[ instance_id ][ attribute ] !== value ) {
changes.methods[ instance_id ] = {};
changes.methods[ instance_id ][ attribute ] = value;
methods[ instance_id ][ attribute ] = value;
}
model.logChanges( changes );
},
updateModelOnSort: function( event ) {
var view = event.data.view,
model = view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
changes = {};
_.each( methods, function( method ) {
var old_position = parseInt( method.method_order, 10 );
var new_position = parseInt( $table.find( 'tr[data-id="' + method.instance_id + '"]').index() + 1, 10 );
if ( old_position !== new_position ) {
methods[ method.instance_id ].method_order = new_position;
changes.methods = changes.methods || { methods : {} };
changes.methods[ method.instance_id ] = _.extend(
changes.methods[ method.instance_id ] || {}, { method_order : new_position }
);
}
} );
if ( _.size( changes ) ) {
model.logChanges( changes );
}
},
onConfigureShippingMethod: function( event ) {
var instance_id = $( this ).closest( 'tr' ).data( 'id' ),
model = event.data.view.model,
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
method = methods[ instance_id ];
// Only load modal if supported
if ( ! method.settings_html ) {
return true;
}
event.preventDefault();
$( this ).WCBackboneModal({
template : 'wc-modal-shipping-method-settings',
variable : {
instance_id : instance_id,
method : method
},
data : {
instance_id : instance_id,
method : method
}
});
$( document.body ).trigger( 'init_tooltips' );
},
onConfigureShippingMethodSubmitted: function( event, target, posted_data ) {
if ( 'wc-modal-shipping-method-settings' === target ) {
shippingMethodView.block();
// Save method settings via ajax call
$.post(
ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_methods_save_settings',
{
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
instance_id : posted_data.instance_id,
data : posted_data
},
function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
$( 'table.wc-shipping-zone-methods' ).parent().find( '#woocommerce_errors' ).remove();
// If there were errors, prepend the form.
if ( response.data.errors.length > 0 ) {
shippingMethodView.showErrors( response.data.errors );
}
// Method was saved. Re-render.
if ( _.size( shippingMethodView.model.changes ) ) {
shippingMethodView.model.save();
} else {
shippingMethodView.model.onSaveResponse( response, textStatus );
}
} else {
window.alert( data.strings.save_failed );
shippingMethodView.unblock();
}
},
'json'
);
}
},
showErrors: function( errors ) {
var error_html = '<div id="woocommerce_errors" class="error notice is-dismissible">';
$( errors ).each( function( index, value ) {
error_html = error_html + '<p>' + value + '</p>';
} );
error_html = error_html + '</div>';
$( 'table.wc-shipping-zone-methods' ).before( error_html );
},
onAddShippingMethod: function( event ) {
event.preventDefault();
$( this ).WCBackboneModal({
template : 'wc-modal-add-shipping-method',
variable : {
zone_id : data.zone_id
}
});
$( '.wc-shipping-zone-method-selector select' ).trigger( 'change' );
},
onAddShippingMethodSubmitted: function( event, target, posted_data ) {
if ( 'wc-modal-add-shipping-method' === target ) {
shippingMethodView.block();
// Add method to zone via ajax call
$.post( ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_add_method', {
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
method_id : posted_data.add_method_id,
zone_id : data.zone_id
}, function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
if ( response.data.zone_id !== data.zone_id ) {
data.zone_id = response.data.zone_id;
if ( window.history.pushState ) {
window.history.pushState(
{},
'',
'admin.php?page=wc-settings&tab=shipping&zone_id=' + response.data.zone_id
);
}
}
// Trigger save if there are changes, or just re-render
if ( _.size( shippingMethodView.model.changes ) ) {
shippingMethodView.model.save();
} else {
shippingMethodView.model.set( 'methods', response.data.methods );
shippingMethodView.model.trigger( 'change:methods' );
shippingMethodView.model.changes = {};
shippingMethodView.model.trigger( 'saved:methods' );
}
}
shippingMethodView.unblock();
}, 'json' );
}
},
onChangeShippingMethodSelector: function() {
var description = $( this ).find( 'option:selected' ).data( 'description' );
$( this ).parent().find( '.wc-shipping-zone-method-description' ).remove();
$( this ).after( '<div class="wc-shipping-zone-method-description">' + description + '</div>' );
$( this ).closest( 'article' ).height( $( this ).parent().height() );
},
onTogglePostcodes: function( event ) {
event.preventDefault();
var $tr = $( this ).closest( 'tr');
$tr.find( '.wc-shipping-zone-postcodes' ).show();
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide();
}
} ),
shippingMethod = new ShippingMethod({
methods: data.methods,
zone_name: data.zone_name
} ),
shippingMethodView = new ShippingMethodView({
model: shippingMethod,
el: $tbody
} );
shippingMethodView.render();
$tbody.sortable({
items: 'tr',
cursor: 'move',
axis: 'y',
handle: 'td.wc-shipping-zone-method-sort',
scrollSensitivity: 40
});
});
})( jQuery, shippingZoneMethodsLocalizeScript, wp, ajaxurl );

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,275 @@
/* global shippingZonesLocalizeScript, ajaxurl */
( function( $, data, wp, ajaxurl ) {
$( function() {
var $table = $( '.wc-shipping-zones' ),
$tbody = $( '.wc-shipping-zone-rows' ),
$save_button = $( '.wc-shipping-zone-save' ),
$row_template = wp.template( 'wc-shipping-zone-row' ),
$blank_template = wp.template( 'wc-shipping-zone-row-blank' ),
// Backbone model
ShippingZone = Backbone.Model.extend({
changes: {},
logChanges: function( changedRows ) {
var changes = this.changes || {};
_.each( changedRows, function( row, id ) {
changes[ id ] = _.extend( changes[ id ] || { zone_id : id }, row );
} );
this.changes = changes;
this.trigger( 'change:zones' );
},
discardChanges: function( id ) {
var changes = this.changes || {},
set_position = null,
zones = _.indexBy( this.get( 'zones' ), 'zone_id' );
// Find current set position if it has moved since last save
if ( changes[ id ] && changes[ id ].zone_order !== undefined ) {
set_position = changes[ id ].zone_order;
}
// Delete all changes
delete changes[ id ];
// If the position was set, and this zone does exist in DB, set the position again so the changes are not lost.
if ( set_position !== null && zones[ id ] && zones[ id ].zone_order !== set_position ) {
changes[ id ] = _.extend( changes[ id ] || {}, { zone_id : id, zone_order : set_position } );
}
this.changes = changes;
// No changes? Disable save button.
if ( 0 === _.size( this.changes ) ) {
shippingZoneView.clearUnloadConfirmation();
}
},
save: function() {
if ( _.size( this.changes ) ) {
$.post( ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zones_save_changes', {
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
changes : this.changes
}, this.onSaveResponse, 'json' );
} else {
shippingZone.trigger( 'saved:zones' );
}
},
onSaveResponse: function( response, textStatus ) {
if ( 'success' === textStatus ) {
if ( response.success ) {
shippingZone.set( 'zones', response.data.zones );
shippingZone.trigger( 'change:zones' );
shippingZone.changes = {};
shippingZone.trigger( 'saved:zones' );
} else {
window.alert( data.strings.save_failed );
}
}
}
} ),
// Backbone view
ShippingZoneView = Backbone.View.extend({
rowTemplate: $row_template,
initialize: function() {
this.listenTo( this.model, 'change:zones', this.setUnloadConfirmation );
this.listenTo( this.model, 'saved:zones', this.clearUnloadConfirmation );
this.listenTo( this.model, 'saved:zones', this.render );
$tbody.on( 'change', { view: this }, this.updateModelOnChange );
$tbody.on( 'sortupdate', { view: this }, this.updateModelOnSort );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$( document.body ).on( 'click', '.wc-shipping-zone-add', { view: this }, this.onAddNewRow );
},
onAddNewRow: function() {
var $link = $( this );
window.location.href = $link.attr( 'href' );
},
block: function() {
$( this.el ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
unblock: function() {
$( this.el ).unblock();
},
render: function() {
var zones = _.indexBy( this.model.get( 'zones' ), 'zone_id' ),
view = this;
view.$el.empty();
view.unblock();
if ( _.size( zones ) ) {
// Sort zones
zones = _( zones )
.chain()
.sortBy( function ( zone ) { return parseInt( zone.zone_id, 10 ); } )
.sortBy( function ( zone ) { return parseInt( zone.zone_order, 10 ); } )
.value();
// Populate $tbody with the current zones
$.each( zones, function( id, rowData ) {
view.renderRow( rowData );
} );
} else {
view.$el.append( $blank_template );
}
view.initRows();
},
renderRow: function( rowData ) {
var view = this;
view.$el.append( view.rowTemplate( rowData ) );
view.initRow( rowData );
},
initRow: function( rowData ) {
var view = this;
var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]');
// List shipping methods
view.renderShippingMethods( rowData.zone_id, rowData.shipping_methods );
$tr.find( '.wc-shipping-zone-delete' ).on( 'click', { view: this }, this.onDeleteRow );
},
initRows: function() {
// Stripe
if ( 0 === ( $( 'tbody.wc-shipping-zone-rows tr' ).length % 2 ) ) {
$table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).addClass( 'odd' );
} else {
$table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' );
}
// Tooltips
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
$( '.tips' ).tipTip({ 'attribute': 'data-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 });
},
renderShippingMethods: function( zone_id, shipping_methods ) {
var $tr = $( '.wc-shipping-zones tr[data-id="' + zone_id + '"]');
var $method_list = $tr.find('.wc-shipping-zone-methods ul');
$method_list.find( '.wc-shipping-zone-method' ).remove();
if ( _.size( shipping_methods ) ) {
shipping_methods = _.sortBy( shipping_methods, function( method ) {
return parseInt( method.method_order, 10 );
} );
_.each( shipping_methods, function( shipping_method ) {
var class_name = 'method_disabled';
if ( 'yes' === shipping_method.enabled ) {
class_name = 'method_enabled';
}
$method_list.append(
'<li class="wc-shipping-zone-method ' + class_name + '">' + shipping_method.title + '</li>'
);
} );
} else {
$method_list.append( '<li class="wc-shipping-zone-method">' + data.strings.no_shipping_methods_offered + '</li>' );
}
},
onDeleteRow: function( event ) {
var view = event.data.view,
model = view.model,
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
changes = {},
row = $( this ).closest('tr'),
zone_id = row.data('id');
event.preventDefault();
if ( window.confirm( data.strings.delete_confirmation_msg ) ) {
if ( zones[ zone_id ] ) {
delete zones[ zone_id ];
changes[ zone_id ] = _.extend( changes[ zone_id ] || {}, { deleted : 'deleted' } );
model.set( 'zones', zones );
model.logChanges( changes );
event.data.view.block();
event.data.view.model.save();
}
}
},
setUnloadConfirmation: function() {
this.needsUnloadConfirm = true;
$save_button.prop( 'disabled', false );
},
clearUnloadConfirmation: function() {
this.needsUnloadConfirm = false;
$save_button.prop( 'disabled', true );
},
unloadConfirmation: function( event ) {
if ( event.data.view.needsUnloadConfirm ) {
event.returnValue = data.strings.unload_confirmation_msg;
window.event.returnValue = data.strings.unload_confirmation_msg;
return data.strings.unload_confirmation_msg;
}
},
updateModelOnChange: function( event ) {
var model = event.data.view.model,
$target = $( event.target ),
zone_id = $target.closest( 'tr' ).data( 'id' ),
attribute = $target.data( 'attribute' ),
value = $target.val(),
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
changes = {};
if ( ! zones[ zone_id ] || zones[ zone_id ][ attribute ] !== value ) {
changes[ zone_id ] = {};
changes[ zone_id ][ attribute ] = value;
}
model.logChanges( changes );
},
updateModelOnSort: function( event ) {
var view = event.data.view,
model = view.model,
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
rows = $( 'tbody.wc-shipping-zone-rows tr' ),
changes = {};
// Update sorted row position
_.each( rows, function( row ) {
var zone_id = $( row ).data( 'id' ),
old_position = null,
new_position = parseInt( $( row ).index(), 10 );
if ( zones[ zone_id ] ) {
old_position = parseInt( zones[ zone_id ].zone_order, 10 );
}
if ( old_position !== new_position ) {
changes[ zone_id ] = _.extend( changes[ zone_id ] || {}, { zone_order : new_position } );
}
} );
if ( _.size( changes ) ) {
model.logChanges( changes );
event.data.view.block();
event.data.view.model.save();
}
}
} ),
shippingZone = new ShippingZone({
zones: data.zones
} ),
shippingZoneView = new ShippingZoneView({
model: shippingZone,
el: $tbody
} );
shippingZoneView.render();
$tbody.sortable({
items: 'tr',
cursor: 'move',
axis: 'y',
handle: 'td.wc-shipping-zone-sort',
scrollSensitivity: 40
});
});
})( jQuery, shippingZonesLocalizeScript, wp, ajaxurl );

View File

@ -0,0 +1 @@
!function(r,l,c,h){r(function(){var e=r(".wc-shipping-zones"),n=r(".wc-shipping-zone-rows"),i=r(".wc-shipping-zone-save"),o=c.template("wc-shipping-zone-row"),t=c.template("wc-shipping-zone-row-blank"),s=Backbone.Model.extend({changes:{},logChanges:function(e){var i=this.changes||{};_.each(e,function(e,n){i[n]=_.extend(i[n]||{zone_id:n},e)}),this.changes=i,this.trigger("change:zones")},discardChanges:function(e){var n=this.changes||{},i=null,o=_.indexBy(this.get("zones"),"zone_id");n[e]&&n[e].zone_order!==undefined&&(i=n[e].zone_order),delete n[e],null!==i&&o[e]&&o[e].zone_order!==i&&(n[e]=_.extend(n[e]||{},{zone_id:e,zone_order:i})),this.changes=n,0===_.size(this.changes)&&a.clearUnloadConfirmation()},save:function(){_.size(this.changes)?r.post(h+(0<h.indexOf("?")?"&":"?")+"action=woocommerce_shipping_zones_save_changes",{wc_shipping_zones_nonce:l.wc_shipping_zones_nonce,changes:this.changes},this.onSaveResponse,"json"):d.trigger("saved:zones")},onSaveResponse:function(e,n){"success"===n&&(e.success?(d.set("zones",e.data.zones),d.trigger("change:zones"),d.changes={},d.trigger("saved:zones")):window.alert(l.strings.save_failed))}}),o=Backbone.View.extend({rowTemplate:o,initialize:function(){this.listenTo(this.model,"change:zones",this.setUnloadConfirmation),this.listenTo(this.model,"saved:zones",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:zones",this.render),n.on("change",{view:this},this.updateModelOnChange),n.on("sortupdate",{view:this},this.updateModelOnSort),r(window).on("beforeunload",{view:this},this.unloadConfirmation),r(document.body).on("click",".wc-shipping-zone-add",{view:this},this.onAddNewRow)},onAddNewRow:function(){var e=r(this);window.location.href=e.attr("href")},block:function(){r(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){r(this.el).unblock()},render:function(){var e=_.indexBy(this.model.get("zones"),"zone_id"),i=this;i.$el.empty(),i.unblock(),_.size(e)?(e=_(e).chain().sortBy(function(e){return parseInt(e.zone_id,10)}).sortBy(function(e){return parseInt(e.zone_order,10)}).value(),r.each(e,function(e,n){i.renderRow(n)})):i.$el.append(t),i.initRows()},renderRow:function(e){var n=this;n.$el.append(n.rowTemplate(e)),n.initRow(e)},initRow:function(e){var n=this.$el.find('tr[data-id="'+e.zone_id+'"]');this.renderShippingMethods(e.zone_id,e.shipping_methods),n.find(".wc-shipping-zone-delete").on("click",{view:this},this.onDeleteRow)},initRows:function(){0==r("tbody.wc-shipping-zone-rows tr").length%2?e.find("tbody.wc-shipping-zone-rows").next("tbody").find("tr").addClass("odd"):e.find("tbody.wc-shipping-zone-rows").next("tbody").find("tr").removeClass("odd"),r("#tiptip_holder").removeAttr("style"),r("#tiptip_arrow").removeAttr("style"),r(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},renderShippingMethods:function(e,n){var i=r('.wc-shipping-zones tr[data-id="'+e+'"]').find(".wc-shipping-zone-methods ul");i.find(".wc-shipping-zone-method").remove(),_.size(n)?(n=_.sortBy(n,function(e){return parseInt(e.method_order,10)}),_.each(n,function(e){var n="method_disabled";"yes"===e.enabled&&(n="method_enabled"),i.append('<li class="wc-shipping-zone-method '+n+'">'+e.title+"</li>")})):i.append('<li class="wc-shipping-zone-method">'+l.strings.no_shipping_methods_offered+"</li>")},onDeleteRow:function(e){var n=e.data.view.model,i=_.indexBy(n.get("zones"),"zone_id"),o={},t=r(this).closest("tr").data("id");e.preventDefault(),window.confirm(l.strings.delete_confirmation_msg)&&i[t]&&(delete i[t],o[t]=_.extend(o[t]||{},{deleted:"deleted"}),n.set("zones",i),n.logChanges(o),e.data.view.block(),e.data.view.model.save())},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,i.prop("disabled",!1)},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,i.prop("disabled",!0)},unloadConfirmation:function(e){if(e.data.view.needsUnloadConfirm)return e.returnValue=l.strings.unload_confirmation_msg,window.event.returnValue=l.strings.unload_confirmation_msg,l.strings.unload_confirmation_msg},updateModelOnChange:function(e){var n=e.data.view.model,i=r(e.target),o=i.closest("tr").data("id"),t=i.data("attribute"),s=i.val(),e=_.indexBy(n.get("zones"),"zone_id"),i={};e[o]&&e[o][t]===s||(i[o]={},i[o][t]=s),n.logChanges(i)},updateModelOnSort:function(e){var n=e.data.view.model,o=_.indexBy(n.get("zones"),"zone_id"),i=r("tbody.wc-shipping-zone-rows tr"),t={};_.each(i,function(e){var n=r(e).data("id"),i=null,e=parseInt(r(e).index(),10);o[n]&&(i=parseInt(o[n].zone_order,10)),i!==e&&(t[n]=_.extend(t[n]||{},{zone_order:e}))}),_.size(t)&&(n.logChanges(t),e.data.view.block(),e.data.view.model.save())}}),d=new s({zones:l.zones}),a=new o({model:d,el:n});a.render(),n.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.wc-shipping-zone-sort",scrollSensitivity:40})})}(jQuery,shippingZonesLocalizeScript,wp,ajaxurl);

View File

@ -0,0 +1,32 @@
/*global jQuery */
(function( $ ) {
var recordEvent = function( link ) {
window.wcTracks.recordEvent( 'status_widget_click', {
link: link
} );
};
$( '.sales-this-month a' ).on( 'click', function() {
recordEvent( 'net-sales' );
});
$( '.best-seller-this-month a' ).on( 'click', function() {
recordEvent( 'best-seller-this-month' );
});
$( '.processing-orders a' ).on( 'click', function() {
recordEvent( 'orders-processing' );
});
$( '.on-hold-orders a' ).on( 'click', function() {
recordEvent( 'orders-on-hold' );
});
$( '.low-in-stock a' ).on( 'click', function() {
recordEvent( 'low-stock' );
});
$( '.out-of-stock a' ).on( 'click', function() {
recordEvent( 'out-of-stock' );
});
})( jQuery );

View File

@ -0,0 +1 @@
!function(o){var n=function(o){window.wcTracks.recordEvent("status_widget_click",{link:o})};o(".sales-this-month a").on("click",function(){n("net-sales")}),o(".best-seller-this-month a").on("click",function(){n("best-seller-this-month")}),o(".processing-orders a").on("click",function(){n("orders-processing")}),o(".on-hold-orders a").on("click",function(){n("orders-on-hold")}),o(".low-in-stock a").on("click",function(){n("low-stock")}),o(".out-of-stock a").on("click",function(){n("out-of-stock")})}(jQuery);

View File

@ -0,0 +1,452 @@
/* global woocommerce_admin */
( function( $, woocommerce_admin ) {
$( function() {
if ( 'undefined' === typeof woocommerce_admin ) {
return;
}
// Add buttons to product screen.
var $product_screen = $( '.edit-php.post-type-product' ),
$title_action = $product_screen.find( '.page-title-action:first' ),
$blankslate = $product_screen.find( '.woocommerce-BlankState' );
if ( 0 === $blankslate.length ) {
if ( woocommerce_admin.urls.export_products ) {
$title_action.after(
'<a href="' +
woocommerce_admin.urls.export_products +
'" class="page-title-action">' +
woocommerce_admin.strings.export_products +
'</a>'
);
}
if ( woocommerce_admin.urls.import_products ) {
$title_action.after(
'<a href="' +
woocommerce_admin.urls.import_products +
'" class="page-title-action">' +
woocommerce_admin.strings.import_products +
'</a>'
);
}
} else {
$title_action.hide();
}
// Progress indicators when showing steps.
$( '.woocommerce-progress-form-wrapper .button-next' ).on( 'click', function() {
$('.wc-progress-form-content').block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
return true;
} );
// Field validation error tips
$( document.body )
.on( 'wc_add_error_tip', function( e, element, error_type ) {
var offset = element.position();
if ( element.parent().find( '.wc_error_tip' ).length === 0 ) {
element.after( '<div class="wc_error_tip ' + error_type + '">' + woocommerce_admin[error_type] + '</div>' );
element.parent().find( '.wc_error_tip' )
.css( 'left', offset.left + element.width() - ( element.width() / 2 ) - ( $( '.wc_error_tip' ).width() / 2 ) )
.css( 'top', offset.top + element.height() )
.fadeIn( '100' );
}
})
.on( 'wc_remove_error_tip', function( e, element, error_type ) {
element.parent().find( '.wc_error_tip.' + error_type ).fadeOut( '100', function() { $( this ).remove(); } );
})
.on( 'click', function() {
$( '.wc_error_tip' ).fadeOut( '100', function() { $( this ).remove(); } );
})
.on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() {
$( '.wc_error_tip' ).fadeOut( '100', function() { $( this ).remove(); } );
})
.on(
'change',
'.wc_input_price[type=text], .wc_input_decimal[type=text], .wc-order-totals #refund_amount[type=text]',
function() {
var regex, decimalRegex,
decimailPoint = woocommerce_admin.decimal_point;
if ( $( this ).is( '.wc_input_price' ) || $( this ).is( '#refund_amount' ) ) {
decimailPoint = woocommerce_admin.mon_decimal_point;
}
regex = new RegExp( '[^\-0-9\%\\' + decimailPoint + ']+', 'gi' );
decimalRegex = new RegExp( '\\' + decimailPoint + '+', 'gi' );
var value = $( this ).val();
var newvalue = value.replace( regex, '' ).replace( decimalRegex, decimailPoint );
if ( value !== newvalue ) {
$( this ).val( newvalue );
}
}
)
.on(
'keyup',
// eslint-disable-next-line max-len
'.wc_input_price[type=text], .wc_input_decimal[type=text], .wc_input_country_iso[type=text], .wc-order-totals #refund_amount[type=text]',
function() {
var regex, error, decimalRegex;
var checkDecimalNumbers = false;
if ( $( this ).is( '.wc_input_price' ) || $( this ).is( '#refund_amount' ) ) {
checkDecimalNumbers = true;
regex = new RegExp( '[^\-0-9\%\\' + woocommerce_admin.mon_decimal_point + ']+', 'gi' );
decimalRegex = new RegExp( '[^\\' + woocommerce_admin.mon_decimal_point + ']', 'gi' );
error = 'i18n_mon_decimal_error';
} else if ( $( this ).is( '.wc_input_country_iso' ) ) {
regex = new RegExp( '([^A-Z])+|(.){3,}', 'im' );
error = 'i18n_country_iso_error';
} else {
checkDecimalNumbers = true;
regex = new RegExp( '[^\-0-9\%\\' + woocommerce_admin.decimal_point + ']+', 'gi' );
decimalRegex = new RegExp( '[^\\' + woocommerce_admin.decimal_point + ']', 'gi' );
error = 'i18n_decimal_error';
}
var value = $( this ).val();
var newvalue = value.replace( regex, '' );
// Check if newvalue have more than one decimal point.
if ( checkDecimalNumbers && 1 < newvalue.replace( decimalRegex, '' ).length ) {
newvalue = newvalue.replace( decimalRegex, '' );
}
if ( value !== newvalue ) {
$( document.body ).triggerHandler( 'wc_add_error_tip', [ $( this ), error ] );
} else {
$( document.body ).triggerHandler( 'wc_remove_error_tip', [ $( this ), error ] );
}
}
)
.on( 'change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function() {
var sale_price_field = $( this ), regular_price_field;
if ( sale_price_field.attr( 'name' ).indexOf( 'variable' ) !== -1 ) {
regular_price_field = sale_price_field
.parents( '.variable_pricing' )
.find( '.wc_input_price[name^=variable_regular_price]' );
} else {
regular_price_field = $( '#_regular_price' );
}
var sale_price = parseFloat(
window.accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point )
);
var regular_price = parseFloat(
window.accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point )
);
if ( sale_price >= regular_price ) {
$( this ).val( '' );
}
})
.on( 'keyup', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function() {
var sale_price_field = $( this ), regular_price_field;
if ( sale_price_field.attr( 'name' ).indexOf( 'variable' ) !== -1 ) {
regular_price_field = sale_price_field
.parents( '.variable_pricing' )
.find( '.wc_input_price[name^=variable_regular_price]' );
} else {
regular_price_field = $( '#_regular_price' );
}
var sale_price = parseFloat(
window.accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point )
);
var regular_price = parseFloat(
window.accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point )
);
if ( sale_price >= regular_price ) {
$( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_sale_less_than_regular_error' ] );
} else {
$( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_sale_less_than_regular_error' ] );
}
})
.on( 'init_tooltips', function() {
$( '.tips, .help_tip, .woocommerce-help-tip' ).tipTip( {
'attribute': 'data-tip',
'fadeIn': 50,
'fadeOut': 50,
'delay': 200,
'keepAlive': true
} );
$( '.column-wc_actions .wc-action-button' ).tipTip( {
'fadeIn': 50,
'fadeOut': 50,
'delay': 200
} );
// Add tiptip to parent element for widefat tables
$( '.parent-tips' ).each( function() {
$( this ).closest( 'a, th' ).attr( 'data-tip', $( this ).data( 'tip' ) ).tipTip( {
'attribute': 'data-tip',
'fadeIn': 50,
'fadeOut': 50,
'delay': 200,
'keepAlive': true
} ).css( 'cursor', 'help' );
});
});
// Tooltips
$( document.body ).trigger( 'init_tooltips' );
// wc_input_table tables
$( '.wc_input_table.sortable tbody' ).sortable({
items: 'tr',
cursor: 'move',
axis: 'y',
scrollSensitivity: 40,
forcePlaceholderSize: true,
helper: 'clone',
opacity: 0.65,
placeholder: 'wc-metabox-sortable-placeholder',
start: function( event, ui ) {
ui.item.css( 'background-color', '#f6f6f6' );
},
stop: function( event, ui ) {
ui.item.removeAttr( 'style' );
}
});
// Focus on inputs within the table if clicked instead of trying to sort.
$( '.wc_input_table.sortable tbody input' ).on( 'click', function() {
$( this ).trigger( 'focus' );
} );
$( '.wc_input_table .remove_rows' ).on( 'click', function() {
var $tbody = $( this ).closest( '.wc_input_table' ).find( 'tbody' );
if ( $tbody.find( 'tr.current' ).length > 0 ) {
var $current = $tbody.find( 'tr.current' );
$current.each( function() {
$( this ).remove();
});
}
return false;
});
var controlled = false;
var shifted = false;
var hasFocus = false;
$( document.body ).on( 'keyup keydown', function( e ) {
shifted = e.shiftKey;
controlled = e.ctrlKey || e.metaKey;
});
$( '.wc_input_table' ).on( 'focus click', 'input', function( e ) {
var $this_table = $( this ).closest( 'table, tbody' );
var $this_row = $( this ).closest( 'tr' );
if ( ( e.type === 'focus' && hasFocus !== $this_row.index() ) || ( e.type === 'click' && $( this ).is( ':focus' ) ) ) {
hasFocus = $this_row.index();
if ( ! shifted && ! controlled ) {
$( 'tr', $this_table ).removeClass( 'current' ).removeClass( 'last_selected' );
$this_row.addClass( 'current' ).addClass( 'last_selected' );
} else if ( shifted ) {
$( 'tr', $this_table ).removeClass( 'current' );
$this_row.addClass( 'selected_now' ).addClass( 'current' );
if ( $( 'tr.last_selected', $this_table ).length > 0 ) {
if ( $this_row.index() > $( 'tr.last_selected', $this_table ).index() ) {
$( 'tr', $this_table )
.slice( $( 'tr.last_selected', $this_table ).index(), $this_row.index() )
.addClass( 'current' );
} else {
$( 'tr', $this_table )
.slice( $this_row.index(), $( 'tr.last_selected', $this_table ).index() + 1 )
.addClass( 'current' );
}
}
$( 'tr', $this_table ).removeClass( 'last_selected' );
$this_row.addClass( 'last_selected' );
} else {
$( 'tr', $this_table ).removeClass( 'last_selected' );
if ( controlled && $( this ).closest( 'tr' ).is( '.current' ) ) {
$this_row.removeClass( 'current' );
} else {
$this_row.addClass( 'current' ).addClass( 'last_selected' );
}
}
$( 'tr', $this_table ).removeClass( 'selected_now' );
}
}).on( 'blur', 'input', function() {
hasFocus = false;
});
// Additional cost and Attribute term tables
$( '.woocommerce_page_wc-settings .shippingrows tbody tr:even, table.attributes-table tbody tr:nth-child(odd)' )
.addClass( 'alternate' );
// Show order items on orders page
$( document.body ).on( 'click', '.show_order_items', function() {
$( this ).closest( 'td' ).find( 'table' ).toggle();
return false;
});
// Select availability
$( 'select.availability' ).on( 'change', function() {
if ( $( this ).val() === 'all' ) {
$( this ).closest( 'tr' ).next( 'tr' ).hide();
} else {
$( this ).closest( 'tr' ).next( 'tr' ).show();
}
}).trigger( 'change' );
// Hidden options
$( '.hide_options_if_checked' ).each( function() {
$( this ).find( 'input:eq(0)' ).on( 'change', function() {
if ( $( this ).is( ':checked' ) ) {
$( this )
.closest( 'fieldset, tr' )
.nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' )
.hide();
} else {
$( this )
.closest( 'fieldset, tr' )
.nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' )
.show();
}
}).trigger( 'change' );
});
$( '.show_options_if_checked' ).each( function() {
$( this ).find( 'input:eq(0)' ).on( 'change', function() {
if ( $( this ).is( ':checked' ) ) {
$( this )
.closest( 'fieldset, tr' )
.nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' )
.show();
} else {
$( this )
.closest( 'fieldset, tr' )
.nextUntil( '.hide_options_if_checked, .show_options_if_checked', '.hidden_option' )
.hide();
}
}).trigger( 'change' );
});
// Reviews.
$( 'input#woocommerce_enable_reviews' ).on( 'change', function() {
if ( $( this ).is( ':checked' ) ) {
$( '#woocommerce_enable_review_rating' ).closest( 'tr' ).show();
} else {
$( '#woocommerce_enable_review_rating' ).closest( 'tr' ).hide();
}
}).trigger( 'change' );
// Attribute term table
$( 'table.attributes-table tbody tr:nth-child(odd)' ).addClass( 'alternate' );
// Toggle gateway on/off.
$( '.wc_gateways' ).on( 'click', '.wc-payment-gateway-method-toggle-enabled', function() {
var $link = $( this ),
$row = $link.closest( 'tr' ),
$toggle = $link.find( '.woocommerce-input-toggle' );
var data = {
action: 'woocommerce_toggle_gateway_enabled',
security: woocommerce_admin.nonces.gateway_toggle,
gateway_id: $row.data( 'gateway_id' )
};
$toggle.addClass( 'woocommerce-input-toggle--loading' );
$.ajax( {
url: woocommerce_admin.ajax_url,
data: data,
dataType : 'json',
type : 'POST',
success: function( response ) {
if ( true === response.data ) {
$toggle.removeClass( 'woocommerce-input-toggle--enabled, woocommerce-input-toggle--disabled' );
$toggle.addClass( 'woocommerce-input-toggle--enabled' );
$toggle.removeClass( 'woocommerce-input-toggle--loading' );
} else if ( false === response.data ) {
$toggle.removeClass( 'woocommerce-input-toggle--enabled, woocommerce-input-toggle--disabled' );
$toggle.addClass( 'woocommerce-input-toggle--disabled' );
$toggle.removeClass( 'woocommerce-input-toggle--loading' );
} else if ( 'needs_setup' === response.data ) {
window.location.href = $link.attr( 'href' );
}
}
} );
return false;
});
$( '#wpbody' ).on( 'click', '#doaction, #doaction2', function() {
var action = $( this ).is( '#doaction' ) ? $( '#bulk-action-selector-top' ).val() : $( '#bulk-action-selector-bottom' ).val();
if ( 'remove_personal_data' === action ) {
return window.confirm( woocommerce_admin.i18n_remove_personal_data_notice );
}
});
var marketplaceSectionDropdown = $( '#marketplace-current-section-dropdown' );
var marketplaceSectionName = $( '#marketplace-current-section-name' );
var marketplaceMenuIsOpen = false;
// Add event listener to toggle Marketplace menu on touch devices
if ( marketplaceSectionDropdown.length ) {
if ( isTouchDevice() ) {
marketplaceSectionName.on( 'click', function() {
marketplaceMenuIsOpen = ! marketplaceMenuIsOpen;
if ( marketplaceMenuIsOpen ) {
marketplaceSectionDropdown.addClass( 'is-open' );
$( document ).on( 'click', maybeToggleMarketplaceMenu );
} else {
marketplaceSectionDropdown.removeClass( 'is-open' );
$( document ).off( 'click', maybeToggleMarketplaceMenu );
}
} );
} else {
document.body.classList.add( 'no-touch' );
}
}
// Close menu if the user clicks outside it
function maybeToggleMarketplaceMenu( e ) {
if (
! marketplaceSectionDropdown.is( e.target )
&& marketplaceSectionDropdown.has( e.target ).length === 0
) {
marketplaceSectionDropdown.removeClass( 'is-open' );
marketplaceMenuIsOpen = false;
$( document ).off( 'click', maybeToggleMarketplaceMenu );
}
}
function isTouchDevice() {
return ( ( 'ontouchstart' in window ) ||
( navigator.maxTouchPoints > 0 ) ||
( navigator.msMaxTouchPoints > 0 ) );
}
});
})( jQuery, woocommerce_admin );

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,35 @@
jQuery( function( $ ) {
// woocommerce_params is required to continue, ensure the object exists
if ( typeof woocommerce_params === 'undefined' ) {
return false;
}
$( '#add_payment_method' )
/* Payment option selection */
.on( 'click init_add_payment_method', '.payment_methods input.input-radio', function() {
if ( $( '.payment_methods input.input-radio' ).length > 1 ) {
var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) );
if ( $( this ).is( ':checked' ) && ! target_payment_box.is( ':visible' ) ) {
$( 'div.payment_box' ).filter( ':visible' ).slideUp( 250 );
if ( $( this ).is( ':checked' ) ) {
$( 'div.payment_box.' + $( this ).attr( 'ID' ) ).slideDown( 250 );
}
}
} else {
$( 'div.payment_box' ).show();
}
})
// Trigger initial click
.find( 'input[name=payment_method]:checked' ).trigger( 'click' );
$( '#add_payment_method' ).on( 'submit', function() {
$( '#add_payment_method' ).block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } });
});
$( document.body ).trigger( 'init_add_payment_method' );
});

View File

@ -0,0 +1 @@
jQuery(function(e){return"undefined"!=typeof woocommerce_params&&(e("#add_payment_method").on("click init_add_payment_method",".payment_methods input.input-radio",function(){var t;1<e(".payment_methods input.input-radio").length?(t=e("div.payment_box."+e(this).attr("ID")),e(this).is(":checked")&&!t.is(":visible")&&(e("div.payment_box").filter(":visible").slideUp(250),e(this).is(":checked")&&e("div.payment_box."+e(this).attr("ID")).slideDown(250))):e("div.payment_box").show()}).find("input[name=payment_method]:checked").trigger("click"),e("#add_payment_method").on("submit",function(){e("#add_payment_method").block({message:null,overlayCSS:{background:"#fff",opacity:.6}})}),void e(document.body).trigger("init_add_payment_method"))});

View File

@ -0,0 +1,818 @@
/*global wc_add_to_cart_variation_params */
;(function ( $, window, document, undefined ) {
/**
* VariationForm class which handles variation forms and attributes.
*/
var VariationForm = function( $form ) {
var self = this;
self.$form = $form;
self.$attributeFields = $form.find( '.variations select' );
self.$singleVariation = $form.find( '.single_variation' );
self.$singleVariationWrap = $form.find( '.single_variation_wrap' );
self.$resetVariations = $form.find( '.reset_variations' );
self.$product = $form.closest( '.product' );
self.variationData = $form.data( 'product_variations' );
self.useAjax = false === self.variationData;
self.xhr = false;
self.loading = true;
// Initial state.
self.$singleVariationWrap.show();
self.$form.off( '.wc-variation-form' );
// Methods.
self.getChosenAttributes = self.getChosenAttributes.bind( self );
self.findMatchingVariations = self.findMatchingVariations.bind( self );
self.isMatch = self.isMatch.bind( self );
self.toggleResetLink = self.toggleResetLink.bind( self );
// Events.
$form.on( 'click.wc-variation-form', '.reset_variations', { variationForm: self }, self.onReset );
$form.on( 'reload_product_variations', { variationForm: self }, self.onReload );
$form.on( 'hide_variation', { variationForm: self }, self.onHide );
$form.on( 'show_variation', { variationForm: self }, self.onShow );
$form.on( 'click', '.single_add_to_cart_button', { variationForm: self }, self.onAddToCart );
$form.on( 'reset_data', { variationForm: self }, self.onResetDisplayedVariation );
$form.on( 'reset_image', { variationForm: self }, self.onResetImage );
$form.on( 'change.wc-variation-form', '.variations select', { variationForm: self }, self.onChange );
$form.on( 'found_variation.wc-variation-form', { variationForm: self }, self.onFoundVariation );
$form.on( 'check_variations.wc-variation-form', { variationForm: self }, self.onFindVariation );
$form.on( 'update_variation_values.wc-variation-form', { variationForm: self }, self.onUpdateAttributes );
// Init after gallery.
setTimeout( function() {
$form.trigger( 'check_variations' );
$form.trigger( 'wc_variation_form', self );
self.loading = false;
}, 100 );
};
/**
* Reset all fields.
*/
VariationForm.prototype.onReset = function( event ) {
event.preventDefault();
event.data.variationForm.$attributeFields.val( '' ).trigger( 'change' );
event.data.variationForm.$form.trigger( 'reset_data' );
};
/**
* Reload variation data from the DOM.
*/
VariationForm.prototype.onReload = function( event ) {
var form = event.data.variationForm;
form.variationData = form.$form.data( 'product_variations' );
form.useAjax = false === form.variationData;
form.$form.trigger( 'check_variations' );
};
/**
* When a variation is hidden.
*/
VariationForm.prototype.onHide = function( event ) {
event.preventDefault();
event.data.variationForm.$form
.find( '.single_add_to_cart_button' )
.removeClass( 'wc-variation-is-unavailable' )
.addClass( 'disabled wc-variation-selection-needed' );
event.data.variationForm.$form
.find( '.woocommerce-variation-add-to-cart' )
.removeClass( 'woocommerce-variation-add-to-cart-enabled' )
.addClass( 'woocommerce-variation-add-to-cart-disabled' );
};
/**
* When a variation is shown.
*/
VariationForm.prototype.onShow = function( event, variation, purchasable ) {
event.preventDefault();
if ( purchasable ) {
event.data.variationForm.$form
.find( '.single_add_to_cart_button' )
.removeClass( 'disabled wc-variation-selection-needed wc-variation-is-unavailable' );
event.data.variationForm.$form
.find( '.woocommerce-variation-add-to-cart' )
.removeClass( 'woocommerce-variation-add-to-cart-disabled' )
.addClass( 'woocommerce-variation-add-to-cart-enabled' );
} else {
event.data.variationForm.$form
.find( '.single_add_to_cart_button' )
.removeClass( 'wc-variation-selection-needed' )
.addClass( 'disabled wc-variation-is-unavailable' );
event.data.variationForm.$form
.find( '.woocommerce-variation-add-to-cart' )
.removeClass( 'woocommerce-variation-add-to-cart-enabled' )
.addClass( 'woocommerce-variation-add-to-cart-disabled' );
}
// If present, the media element library needs initialized on the variation description.
if ( wp.mediaelement ) {
event.data.variationForm.$form.find( '.wp-audio-shortcode, .wp-video-shortcode' )
.not( '.mejs-container' )
.filter(
function () {
return ! $( this ).parent().hasClass( 'mejs-mediaelement' );
}
)
.mediaelementplayer( wp.mediaelement.settings );
}
};
/**
* When the cart button is pressed.
*/
VariationForm.prototype.onAddToCart = function( event ) {
if ( $( this ).is('.disabled') ) {
event.preventDefault();
if ( $( this ).is('.wc-variation-is-unavailable') ) {
window.alert( wc_add_to_cart_variation_params.i18n_unavailable_text );
} else if ( $( this ).is('.wc-variation-selection-needed') ) {
window.alert( wc_add_to_cart_variation_params.i18n_make_a_selection_text );
}
}
};
/**
* When displayed variation data is reset.
*/
VariationForm.prototype.onResetDisplayedVariation = function( event ) {
var form = event.data.variationForm;
form.$product.find( '.product_meta' ).find( '.sku' ).wc_reset_content();
form.$product
.find( '.product_weight, .woocommerce-product-attributes-item--weight .woocommerce-product-attributes-item__value' )
.wc_reset_content();
form.$product
.find( '.product_dimensions, .woocommerce-product-attributes-item--dimensions .woocommerce-product-attributes-item__value' )
.wc_reset_content();
form.$form.trigger( 'reset_image' );
form.$singleVariation.slideUp( 200 ).trigger( 'hide_variation' );
};
/**
* When the product image is reset.
*/
VariationForm.prototype.onResetImage = function( event ) {
event.data.variationForm.$form.wc_variations_image_update( false );
};
/**
* Looks for matching variations for current selected attributes.
*/
VariationForm.prototype.onFindVariation = function( event, chosenAttributes ) {
var form = event.data.variationForm,
attributes = 'undefined' !== typeof chosenAttributes ? chosenAttributes : form.getChosenAttributes(),
currentAttributes = attributes.data;
if ( attributes.count && attributes.count === attributes.chosenCount ) {
if ( form.useAjax ) {
if ( form.xhr ) {
form.xhr.abort();
}
form.$form.block( { message: null, overlayCSS: { background: '#fff', opacity: 0.6 } } );
currentAttributes.product_id = parseInt( form.$form.data( 'product_id' ), 10 );
currentAttributes.custom_data = form.$form.data( 'custom_data' );
form.xhr = $.ajax( {
url: wc_add_to_cart_variation_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_variation' ),
type: 'POST',
data: currentAttributes,
success: function( variation ) {
if ( variation ) {
form.$form.trigger( 'found_variation', [ variation ] );
} else {
form.$form.trigger( 'reset_data' );
attributes.chosenCount = 0;
if ( ! form.loading ) {
form.$form
.find( '.single_variation' )
.after(
'<p class="wc-no-matching-variations woocommerce-info">' +
wc_add_to_cart_variation_params.i18n_no_matching_variations_text +
'</p>'
);
form.$form.find( '.wc-no-matching-variations' ).slideDown( 200 );
}
}
},
complete: function() {
form.$form.unblock();
}
} );
} else {
form.$form.trigger( 'update_variation_values' );
var matching_variations = form.findMatchingVariations( form.variationData, currentAttributes ),
variation = matching_variations.shift();
if ( variation ) {
form.$form.trigger( 'found_variation', [ variation ] );
} else {
form.$form.trigger( 'reset_data' );
attributes.chosenCount = 0;
if ( ! form.loading ) {
form.$form
.find( '.single_variation' )
.after(
'<p class="wc-no-matching-variations woocommerce-info">' +
wc_add_to_cart_variation_params.i18n_no_matching_variations_text +
'</p>'
);
form.$form.find( '.wc-no-matching-variations' ).slideDown( 200 );
}
}
}
} else {
form.$form.trigger( 'update_variation_values' );
form.$form.trigger( 'reset_data' );
}
// Show reset link.
form.toggleResetLink( attributes.chosenCount > 0 );
};
/**
* Triggered when a variation has been found which matches all attributes.
*/
VariationForm.prototype.onFoundVariation = function( event, variation ) {
var form = event.data.variationForm,
$sku = form.$product.find( '.product_meta' ).find( '.sku' ),
$weight = form.$product.find(
'.product_weight, .woocommerce-product-attributes-item--weight .woocommerce-product-attributes-item__value'
),
$dimensions = form.$product.find(
'.product_dimensions, .woocommerce-product-attributes-item--dimensions .woocommerce-product-attributes-item__value'
),
$qty = form.$singleVariationWrap.find( '.quantity' ),
purchasable = true,
variation_id = '',
template = false,
$template_html = '';
if ( variation.sku ) {
$sku.wc_set_content( variation.sku );
} else {
$sku.wc_reset_content();
}
if ( variation.weight ) {
$weight.wc_set_content( variation.weight_html );
} else {
$weight.wc_reset_content();
}
if ( variation.dimensions ) {
// Decode HTML entities.
$dimensions.wc_set_content( $.parseHTML( variation.dimensions_html )[0].data );
} else {
$dimensions.wc_reset_content();
}
form.$form.wc_variations_image_update( variation );
if ( ! variation.variation_is_visible ) {
template = wp_template( 'unavailable-variation-template' );
} else {
template = wp_template( 'variation-template' );
variation_id = variation.variation_id;
}
$template_html = template( {
variation: variation
} );
$template_html = $template_html.replace( '/*<![CDATA[*/', '' );
$template_html = $template_html.replace( '/*]]>*/', '' );
form.$singleVariation.html( $template_html );
form.$form.find( 'input[name="variation_id"], input.variation_id' ).val( variation.variation_id ).trigger( 'change' );
// Hide or show qty input
if ( variation.is_sold_individually === 'yes' ) {
$qty.find( 'input.qty' ).val( '1' ).attr( 'min', '1' ).attr( 'max', '' ).trigger( 'change' );
$qty.hide();
} else {
var $qty_input = $qty.find( 'input.qty' ),
qty_val = parseFloat( $qty_input.val() );
if ( isNaN( qty_val ) ) {
qty_val = variation.min_qty;
} else {
qty_val = qty_val > parseFloat( variation.max_qty ) ? variation.max_qty : qty_val;
qty_val = qty_val < parseFloat( variation.min_qty ) ? variation.min_qty : qty_val;
}
$qty_input.attr( 'min', variation.min_qty ).attr( 'max', variation.max_qty ).val( qty_val ).trigger( 'change' );
$qty.show();
}
// Enable or disable the add to cart button
if ( ! variation.is_purchasable || ! variation.is_in_stock || ! variation.variation_is_visible ) {
purchasable = false;
}
// Reveal
if ( form.$singleVariation.text().trim() ) {
form.$singleVariation.slideDown( 200 ).trigger( 'show_variation', [ variation, purchasable ] );
} else {
form.$singleVariation.show().trigger( 'show_variation', [ variation, purchasable ] );
}
};
/**
* Triggered when an attribute field changes.
*/
VariationForm.prototype.onChange = function( event ) {
var form = event.data.variationForm;
form.$form.find( 'input[name="variation_id"], input.variation_id' ).val( '' ).trigger( 'change' );
form.$form.find( '.wc-no-matching-variations' ).remove();
if ( form.useAjax ) {
form.$form.trigger( 'check_variations' );
} else {
form.$form.trigger( 'woocommerce_variation_select_change' );
form.$form.trigger( 'check_variations' );
}
// Custom event for when variation selection has been changed
form.$form.trigger( 'woocommerce_variation_has_changed' );
};
/**
* Escape quotes in a string.
* @param {string} string
* @return {string}
*/
VariationForm.prototype.addSlashes = function( string ) {
string = string.replace( /'/g, '\\\'' );
string = string.replace( /"/g, '\\\"' );
return string;
};
/**
* Updates attributes in the DOM to show valid values.
*/
VariationForm.prototype.onUpdateAttributes = function( event ) {
var form = event.data.variationForm,
attributes = form.getChosenAttributes(),
currentAttributes = attributes.data;
if ( form.useAjax ) {
return;
}
// Loop through selects and disable/enable options based on selections.
form.$attributeFields.each( function( index, el ) {
var current_attr_select = $( el ),
current_attr_name = current_attr_select.data( 'attribute_name' ) || current_attr_select.attr( 'name' ),
show_option_none = $( el ).data( 'show_option_none' ),
option_gt_filter = ':gt(0)',
attached_options_count = 0,
new_attr_select = $( '<select/>' ),
selected_attr_val = current_attr_select.val() || '',
selected_attr_val_valid = true;
// Reference options set at first.
if ( ! current_attr_select.data( 'attribute_html' ) ) {
var refSelect = current_attr_select.clone();
refSelect.find( 'option' ).removeAttr( 'attached' ).prop( 'disabled', false ).prop( 'selected', false );
// Legacy data attribute.
current_attr_select.data(
'attribute_options',
refSelect.find( 'option' + option_gt_filter ).get()
);
current_attr_select.data( 'attribute_html', refSelect.html() );
}
new_attr_select.html( current_attr_select.data( 'attribute_html' ) );
// The attribute of this select field should not be taken into account when calculating its matching variations:
// The constraints of this attribute are shaped by the values of the other attributes.
var checkAttributes = $.extend( true, {}, currentAttributes );
checkAttributes[ current_attr_name ] = '';
var variations = form.findMatchingVariations( form.variationData, checkAttributes );
// Loop through variations.
for ( var num in variations ) {
if ( typeof( variations[ num ] ) !== 'undefined' ) {
var variationAttributes = variations[ num ].attributes;
for ( var attr_name in variationAttributes ) {
if ( variationAttributes.hasOwnProperty( attr_name ) ) {
var attr_val = variationAttributes[ attr_name ],
variation_active = '';
if ( attr_name === current_attr_name ) {
if ( variations[ num ].variation_is_active ) {
variation_active = 'enabled';
}
if ( attr_val ) {
// Decode entities.
attr_val = $( '<div/>' ).html( attr_val ).text();
// Attach to matching options by value. This is done to compare
// TEXT values rather than any HTML entities.
var $option_elements = new_attr_select.find( 'option' );
if ( $option_elements.length ) {
for (var i = 0, len = $option_elements.length; i < len; i++) {
var $option_element = $( $option_elements[i] ),
option_value = $option_element.val();
if ( attr_val === option_value ) {
$option_element.addClass( 'attached ' + variation_active );
break;
}
}
}
} else {
// Attach all apart from placeholder.
new_attr_select.find( 'option:gt(0)' ).addClass( 'attached ' + variation_active );
}
}
}
}
}
}
// Count available options.
attached_options_count = new_attr_select.find( 'option.attached' ).length;
// Check if current selection is in attached options.
if ( selected_attr_val ) {
selected_attr_val_valid = false;
if ( 0 !== attached_options_count ) {
new_attr_select.find( 'option.attached.enabled' ).each( function() {
var option_value = $( this ).val();
if ( selected_attr_val === option_value ) {
selected_attr_val_valid = true;
return false; // break.
}
});
}
}
// Detach the placeholder if:
// - Valid options exist.
// - The current selection is non-empty.
// - The current selection is valid.
// - Placeholders are not set to be permanently visible.
if ( attached_options_count > 0 && selected_attr_val && selected_attr_val_valid && ( 'no' === show_option_none ) ) {
new_attr_select.find( 'option:first' ).remove();
option_gt_filter = '';
}
// Detach unattached.
new_attr_select.find( 'option' + option_gt_filter + ':not(.attached)' ).remove();
// Finally, copy to DOM and set value.
current_attr_select.html( new_attr_select.html() );
current_attr_select.find( 'option' + option_gt_filter + ':not(.enabled)' ).prop( 'disabled', true );
// Choose selected value.
if ( selected_attr_val ) {
// If the previously selected value is no longer available, fall back to the placeholder (it's going to be there).
if ( selected_attr_val_valid ) {
current_attr_select.val( selected_attr_val );
} else {
current_attr_select.val( '' ).trigger( 'change' );
}
} else {
current_attr_select.val( '' ); // No change event to prevent infinite loop.
}
});
// Custom event for when variations have been updated.
form.$form.trigger( 'woocommerce_update_variation_values' );
};
/**
* Get chosen attributes from form.
* @return array
*/
VariationForm.prototype.getChosenAttributes = function() {
var data = {};
var count = 0;
var chosen = 0;
this.$attributeFields.each( function() {
var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );
var value = $( this ).val() || '';
if ( value.length > 0 ) {
chosen ++;
}
count ++;
data[ attribute_name ] = value;
});
return {
'count' : count,
'chosenCount': chosen,
'data' : data
};
};
/**
* Find matching variations for attributes.
*/
VariationForm.prototype.findMatchingVariations = function( variations, attributes ) {
var matching = [];
for ( var i = 0; i < variations.length; i++ ) {
var variation = variations[i];
if ( this.isMatch( variation.attributes, attributes ) ) {
matching.push( variation );
}
}
return matching;
};
/**
* See if attributes match.
* @return {Boolean}
*/
VariationForm.prototype.isMatch = function( variation_attributes, attributes ) {
var match = true;
for ( var attr_name in variation_attributes ) {
if ( variation_attributes.hasOwnProperty( attr_name ) ) {
var val1 = variation_attributes[ attr_name ];
var val2 = attributes[ attr_name ];
if ( val1 !== undefined && val2 !== undefined && val1.length !== 0 && val2.length !== 0 && val1 !== val2 ) {
match = false;
}
}
}
return match;
};
/**
* Show or hide the reset link.
*/
VariationForm.prototype.toggleResetLink = function( on ) {
if ( on ) {
if ( this.$resetVariations.css( 'visibility' ) === 'hidden' ) {
this.$resetVariations.css( 'visibility', 'visible' ).hide().fadeIn();
}
} else {
this.$resetVariations.css( 'visibility', 'hidden' );
}
};
/**
* Function to call wc_variation_form on jquery selector.
*/
$.fn.wc_variation_form = function() {
new VariationForm( this );
return this;
};
/**
* Stores the default text for an element so it can be reset later
*/
$.fn.wc_set_content = function( content ) {
if ( undefined === this.attr( 'data-o_content' ) ) {
this.attr( 'data-o_content', this.text() );
}
this.text( content );
};
/**
* Stores the default text for an element so it can be reset later
*/
$.fn.wc_reset_content = function() {
if ( undefined !== this.attr( 'data-o_content' ) ) {
this.text( this.attr( 'data-o_content' ) );
}
};
/**
* Stores a default attribute for an element so it can be reset later
*/
$.fn.wc_set_variation_attr = function( attr, value ) {
if ( undefined === this.attr( 'data-o_' + attr ) ) {
this.attr( 'data-o_' + attr, ( ! this.attr( attr ) ) ? '' : this.attr( attr ) );
}
if ( false === value ) {
this.removeAttr( attr );
} else {
this.attr( attr, value );
}
};
/**
* Reset a default attribute for an element so it can be reset later
*/
$.fn.wc_reset_variation_attr = function( attr ) {
if ( undefined !== this.attr( 'data-o_' + attr ) ) {
this.attr( attr, this.attr( 'data-o_' + attr ) );
}
};
/**
* Reset the slide position if the variation has a different image than the current one
*/
$.fn.wc_maybe_trigger_slide_position_reset = function( variation ) {
var $form = $( this ),
$product = $form.closest( '.product' ),
$product_gallery = $product.find( '.images' ),
reset_slide_position = false,
new_image_id = ( variation && variation.image_id ) ? variation.image_id : '';
if ( $form.attr( 'current-image' ) !== new_image_id ) {
reset_slide_position = true;
}
$form.attr( 'current-image', new_image_id );
if ( reset_slide_position ) {
$product_gallery.trigger( 'woocommerce_gallery_reset_slide_position' );
}
};
/**
* Sets product images for the chosen variation
*/
$.fn.wc_variations_image_update = function( variation ) {
var $form = this,
$product = $form.closest( '.product' ),
$product_gallery = $product.find( '.images' ),
$gallery_nav = $product.find( '.flex-control-nav' ),
$gallery_img = $gallery_nav.find( 'li:eq(0) img' ),
$product_img_wrap = $product_gallery
.find( '.woocommerce-product-gallery__image, .woocommerce-product-gallery__image--placeholder' )
.eq( 0 ),
$product_img = $product_img_wrap.find( '.wp-post-image' ),
$product_link = $product_img_wrap.find( 'a' ).eq( 0 );
if ( variation && variation.image && variation.image.src && variation.image.src.length > 1 ) {
// See if the gallery has an image with the same original src as the image we want to switch to.
var galleryHasImage = $gallery_nav.find( 'li img[data-o_src="' + variation.image.gallery_thumbnail_src + '"]' ).length > 0;
// If the gallery has the image, reset the images. We'll scroll to the correct one.
if ( galleryHasImage ) {
$form.wc_variations_image_reset();
}
// See if gallery has a matching image we can slide to.
var slideToImage = $gallery_nav.find( 'li img[src="' + variation.image.gallery_thumbnail_src + '"]' );
if ( slideToImage.length > 0 ) {
slideToImage.trigger( 'click' );
$form.attr( 'current-image', variation.image_id );
window.setTimeout( function() {
$( window ).trigger( 'resize' );
$product_gallery.trigger( 'woocommerce_gallery_init_zoom' );
}, 20 );
return;
}
$product_img.wc_set_variation_attr( 'src', variation.image.src );
$product_img.wc_set_variation_attr( 'height', variation.image.src_h );
$product_img.wc_set_variation_attr( 'width', variation.image.src_w );
$product_img.wc_set_variation_attr( 'srcset', variation.image.srcset );
$product_img.wc_set_variation_attr( 'sizes', variation.image.sizes );
$product_img.wc_set_variation_attr( 'title', variation.image.title );
$product_img.wc_set_variation_attr( 'data-caption', variation.image.caption );
$product_img.wc_set_variation_attr( 'alt', variation.image.alt );
$product_img.wc_set_variation_attr( 'data-src', variation.image.full_src );
$product_img.wc_set_variation_attr( 'data-large_image', variation.image.full_src );
$product_img.wc_set_variation_attr( 'data-large_image_width', variation.image.full_src_w );
$product_img.wc_set_variation_attr( 'data-large_image_height', variation.image.full_src_h );
$product_img_wrap.wc_set_variation_attr( 'data-thumb', variation.image.src );
$gallery_img.wc_set_variation_attr( 'src', variation.image.gallery_thumbnail_src );
$product_link.wc_set_variation_attr( 'href', variation.image.full_src );
} else {
$form.wc_variations_image_reset();
}
window.setTimeout( function() {
$( window ).trigger( 'resize' );
$form.wc_maybe_trigger_slide_position_reset( variation );
$product_gallery.trigger( 'woocommerce_gallery_init_zoom' );
}, 20 );
};
/**
* Reset main image to defaults.
*/
$.fn.wc_variations_image_reset = function() {
var $form = this,
$product = $form.closest( '.product' ),
$product_gallery = $product.find( '.images' ),
$gallery_nav = $product.find( '.flex-control-nav' ),
$gallery_img = $gallery_nav.find( 'li:eq(0) img' ),
$product_img_wrap = $product_gallery
.find( '.woocommerce-product-gallery__image, .woocommerce-product-gallery__image--placeholder' )
.eq( 0 ),
$product_img = $product_img_wrap.find( '.wp-post-image' ),
$product_link = $product_img_wrap.find( 'a' ).eq( 0 );
$product_img.wc_reset_variation_attr( 'src' );
$product_img.wc_reset_variation_attr( 'width' );
$product_img.wc_reset_variation_attr( 'height' );
$product_img.wc_reset_variation_attr( 'srcset' );
$product_img.wc_reset_variation_attr( 'sizes' );
$product_img.wc_reset_variation_attr( 'title' );
$product_img.wc_reset_variation_attr( 'data-caption' );
$product_img.wc_reset_variation_attr( 'alt' );
$product_img.wc_reset_variation_attr( 'data-src' );
$product_img.wc_reset_variation_attr( 'data-large_image' );
$product_img.wc_reset_variation_attr( 'data-large_image_width' );
$product_img.wc_reset_variation_attr( 'data-large_image_height' );
$product_img_wrap.wc_reset_variation_attr( 'data-thumb' );
$gallery_img.wc_reset_variation_attr( 'src' );
$product_link.wc_reset_variation_attr( 'href' );
};
$(function() {
if ( typeof wc_add_to_cart_variation_params !== 'undefined' ) {
$( '.variations_form' ).each( function() {
$( this ).wc_variation_form();
});
}
});
/**
* Matches inline variation objects to chosen attributes
* @deprecated 2.6.9
* @type {Object}
*/
var wc_variation_form_matcher = {
find_matching_variations: function( product_variations, settings ) {
var matching = [];
for ( var i = 0; i < product_variations.length; i++ ) {
var variation = product_variations[i];
if ( wc_variation_form_matcher.variations_match( variation.attributes, settings ) ) {
matching.push( variation );
}
}
return matching;
},
variations_match: function( attrs1, attrs2 ) {
var match = true;
for ( var attr_name in attrs1 ) {
if ( attrs1.hasOwnProperty( attr_name ) ) {
var val1 = attrs1[ attr_name ];
var val2 = attrs2[ attr_name ];
if ( val1 !== undefined && val2 !== undefined && val1.length !== 0 && val2.length !== 0 && val1 !== val2 ) {
match = false;
}
}
}
return match;
}
};
/**
* Avoids using wp.template where possible in order to be CSP compliant.
* wp.template uses internally eval().
* @param {string} templateId
* @return {Function}
*/
var wp_template = function( templateId ) {
var html = document.getElementById( 'tmpl-' + templateId ).textContent;
var hard = false;
// any <# #> interpolate (evaluate).
hard = hard || /<#\s?data\./.test( html );
// any data that is NOT data.variation.
hard = hard || /{{{?\s?data\.(?!variation\.).+}}}?/.test( html );
// any data access deeper than 1 level e.g.
// data.variation.object.item
// data.variation.object['item']
// data.variation.array[0]
hard = hard || /{{{?\s?data\.variation\.[\w-]*[^\s}]/.test ( html );
if ( hard ) {
return wp.template( templateId );
}
return function template ( data ) {
var variation = data.variation || {};
return html.replace( /({{{?)\s?data\.variation\.([\w-]*)\s?(}}}?)/g, function( _, open, key, close ) {
// Error in the format, ignore.
if ( open.length !== close.length ) {
return '';
}
var replacement = variation[ key ] || '';
// {{{ }}} => interpolate (unescaped).
// {{ }} => interpolate (escaped).
// https://codex.wordpress.org/Javascript_Reference/wp.template
if ( open.length === 2 ) {
return window.escape( replacement );
}
return replacement;
});
};
};
})( jQuery, window, document );

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,214 @@
/* global wc_add_to_cart_params */
jQuery( function( $ ) {
if ( typeof wc_add_to_cart_params === 'undefined' ) {
return false;
}
/**
* AddToCartHandler class.
*/
var AddToCartHandler = function() {
this.requests = [];
this.addRequest = this.addRequest.bind( this );
this.run = this.run.bind( this );
$( document.body )
.on( 'click', '.add_to_cart_button', { addToCartHandler: this }, this.onAddToCart )
.on( 'click', '.remove_from_cart_button', { addToCartHandler: this }, this.onRemoveFromCart )
.on( 'added_to_cart', this.updateButton )
.on( 'ajax_request_not_sent.adding_to_cart', this.updateButton )
.on( 'added_to_cart removed_from_cart', { addToCartHandler: this }, this.updateFragments );
};
/**
* Add add to cart event.
*/
AddToCartHandler.prototype.addRequest = function( request ) {
this.requests.push( request );
if ( 1 === this.requests.length ) {
this.run();
}
};
/**
* Run add to cart events.
*/
AddToCartHandler.prototype.run = function() {
var requestManager = this,
originalCallback = requestManager.requests[0].complete;
requestManager.requests[0].complete = function() {
if ( typeof originalCallback === 'function' ) {
originalCallback();
}
requestManager.requests.shift();
if ( requestManager.requests.length > 0 ) {
requestManager.run();
}
};
$.ajax( this.requests[0] );
};
/**
* Handle the add to cart event.
*/
AddToCartHandler.prototype.onAddToCart = function( e ) {
var $thisbutton = $( this );
if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
if ( ! $thisbutton.attr( 'data-product_id' ) ) {
return true;
}
e.preventDefault();
$thisbutton.removeClass( 'added' );
$thisbutton.addClass( 'loading' );
// Allow 3rd parties to validate and quit early.
if ( false === $( document.body ).triggerHandler( 'should_send_ajax_request.adding_to_cart', [ $thisbutton ] ) ) {
$( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] );
return true;
}
var data = {};
// Fetch changes that are directly added by calling $thisbutton.data( key, value )
$.each( $thisbutton.data(), function( key, value ) {
data[ key ] = value;
});
// Fetch data attributes in $thisbutton. Give preference to data-attributes because they can be directly modified by javascript
// while `.data` are jquery specific memory stores.
$.each( $thisbutton[0].dataset, function( key, value ) {
data[ key ] = value;
});
// Trigger event.
$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
e.data.addToCartHandler.addRequest({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ),
data: data,
success: function( response ) {
if ( ! response ) {
return;
}
if ( response.error && response.product_url ) {
window.location = response.product_url;
return;
}
// Redirect to cart option
if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
window.location = wc_add_to_cart_params.cart_url;
return;
}
// Trigger event so themes can refresh other areas.
$( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
},
dataType: 'json'
});
}
};
/**
* Update fragments after remove from cart event in mini-cart.
*/
AddToCartHandler.prototype.onRemoveFromCart = function( e ) {
var $thisbutton = $( this ),
$row = $thisbutton.closest( '.woocommerce-mini-cart-item' );
e.preventDefault();
$row.block({
message: null,
overlayCSS: {
opacity: 0.6
}
});
e.data.addToCartHandler.addRequest({
type: 'POST',
url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ),
data: {
cart_item_key : $thisbutton.data( 'cart_item_key' )
},
success: function( response ) {
if ( ! response || ! response.fragments ) {
window.location = $thisbutton.attr( 'href' );
return;
}
$( document.body ).trigger( 'removed_from_cart', [ response.fragments, response.cart_hash, $thisbutton ] );
},
error: function() {
window.location = $thisbutton.attr( 'href' );
return;
},
dataType: 'json'
});
};
/**
* Update cart page elements after add to cart events.
*/
AddToCartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
$button = typeof $button === 'undefined' ? false : $button;
if ( $button ) {
$button.removeClass( 'loading' );
if ( fragments ) {
$button.addClass( 'added' );
}
// View cart text.
if ( fragments && ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) {
$button.after( '<a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
}
$( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
}
};
/**
* Update fragments after add to cart events.
*/
AddToCartHandler.prototype.updateFragments = function( e, fragments ) {
if ( fragments ) {
$.each( fragments, function( key ) {
$( key )
.addClass( 'updating' )
.fadeTo( '400', '0.6' )
.block({
message: null,
overlayCSS: {
opacity: 0.6
}
});
});
$.each( fragments, function( key, value ) {
$( key ).replaceWith( value );
$( key ).stop( true ).css( 'opacity', '1' ).unblock();
});
$( document.body ).trigger( 'wc_fragments_loaded' );
}
};
/**
* Init AddToCartHandler.
*/
new AddToCartHandler();
});

1
assets/js/frontend/add-to-cart.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(d){if("undefined"==typeof wc_add_to_cart_params)return!1;var t=function(){this.requests=[],this.addRequest=this.addRequest.bind(this),this.run=this.run.bind(this),d(document.body).on("click",".add_to_cart_button",{addToCartHandler:this},this.onAddToCart).on("click",".remove_from_cart_button",{addToCartHandler:this},this.onRemoveFromCart).on("added_to_cart",this.updateButton).on("ajax_request_not_sent.adding_to_cart",this.updateButton).on("added_to_cart removed_from_cart",{addToCartHandler:this},this.updateFragments)};t.prototype.addRequest=function(t){this.requests.push(t),1===this.requests.length&&this.run()},t.prototype.run=function(){var t=this,a=t.requests[0].complete;t.requests[0].complete=function(){"function"==typeof a&&a(),t.requests.shift(),0<t.requests.length&&t.run()},d.ajax(this.requests[0])},t.prototype.onAddToCart=function(t){var a=d(this);if(a.is(".ajax_add_to_cart")){if(!a.attr("data-product_id"))return!0;if(t.preventDefault(),a.removeClass("added"),a.addClass("loading"),!1===d(document.body).triggerHandler("should_send_ajax_request.adding_to_cart",[a]))return d(document.body).trigger("ajax_request_not_sent.adding_to_cart",[!1,!1,a]),!0;var e={};d.each(a.data(),function(t,a){e[t]=a}),d.each(a[0].dataset,function(t,a){e[t]=a}),d(document.body).trigger("adding_to_cart",[a,e]),t.data.addToCartHandler.addRequest({type:"POST",url:wc_add_to_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","add_to_cart"),data:e,success:function(t){t&&(t.error&&t.product_url?window.location=t.product_url:"yes"!==wc_add_to_cart_params.cart_redirect_after_add?d(document.body).trigger("added_to_cart",[t.fragments,t.cart_hash,a]):window.location=wc_add_to_cart_params.cart_url)},dataType:"json"})}},t.prototype.onRemoveFromCart=function(t){var a=d(this),e=a.closest(".woocommerce-mini-cart-item");t.preventDefault(),e.block({message:null,overlayCSS:{opacity:.6}}),t.data.addToCartHandler.addRequest({type:"POST",url:wc_add_to_cart_params.wc_ajax_url.toString().replace("%%endpoint%%","remove_from_cart"),data:{cart_item_key:a.data("cart_item_key")},success:function(t){t&&t.fragments?d(document.body).trigger("removed_from_cart",[t.fragments,t.cart_hash,a]):window.location=a.attr("href")},error:function(){window.location=a.attr("href")},dataType:"json"})},t.prototype.updateButton=function(t,a,e,r){(r=void 0!==r&&r)&&(r.removeClass("loading"),a&&r.addClass("added"),a&&!wc_add_to_cart_params.is_cart&&0===r.parent().find(".added_to_cart").length&&r.after('<a href="'+wc_add_to_cart_params.cart_url+'" class="added_to_cart wc-forward" title="'+wc_add_to_cart_params.i18n_view_cart+'">'+wc_add_to_cart_params.i18n_view_cart+"</a>"),d(document.body).trigger("wc_cart_button_updated",[r]))},t.prototype.updateFragments=function(t,a){a&&(d.each(a,function(t){d(t).addClass("updating").fadeTo("400","0.6").block({message:null,overlayCSS:{opacity:.6}})}),d.each(a,function(t,a){d(t).replaceWith(a),d(t).stop(!0).css("opacity","1").unblock()}),d(document.body).trigger("wc_fragments_loaded"))},new t});

View File

@ -0,0 +1,151 @@
/*global wc_address_i18n_params */
jQuery( function( $ ) {
// wc_address_i18n_params is required to continue, ensure the object exists
if ( typeof wc_address_i18n_params === 'undefined' ) {
return false;
}
var locale_json = wc_address_i18n_params.locale.replace( /&quot;/g, '"' ), locale = JSON.parse( locale_json );
function field_is_required( field, is_required ) {
if ( is_required ) {
field.find( 'label .optional' ).remove();
field.addClass( 'validate-required' );
if ( field.find( 'label .required' ).length === 0 ) {
field.find( 'label' ).append(
'&nbsp;<abbr class="required" title="' +
wc_address_i18n_params.i18n_required_text +
'">*</abbr>'
);
}
} else {
field.find( 'label .required' ).remove();
field.removeClass( 'validate-required woocommerce-invalid woocommerce-invalid-required-field' );
if ( field.find( 'label .optional' ).length === 0 ) {
field.find( 'label' ).append( '&nbsp;<span class="optional">(' + wc_address_i18n_params.i18n_optional_text + ')</span>' );
}
}
}
// Handle locale
$( document.body )
.on( 'country_to_state_changing', function( event, country, wrapper ) {
var thisform = wrapper, thislocale;
if ( typeof locale[ country ] !== 'undefined' ) {
thislocale = locale[ country ];
} else {
thislocale = locale['default'];
}
var $postcodefield = thisform.find( '#billing_postcode_field, #shipping_postcode_field' ),
$cityfield = thisform.find( '#billing_city_field, #shipping_city_field' ),
$statefield = thisform.find( '#billing_state_field, #shipping_state_field' );
if ( ! $postcodefield.attr( 'data-o_class' ) ) {
$postcodefield.attr( 'data-o_class', $postcodefield.attr( 'class' ) );
$cityfield.attr( 'data-o_class', $cityfield.attr( 'class' ) );
$statefield.attr( 'data-o_class', $statefield.attr( 'class' ) );
}
var locale_fields = JSON.parse( wc_address_i18n_params.locale_fields );
$.each( locale_fields, function( key, value ) {
var field = thisform.find( value ),
fieldLocale = $.extend( true, {}, locale['default'][ key ], thislocale[ key ] );
// Labels.
if ( typeof fieldLocale.label !== 'undefined' ) {
field.find( 'label' ).html( fieldLocale.label );
}
// Placeholders.
if ( typeof fieldLocale.placeholder !== 'undefined' ) {
field.find( ':input' ).attr( 'placeholder', fieldLocale.placeholder );
field.find( ':input' ).attr( 'data-placeholder', fieldLocale.placeholder );
field.find( '.select2-selection__placeholder' ).text( fieldLocale.placeholder );
}
// Use the i18n label as a placeholder if there is no label element and no i18n placeholder.
if (
typeof fieldLocale.placeholder === 'undefined' &&
typeof fieldLocale.label !== 'undefined' &&
! field.find( 'label' ).length
) {
field.find( ':input' ).attr( 'placeholder', fieldLocale.label );
field.find( ':input' ).attr( 'data-placeholder', fieldLocale.label );
field.find( '.select2-selection__placeholder' ).text( fieldLocale.label );
}
// Required.
if ( typeof fieldLocale.required !== 'undefined' ) {
field_is_required( field, fieldLocale.required );
} else {
field_is_required( field, false );
}
// Priority.
if ( typeof fieldLocale.priority !== 'undefined' ) {
field.data( 'priority', fieldLocale.priority );
}
// Hidden fields.
if ( 'state' !== key ) {
if ( typeof fieldLocale.hidden !== 'undefined' && true === fieldLocale.hidden ) {
field.hide().find( ':input' ).val( '' );
} else {
field.show();
}
}
// Class changes.
if ( Array.isArray( fieldLocale.class ) ) {
field.removeClass( 'form-row-first form-row-last form-row-wide' );
field.addClass( fieldLocale.class.join( ' ' ) );
}
});
var fieldsets = $(
'.woocommerce-billing-fields__field-wrapper,' +
'.woocommerce-shipping-fields__field-wrapper,' +
'.woocommerce-address-fields__field-wrapper,' +
'.woocommerce-additional-fields__field-wrapper .woocommerce-account-fields'
);
fieldsets.each( function( index, fieldset ) {
var rows = $( fieldset ).find( '.form-row' );
var wrapper = rows.first().parent();
// Before sorting, ensure all fields have a priority for bW compatibility.
var last_priority = 0;
rows.each( function() {
if ( ! $( this ).data( 'priority' ) ) {
$( this ).data( 'priority', last_priority + 1 );
}
last_priority = $( this ).data( 'priority' );
} );
// Sort the fields.
rows.sort( function( a, b ) {
var asort = parseInt( $( a ).data( 'priority' ), 10 ),
bsort = parseInt( $( b ).data( 'priority' ), 10 );
if ( asort > bsort ) {
return 1;
}
if ( asort < bsort ) {
return -1;
}
return 0;
});
rows.detach().appendTo( wrapper );
});
})
.trigger( 'wc_address_i18n_ready' );
});

View File

@ -0,0 +1 @@
jQuery(function(l){if("undefined"==typeof wc_address_i18n_params)return!1;var e=wc_address_i18n_params.locale.replace(/&quot;/g,'"'),n=JSON.parse(e);function o(e,a){a?(e.find("label .optional").remove(),e.addClass("validate-required"),0===e.find("label .required").length&&e.find("label").append('&nbsp;<abbr class="required" title="'+wc_address_i18n_params.i18n_required_text+'">*</abbr>')):(e.find("label .required").remove(),e.removeClass("validate-required woocommerce-invalid woocommerce-invalid-required-field"),0===e.find("label .optional").length&&e.find("label").append('&nbsp;<span class="optional">('+wc_address_i18n_params.i18n_optional_text+")</span>"))}l(document.body).on("country_to_state_changing",function(e,a,i){var d=i,r="undefined"!=typeof n[a]?n[a]:n["default"],t=d.find("#billing_postcode_field, #shipping_postcode_field"),i=d.find("#billing_city_field, #shipping_city_field"),a=d.find("#billing_state_field, #shipping_state_field");t.attr("data-o_class")||(t.attr("data-o_class",t.attr("class")),i.attr("data-o_class",i.attr("class")),a.attr("data-o_class",a.attr("class")));a=JSON.parse(wc_address_i18n_params.locale_fields);l.each(a,function(e,a){var i=d.find(a),a=l.extend(!0,{},n["default"][e],r[e]);"undefined"!=typeof a.label&&i.find("label").html(a.label),"undefined"!=typeof a.placeholder&&(i.find(":input").attr("placeholder",a.placeholder),i.find(":input").attr("data-placeholder",a.placeholder),i.find(".select2-selection__placeholder").text(a.placeholder)),"undefined"!=typeof a.placeholder||"undefined"==typeof a.label||i.find("label").length||(i.find(":input").attr("placeholder",a.label),i.find(":input").attr("data-placeholder",a.label),i.find(".select2-selection__placeholder").text(a.label)),"undefined"!=typeof a.required?o(i,a.required):o(i,!1),"undefined"!=typeof a.priority&&i.data("priority",a.priority),"state"!==e&&("undefined"!=typeof a.hidden&&!0===a.hidden?i.hide().find(":input").val(""):i.show()),Array.isArray(a["class"])&&(i.removeClass("form-row-first form-row-last form-row-wide"),i.addClass(a["class"].join(" ")))}),l(".woocommerce-billing-fields__field-wrapper,.woocommerce-shipping-fields__field-wrapper,.woocommerce-address-fields__field-wrapper,.woocommerce-additional-fields__field-wrapper .woocommerce-account-fields").each(function(e,a){var i=l(a).find(".form-row"),a=i.first().parent(),d=0;i.each(function(){l(this).data("priority")||l(this).data("priority",d+1),d=l(this).data("priority")}),i.sort(function(e,a){e=parseInt(l(e).data("priority"),10),a=parseInt(l(a).data("priority"),10);return a<e?1:e<a?-1:0}),i.detach().appendTo(a)})}).trigger("wc_address_i18n_ready")});

View File

@ -0,0 +1,187 @@
/* global wc_cart_fragments_params, Cookies */
jQuery( function( $ ) {
// wc_cart_fragments_params is required to continue, ensure the object exists
if ( typeof wc_cart_fragments_params === 'undefined' ) {
return false;
}
/* Storage Handling */
var $supports_html5_storage = true,
cart_hash_key = wc_cart_fragments_params.cart_hash_key;
try {
$supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null );
window.sessionStorage.setItem( 'wc', 'test' );
window.sessionStorage.removeItem( 'wc' );
window.localStorage.setItem( 'wc', 'test' );
window.localStorage.removeItem( 'wc' );
} catch( err ) {
$supports_html5_storage = false;
}
/* Cart session creation time to base expiration on */
function set_cart_creation_timestamp() {
if ( $supports_html5_storage ) {
sessionStorage.setItem( 'wc_cart_created', ( new Date() ).getTime() );
}
}
/** Set the cart hash in both session and local storage */
function set_cart_hash( cart_hash ) {
if ( $supports_html5_storage ) {
localStorage.setItem( cart_hash_key, cart_hash );
sessionStorage.setItem( cart_hash_key, cart_hash );
}
}
var $fragment_refresh = {
url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ),
type: 'POST',
data: {
time: new Date().getTime()
},
timeout: wc_cart_fragments_params.request_timeout,
success: function( data ) {
if ( data && data.fragments ) {
$.each( data.fragments, function( key, value ) {
$( key ).replaceWith( value );
});
if ( $supports_html5_storage ) {
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) );
set_cart_hash( data.cart_hash );
if ( data.cart_hash ) {
set_cart_creation_timestamp();
}
}
$( document.body ).trigger( 'wc_fragments_refreshed' );
}
},
error: function() {
$( document.body ).trigger( 'wc_fragments_ajax_error' );
}
};
/* Named callback for refreshing cart fragment */
function refresh_cart_fragment() {
$.ajax( $fragment_refresh );
}
/* Cart Handling */
if ( $supports_html5_storage ) {
var cart_timeout = null,
day_in_ms = ( 24 * 60 * 60 * 1000 );
$( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() {
refresh_cart_fragment();
});
$( document.body ).on( 'added_to_cart removed_from_cart', function( event, fragments, cart_hash ) {
var prev_cart_hash = sessionStorage.getItem( cart_hash_key );
if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) {
set_cart_creation_timestamp();
}
sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) );
set_cart_hash( cart_hash );
});
$( document.body ).on( 'wc_fragments_refreshed', function() {
clearTimeout( cart_timeout );
cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms );
} );
// Refresh when storage changes in another tab
$( window ).on( 'storage onstorage', function ( e ) {
if (
cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key )
) {
refresh_cart_fragment();
}
});
// Refresh when page is shown after back button (safari)
$( window ).on( 'pageshow' , function( e ) {
if ( e.originalEvent.persisted ) {
$( '.widget_shopping_cart_content' ).empty();
$( document.body ).trigger( 'wc_fragment_refresh' );
}
} );
try {
var wc_fragments = JSON.parse( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ),
cart_hash = sessionStorage.getItem( cart_hash_key ),
cookie_hash = Cookies.get( 'woocommerce_cart_hash'),
cart_created = sessionStorage.getItem( 'wc_cart_created' );
if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) {
cart_hash = '';
}
if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) {
cookie_hash = '';
}
if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) {
throw 'No cart_created';
}
if ( cart_created ) {
var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ),
timestamp_now = ( new Date() ).getTime();
if ( cart_expiration < timestamp_now ) {
throw 'Fragment expired';
}
cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) );
}
if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) {
$.each( wc_fragments, function( key, value ) {
$( key ).replaceWith(value);
});
$( document.body ).trigger( 'wc_fragments_loaded' );
} else {
throw 'No fragment';
}
} catch( err ) {
refresh_cart_fragment();
}
} else {
refresh_cart_fragment();
}
/* Cart Hiding */
if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) {
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
} else {
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide();
}
$( document.body ).on( 'adding_to_cart', function() {
$( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show();
});
// Customiser support.
var hasSelectiveRefresh = (
'undefined' !== typeof wp &&
wp.customize &&
wp.customize.selectiveRefresh &&
wp.customize.widgetsPreview &&
wp.customize.widgetsPreview.WidgetPartial
);
if ( hasSelectiveRefresh ) {
wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() {
refresh_cart_fragment();
} );
}
});

View File

@ -0,0 +1 @@
jQuery(function(r){if("undefined"==typeof wc_cart_fragments_params)return!1;var t=!0,o=wc_cart_fragments_params.cart_hash_key;try{t="sessionStorage"in window&&null!==window.sessionStorage,window.sessionStorage.setItem("wc","test"),window.sessionStorage.removeItem("wc"),window.localStorage.setItem("wc","test"),window.localStorage.removeItem("wc")}catch(w){t=!1}function a(){t&&sessionStorage.setItem("wc_cart_created",(new Date).getTime())}function s(e){t&&(localStorage.setItem(o,e),sessionStorage.setItem(o,e))}var e={url:wc_cart_fragments_params.wc_ajax_url.toString().replace("%%endpoint%%","get_refreshed_fragments"),type:"POST",data:{time:(new Date).getTime()},timeout:wc_cart_fragments_params.request_timeout,success:function(e){e&&e.fragments&&(r.each(e.fragments,function(e,t){r(e).replaceWith(t)}),t&&(sessionStorage.setItem(wc_cart_fragments_params.fragment_name,JSON.stringify(e.fragments)),s(e.cart_hash),e.cart_hash&&a()),r(document.body).trigger("wc_fragments_refreshed"))},error:function(){r(document.body).trigger("wc_fragments_ajax_error")}};function n(){r.ajax(e)}if(t){var i=null;r(document.body).on("wc_fragment_refresh updated_wc_div",function(){n()}),r(document.body).on("added_to_cart removed_from_cart",function(e,t,r){var n=sessionStorage.getItem(o);null!==n&&n!==undefined&&""!==n||a(),sessionStorage.setItem(wc_cart_fragments_params.fragment_name,JSON.stringify(t)),s(r)}),r(document.body).on("wc_fragments_refreshed",function(){clearTimeout(i),i=setTimeout(n,864e5)}),r(window).on("storage onstorage",function(e){o===e.originalEvent.key&&localStorage.getItem(o)!==sessionStorage.getItem(o)&&n()}),r(window).on("pageshow",function(e){e.originalEvent.persisted&&(r(".widget_shopping_cart_content").empty(),r(document.body).trigger("wc_fragment_refresh"))});try{var c=JSON.parse(sessionStorage.getItem(wc_cart_fragments_params.fragment_name)),_=sessionStorage.getItem(o),g=Cookies.get("woocommerce_cart_hash"),m=sessionStorage.getItem("wc_cart_created");if(null!==_&&_!==undefined&&""!==_||(_=""),null!==g&&g!==undefined&&""!==g||(g=""),_&&(null===m||m===undefined||""===m))throw"No cart_created";if(m){var d=+m+864e5,m=(new Date).getTime();if(d<m)throw"Fragment expired";i=setTimeout(n,d-m)}if(!c||!c["div.widget_shopping_cart_content"]||_!==g)throw"No fragment";r.each(c,function(e,t){r(e).replaceWith(t)}),r(document.body).trigger("wc_fragments_loaded")}catch(w){n()}}else n();0<Cookies.get("woocommerce_items_in_cart")?r(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").show():r(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").hide(),r(document.body).on("adding_to_cart",function(){r(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").show()}),"undefined"!=typeof wp&&wp.customize&&wp.customize.selectiveRefresh&&wp.customize.widgetsPreview&&wp.customize.widgetsPreview.WidgetPartial&&wp.customize.selectiveRefresh.bind("partial-content-rendered",function(){n()})});

620
assets/js/frontend/cart.js Normal file
View File

@ -0,0 +1,620 @@
/* global wc_cart_params */
jQuery( function( $ ) {
// wc_cart_params is required to continue, ensure the object exists
if ( typeof wc_cart_params === 'undefined' ) {
return false;
}
// Utility functions for the file.
/**
* Gets a url for a given AJAX endpoint.
*
* @param {String} endpoint The AJAX Endpoint
* @return {String} The URL to use for the request
*/
var get_url = function( endpoint ) {
return wc_cart_params.wc_ajax_url.toString().replace(
'%%endpoint%%',
endpoint
);
};
/**
* Check if a node is blocked for processing.
*
* @param {JQuery Object} $node
* @return {bool} True if the DOM Element is UI Blocked, false if not.
*/
var is_blocked = function( $node ) {
return $node.is( '.processing' ) || $node.parents( '.processing' ).length;
};
/**
* Block a node visually for processing.
*
* @param {JQuery Object} $node
*/
var block = function( $node ) {
if ( ! is_blocked( $node ) ) {
$node.addClass( 'processing' ).block( {
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
} );
}
};
/**
* Unblock a node after processing is complete.
*
* @param {JQuery Object} $node
*/
var unblock = function( $node ) {
$node.removeClass( 'processing' ).unblock();
};
/**
* Removes duplicate notices.
*
* @param {JQuery Object} notices
*/
var remove_duplicate_notices = function( notices ) {
var seen = [];
var new_notices = notices;
notices.each( function( index ) {
var text = $( this ).text();
if ( 'undefined' === typeof seen[ text ] ) {
seen[ text ] = true;
} else {
new_notices.splice( index, 1 );
}
} );
return new_notices;
};
/**
* Update the .woocommerce div with a string of html.
*
* @param {String} html_str The HTML string with which to replace the div.
* @param {bool} preserve_notices Should notices be kept? False by default.
*/
var update_wc_div = function( html_str, preserve_notices ) {
var $html = $.parseHTML( html_str );
var $new_form = $( '.woocommerce-cart-form', $html );
var $new_totals = $( '.cart_totals', $html );
var $notices = remove_duplicate_notices( $( '.woocommerce-error, .woocommerce-message, .woocommerce-info', $html ) );
// No form, cannot do this.
if ( $( '.woocommerce-cart-form' ).length === 0 ) {
window.location.reload();
return;
}
// Remove errors
if ( ! preserve_notices ) {
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
}
if ( $new_form.length === 0 ) {
// If the checkout is also displayed on this page, trigger reload instead.
if ( $( '.woocommerce-checkout' ).length ) {
window.location.reload();
return;
}
// No items to display now! Replace all cart content.
var $cart_html = $( '.cart-empty', $html ).closest( '.woocommerce' );
$( '.woocommerce-cart-form__contents' ).closest( '.woocommerce' ).replaceWith( $cart_html );
// Display errors
if ( $notices.length > 0 ) {
show_notice( $notices );
}
// Notify plugins that the cart was emptied.
$( document.body ).trigger( 'wc_cart_emptied' );
} else {
// If the checkout is also displayed on this page, trigger update event.
if ( $( '.woocommerce-checkout' ).length ) {
$( document.body ).trigger( 'update_checkout' );
}
$( '.woocommerce-cart-form' ).replaceWith( $new_form );
$( '.woocommerce-cart-form' ).find( ':input[name="update_cart"]' ).prop( 'disabled', true ).attr( 'aria-disabled', true );
if ( $notices.length > 0 ) {
show_notice( $notices );
}
update_cart_totals_div( $new_totals );
}
$( document.body ).trigger( 'updated_wc_div' );
};
/**
* Update the .cart_totals div with a string of html.
*
* @param {String} html_str The HTML string with which to replace the div.
*/
var update_cart_totals_div = function( html_str ) {
$( '.cart_totals' ).replaceWith( html_str );
$( document.body ).trigger( 'updated_cart_totals' );
};
/**
* Shows new notices on the page.
*
* @param {Object} The Notice HTML Element in string or object form.
*/
var show_notice = function( html_element, $target ) {
if ( ! $target ) {
$target = $( '.woocommerce-notices-wrapper:first' ) ||
$( '.cart-empty' ).closest( '.woocommerce' ) ||
$( '.woocommerce-cart-form' );
}
$target.prepend( html_element );
};
/**
* Object to handle AJAX calls for cart shipping changes.
*/
var cart_shipping = {
/**
* Initialize event handlers and UI state.
*/
init: function( cart ) {
this.cart = cart;
this.toggle_shipping = this.toggle_shipping.bind( this );
this.shipping_method_selected = this.shipping_method_selected.bind( this );
this.shipping_calculator_submit = this.shipping_calculator_submit.bind( this );
$( document ).on(
'click',
'.shipping-calculator-button',
this.toggle_shipping
);
$( document ).on(
'change',
'select.shipping_method, :input[name^=shipping_method]',
this.shipping_method_selected
);
$( document ).on(
'submit',
'form.woocommerce-shipping-calculator',
this.shipping_calculator_submit
);
$( '.shipping-calculator-form' ).hide();
},
/**
* Toggle Shipping Calculator panel
*/
toggle_shipping: function() {
$( '.shipping-calculator-form' ).slideToggle( 'slow' );
$( 'select.country_to_state, input.country_to_state' ).trigger( 'change' );
$( document.body ).trigger( 'country_to_state_changed' ); // Trigger select2 to load.
return false;
},
/**
* Handles when a shipping method is selected.
*/
shipping_method_selected: function() {
var shipping_methods = {};
// eslint-disable-next-line max-len
$( 'select.shipping_method, :input[name^=shipping_method][type=radio]:checked, :input[name^=shipping_method][type=hidden]' ).each( function() {
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
} );
block( $( 'div.cart_totals' ) );
var data = {
security: wc_cart_params.update_shipping_method_nonce,
shipping_method: shipping_methods
};
$.ajax( {
type: 'post',
url: get_url( 'update_shipping_method' ),
data: data,
dataType: 'html',
success: function( response ) {
update_cart_totals_div( response );
},
complete: function() {
unblock( $( 'div.cart_totals' ) );
$( document.body ).trigger( 'updated_shipping_method' );
}
} );
},
/**
* Handles a shipping calculator form submit.
*
* @param {Object} evt The JQuery event.
*/
shipping_calculator_submit: function( evt ) {
evt.preventDefault();
var $form = $( evt.currentTarget );
block( $( 'div.cart_totals' ) );
block( $form );
// Provide the submit button value because wc-form-handler expects it.
$( '<input />' ).attr( 'type', 'hidden' )
.attr( 'name', 'calc_shipping' )
.attr( 'value', 'x' )
.appendTo( $form );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: function( response ) {
update_wc_div( response );
},
complete: function() {
unblock( $form );
unblock( $( 'div.cart_totals' ) );
}
} );
}
};
/**
* Object to handle cart UI.
*/
var cart = {
/**
* Initialize cart UI events.
*/
init: function() {
this.update_cart_totals = this.update_cart_totals.bind( this );
this.input_keypress = this.input_keypress.bind( this );
this.cart_submit = this.cart_submit.bind( this );
this.submit_click = this.submit_click.bind( this );
this.apply_coupon = this.apply_coupon.bind( this );
this.remove_coupon_clicked = this.remove_coupon_clicked.bind( this );
this.quantity_update = this.quantity_update.bind( this );
this.item_remove_clicked = this.item_remove_clicked.bind( this );
this.item_restore_clicked = this.item_restore_clicked.bind( this );
this.update_cart = this.update_cart.bind( this );
$( document ).on(
'wc_update_cart added_to_cart',
function() { cart.update_cart.apply( cart, [].slice.call( arguments, 1 ) ); } );
$( document ).on(
'click',
'.woocommerce-cart-form :input[type=submit]',
this.submit_click );
$( document ).on(
'keypress',
'.woocommerce-cart-form :input[type=number]',
this.input_keypress );
$( document ).on(
'submit',
'.woocommerce-cart-form',
this.cart_submit );
$( document ).on(
'click',
'a.woocommerce-remove-coupon',
this.remove_coupon_clicked );
$( document ).on(
'click',
'.woocommerce-cart-form .product-remove > a',
this.item_remove_clicked );
$( document ).on(
'click',
'.woocommerce-cart .restore-item',
this.item_restore_clicked );
$( document ).on(
'change input',
'.woocommerce-cart-form .cart_item :input',
this.input_changed );
$( '.woocommerce-cart-form :input[name="update_cart"]' ).prop( 'disabled', true ).attr( 'aria-disabled', true );
},
/**
* After an input is changed, enable the update cart button.
*/
input_changed: function() {
$( '.woocommerce-cart-form :input[name="update_cart"]' ).prop( 'disabled', false ).attr( 'aria-disabled', false );
},
/**
* Update entire cart via ajax.
*/
update_cart: function( preserve_notices ) {
var $form = $( '.woocommerce-cart-form' );
block( $form );
block( $( 'div.cart_totals' ) );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: function( response ) {
update_wc_div( response, preserve_notices );
},
complete: function() {
unblock( $form );
unblock( $( 'div.cart_totals' ) );
$.scroll_to_notices( $( '[role="alert"]' ) );
}
} );
},
/**
* Update the cart after something has changed.
*/
update_cart_totals: function() {
block( $( 'div.cart_totals' ) );
$.ajax( {
url: get_url( 'get_cart_totals' ),
dataType: 'html',
success: function( response ) {
update_cart_totals_div( response );
},
complete: function() {
unblock( $( 'div.cart_totals' ) );
}
} );
},
/**
* Handle the <ENTER> key for quantity fields.
*
* @param {Object} evt The JQuery event
*
* For IE, if you hit enter on a quantity field, it makes the
* document.activeElement the first submit button it finds.
* For us, that is the Apply Coupon button. This is required
* to catch the event before that happens.
*/
input_keypress: function( evt ) {
// Catch the enter key and don't let it submit the form.
if ( 13 === evt.keyCode ) {
var $form = $( evt.currentTarget ).parents( 'form' );
try {
// If there are no validation errors, handle the submit.
if ( $form[0].checkValidity() ) {
evt.preventDefault();
this.cart_submit( evt );
}
} catch( err ) {
evt.preventDefault();
this.cart_submit( evt );
}
}
},
/**
* Handle cart form submit and route to correct logic.
*
* @param {Object} evt The JQuery event
*/
cart_submit: function( evt ) {
var $submit = $( document.activeElement ),
$clicked = $( ':input[type=submit][clicked=true]' ),
$form = $( evt.currentTarget );
// For submit events, currentTarget is form.
// For keypress events, currentTarget is input.
if ( ! $form.is( 'form' ) ) {
$form = $( evt.currentTarget ).parents( 'form' );
}
if ( 0 === $form.find( '.woocommerce-cart-form__contents' ).length ) {
return;
}
if ( is_blocked( $form ) ) {
return false;
}
if ( $clicked.is( ':input[name="update_cart"]' ) || $submit.is( 'input.qty' ) ) {
evt.preventDefault();
this.quantity_update( $form );
} else if ( $clicked.is( ':input[name="apply_coupon"]' ) || $submit.is( '#coupon_code' ) ) {
evt.preventDefault();
this.apply_coupon( $form );
}
},
/**
* Special handling to identify which submit button was clicked.
*
* @param {Object} evt The JQuery event
*/
submit_click: function( evt ) {
$( ':input[type=submit]', $( evt.target ).parents( 'form' ) ).removeAttr( 'clicked' );
$( evt.target ).attr( 'clicked', 'true' );
},
/**
* Apply Coupon code
*
* @param {JQuery Object} $form The cart form.
*/
apply_coupon: function( $form ) {
block( $form );
var cart = this;
var $text_field = $( '#coupon_code' );
var coupon_code = $text_field.val();
var data = {
security: wc_cart_params.apply_coupon_nonce,
coupon_code: coupon_code
};
$.ajax( {
type: 'POST',
url: get_url( 'apply_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
show_notice( response );
$( document.body ).trigger( 'applied_coupon', [ coupon_code ] );
},
complete: function() {
unblock( $form );
$text_field.val( '' );
cart.update_cart( true );
}
} );
},
/**
* Handle when a remove coupon link is clicked.
*
* @param {Object} evt The JQuery event
*/
remove_coupon_clicked: function( evt ) {
evt.preventDefault();
var cart = this;
var $wrapper = $( evt.currentTarget ).closest( '.cart_totals' );
var coupon = $( evt.currentTarget ).attr( 'data-coupon' );
block( $wrapper );
var data = {
security: wc_cart_params.remove_coupon_nonce,
coupon: coupon
};
$.ajax( {
type: 'POST',
url: get_url( 'remove_coupon' ),
data: data,
dataType: 'html',
success: function( response ) {
$( '.woocommerce-error, .woocommerce-message, .woocommerce-info' ).remove();
show_notice( response );
$( document.body ).trigger( 'removed_coupon', [ coupon ] );
unblock( $wrapper );
},
complete: function() {
cart.update_cart( true );
}
} );
},
/**
* Handle a cart Quantity Update
*
* @param {JQuery Object} $form The cart form.
*/
quantity_update: function( $form ) {
block( $form );
block( $( 'div.cart_totals' ) );
// Provide the submit button value because wc-form-handler expects it.
$( '<input />' ).attr( 'type', 'hidden' )
.attr( 'name', 'update_cart' )
.attr( 'value', 'Update Cart' )
.appendTo( $form );
// Make call to actual form post URL.
$.ajax( {
type: $form.attr( 'method' ),
url: $form.attr( 'action' ),
data: $form.serialize(),
dataType: 'html',
success: function( response ) {
update_wc_div( response );
},
complete: function() {
unblock( $form );
unblock( $( 'div.cart_totals' ) );
$.scroll_to_notices( $( '[role="alert"]' ) );
}
} );
},
/**
* Handle when a remove item link is clicked.
*
* @param {Object} evt The JQuery event
*/
item_remove_clicked: function( evt ) {
evt.preventDefault();
var $a = $( evt.currentTarget );
var $form = $a.parents( 'form' );
block( $form );
block( $( 'div.cart_totals' ) );
$.ajax( {
type: 'GET',
url: $a.attr( 'href' ),
dataType: 'html',
success: function( response ) {
update_wc_div( response );
},
complete: function() {
unblock( $form );
unblock( $( 'div.cart_totals' ) );
$.scroll_to_notices( $( '[role="alert"]' ) );
}
} );
},
/**
* Handle when a restore item link is clicked.
*
* @param {Object} evt The JQuery event
*/
item_restore_clicked: function( evt ) {
evt.preventDefault();
var $a = $( evt.currentTarget );
var $form = $( 'form.woocommerce-cart-form' );
block( $form );
block( $( 'div.cart_totals' ) );
$.ajax( {
type: 'GET',
url: $a.attr( 'href' ),
dataType: 'html',
success: function( response ) {
update_wc_div( response );
},
complete: function() {
unblock( $form );
unblock( $( 'div.cart_totals' ) );
}
} );
}
};
cart_shipping.init( cart );
cart.init();
} );

1
assets/js/frontend/cart.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,727 @@
/* global wc_checkout_params */
jQuery( function( $ ) {
// wc_checkout_params is required to continue, ensure the object exists
if ( typeof wc_checkout_params === 'undefined' ) {
return false;
}
$.blockUI.defaults.overlayCSS.cursor = 'default';
var wc_checkout_form = {
updateTimer: false,
dirtyInput: false,
selectedPaymentMethod: false,
xhr: false,
$order_review: $( '#order_review' ),
$checkout_form: $( 'form.checkout' ),
init: function() {
$( document.body ).on( 'update_checkout', this.update_checkout );
$( document.body ).on( 'init_checkout', this.init_checkout );
// Payment methods
this.$checkout_form.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
if ( $( document.body ).hasClass( 'woocommerce-order-pay' ) ) {
this.$order_review.on( 'click', 'input[name="payment_method"]', this.payment_method_selected );
this.$order_review.on( 'submit', this.submitOrder );
this.$order_review.attr( 'novalidate', 'novalidate' );
}
// Prevent HTML5 validation which can conflict.
this.$checkout_form.attr( 'novalidate', 'novalidate' );
// Form submission
this.$checkout_form.on( 'submit', this.submit );
// Inline validation
this.$checkout_form.on( 'input validate change', '.input-text, select, input:checkbox', this.validate_field );
// Manual trigger
this.$checkout_form.on( 'update', this.trigger_update_checkout );
// Inputs/selects which update totals
this.$checkout_form.on( 'change', 'select.shipping_method, input[name^="shipping_method"], #ship-to-different-address input, .update_totals_on_change select, .update_totals_on_change input[type="radio"], .update_totals_on_change input[type="checkbox"]', this.trigger_update_checkout ); // eslint-disable-line max-len
this.$checkout_form.on( 'change', '.address-field select', this.input_changed );
this.$checkout_form.on( 'change', '.address-field input.input-text, .update_totals_on_change input.input-text', this.maybe_input_changed ); // eslint-disable-line max-len
this.$checkout_form.on( 'keydown', '.address-field input.input-text, .update_totals_on_change input.input-text', this.queue_update_checkout ); // eslint-disable-line max-len
// Address fields
this.$checkout_form.on( 'change', '#ship-to-different-address input', this.ship_to_different_address );
// Trigger events
this.$checkout_form.find( '#ship-to-different-address input' ).trigger( 'change' );
this.init_payment_methods();
// Update on page load
if ( wc_checkout_params.is_checkout === '1' ) {
$( document.body ).trigger( 'init_checkout' );
}
if ( wc_checkout_params.option_guest_checkout === 'yes' ) {
$( 'input#createaccount' ).on( 'change', this.toggle_create_account ).trigger( 'change' );
}
},
init_payment_methods: function() {
var $payment_methods = $( '.woocommerce-checkout' ).find( 'input[name="payment_method"]' );
// If there is one method, we can hide the radio input
if ( 1 === $payment_methods.length ) {
$payment_methods.eq(0).hide();
}
// If there was a previously selected method, check that one.
if ( wc_checkout_form.selectedPaymentMethod ) {
$( '#' + wc_checkout_form.selectedPaymentMethod ).prop( 'checked', true );
}
// If there are none selected, select the first.
if ( 0 === $payment_methods.filter( ':checked' ).length ) {
$payment_methods.eq(0).prop( 'checked', true );
}
// Get name of new selected method.
var checkedPaymentMethod = $payment_methods.filter( ':checked' ).eq(0).prop( 'id' );
if ( $payment_methods.length > 1 ) {
// Hide open descriptions.
$( 'div.payment_box:not(".' + checkedPaymentMethod + '")' ).filter( ':visible' ).slideUp( 0 );
}
// Trigger click event for selected method
$payment_methods.filter( ':checked' ).eq(0).trigger( 'click' );
},
get_payment_method: function() {
return wc_checkout_form.$checkout_form.find( 'input[name="payment_method"]:checked' ).val();
},
payment_method_selected: function( e ) {
e.stopPropagation();
if ( $( '.payment_methods input.input-radio' ).length > 1 ) {
var target_payment_box = $( 'div.payment_box.' + $( this ).attr( 'ID' ) ),
is_checked = $( this ).is( ':checked' );
if ( is_checked && ! target_payment_box.is( ':visible' ) ) {
$( 'div.payment_box' ).filter( ':visible' ).slideUp( 230 );
if ( is_checked ) {
target_payment_box.slideDown( 230 );
}
}
} else {
$( 'div.payment_box' ).show();
}
if ( $( this ).data( 'order_button_text' ) ) {
$( '#place_order' ).text( $( this ).data( 'order_button_text' ) );
} else {
$( '#place_order' ).text( $( '#place_order' ).data( 'value' ) );
}
var selectedPaymentMethod = $( '.woocommerce-checkout input[name="payment_method"]:checked' ).attr( 'id' );
if ( selectedPaymentMethod !== wc_checkout_form.selectedPaymentMethod ) {
$( document.body ).trigger( 'payment_method_selected' );
}
wc_checkout_form.selectedPaymentMethod = selectedPaymentMethod;
},
toggle_create_account: function() {
$( 'div.create-account' ).hide();
if ( $( this ).is( ':checked' ) ) {
// Ensure password is not pre-populated.
$( '#account_password' ).val( '' ).trigger( 'change' );
$( 'div.create-account' ).slideDown();
}
},
init_checkout: function() {
$( document.body ).trigger( 'update_checkout' );
},
maybe_input_changed: function( e ) {
if ( wc_checkout_form.dirtyInput ) {
wc_checkout_form.input_changed( e );
}
},
input_changed: function( e ) {
wc_checkout_form.dirtyInput = e.target;
wc_checkout_form.maybe_update_checkout();
},
queue_update_checkout: function( e ) {
var code = e.keyCode || e.which || 0;
if ( code === 9 ) {
return true;
}
wc_checkout_form.dirtyInput = this;
wc_checkout_form.reset_update_checkout_timer();
wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.maybe_update_checkout, '1000' );
},
trigger_update_checkout: function() {
wc_checkout_form.reset_update_checkout_timer();
wc_checkout_form.dirtyInput = false;
$( document.body ).trigger( 'update_checkout' );
},
maybe_update_checkout: function() {
var update_totals = true;
if ( $( wc_checkout_form.dirtyInput ).length ) {
var $required_inputs = $( wc_checkout_form.dirtyInput ).closest( 'div' ).find( '.address-field.validate-required' );
if ( $required_inputs.length ) {
$required_inputs.each( function() {
if ( $( this ).find( 'input.input-text' ).val() === '' ) {
update_totals = false;
}
});
}
}
if ( update_totals ) {
wc_checkout_form.trigger_update_checkout();
}
},
ship_to_different_address: function() {
$( 'div.shipping_address' ).hide();
if ( $( this ).is( ':checked' ) ) {
$( 'div.shipping_address' ).slideDown();
}
},
reset_update_checkout_timer: function() {
clearTimeout( wc_checkout_form.updateTimer );
},
is_valid_json: function( raw_json ) {
try {
var json = JSON.parse( raw_json );
return ( json && 'object' === typeof json );
} catch ( e ) {
return false;
}
},
validate_field: function( e ) {
var $this = $( this ),
$parent = $this.closest( '.form-row' ),
validated = true,
validate_required = $parent.is( '.validate-required' ),
validate_email = $parent.is( '.validate-email' ),
validate_phone = $parent.is( '.validate-phone' ),
pattern = '',
event_type = e.type;
if ( 'input' === event_type ) {
$parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-invalid-phone woocommerce-validated' ); // eslint-disable-line max-len
}
if ( 'validate' === event_type || 'change' === event_type ) {
if ( validate_required ) {
if ( 'checkbox' === $this.attr( 'type' ) && ! $this.is( ':checked' ) ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
validated = false;
} else if ( $this.val() === '' ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-required-field' );
validated = false;
}
}
if ( validate_email ) {
if ( $this.val() ) {
/* https://stackoverflow.com/questions/2855865/jquery-validate-e-mail-address-regex */
pattern = new RegExp( /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[0-9a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i ); // eslint-disable-line max-len
if ( ! pattern.test( $this.val() ) ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-email woocommerce-invalid-phone' ); // eslint-disable-line max-len
validated = false;
}
}
}
if ( validate_phone ) {
pattern = new RegExp( /[\s\#0-9_\-\+\/\(\)\.]/g );
if ( 0 < $this.val().replace( pattern, '' ).length ) {
$parent.removeClass( 'woocommerce-validated' ).addClass( 'woocommerce-invalid woocommerce-invalid-phone' );
validated = false;
}
}
if ( validated ) {
$parent.removeClass( 'woocommerce-invalid woocommerce-invalid-required-field woocommerce-invalid-email woocommerce-invalid-phone' ).addClass( 'woocommerce-validated' ); // eslint-disable-line max-len
}
}
},
update_checkout: function( event, args ) {
// Small timeout to prevent multiple requests when several fields update at the same time
wc_checkout_form.reset_update_checkout_timer();
wc_checkout_form.updateTimer = setTimeout( wc_checkout_form.update_checkout_action, '5', args );
},
update_checkout_action: function( args ) {
if ( wc_checkout_form.xhr ) {
wc_checkout_form.xhr.abort();
}
if ( $( 'form.checkout' ).length === 0 ) {
return;
}
args = typeof args !== 'undefined' ? args : {
update_shipping_method: true
};
var country = $( '#billing_country' ).val(),
state = $( '#billing_state' ).val(),
postcode = $( ':input#billing_postcode' ).val(),
city = $( '#billing_city' ).val(),
address = $( ':input#billing_address_1' ).val(),
address_2 = $( ':input#billing_address_2' ).val(),
s_country = country,
s_state = state,
s_postcode = postcode,
s_city = city,
s_address = address,
s_address_2 = address_2,
$required_inputs = $( wc_checkout_form.$checkout_form ).find( '.address-field.validate-required:visible' ),
has_full_address = true;
if ( $required_inputs.length ) {
$required_inputs.each( function() {
if ( $( this ).find( ':input' ).val() === '' ) {
has_full_address = false;
}
});
}
if ( $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ) ) {
s_country = $( '#shipping_country' ).val();
s_state = $( '#shipping_state' ).val();
s_postcode = $( ':input#shipping_postcode' ).val();
s_city = $( '#shipping_city' ).val();
s_address = $( ':input#shipping_address_1' ).val();
s_address_2 = $( ':input#shipping_address_2' ).val();
}
var data = {
security : wc_checkout_params.update_order_review_nonce,
payment_method : wc_checkout_form.get_payment_method(),
country : country,
state : state,
postcode : postcode,
city : city,
address : address,
address_2 : address_2,
s_country : s_country,
s_state : s_state,
s_postcode : s_postcode,
s_city : s_city,
s_address : s_address,
s_address_2 : s_address_2,
has_full_address: has_full_address,
post_data : $( 'form.checkout' ).serialize()
};
if ( false !== args.update_shipping_method ) {
var shipping_methods = {};
// eslint-disable-next-line max-len
$( 'select.shipping_method, input[name^="shipping_method"][type="radio"]:checked, input[name^="shipping_method"][type="hidden"]' ).each( function() {
shipping_methods[ $( this ).data( 'index' ) ] = $( this ).val();
} );
data.shipping_method = shipping_methods;
}
$( '.woocommerce-checkout-payment, .woocommerce-checkout-review-order-table' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
wc_checkout_form.xhr = $.ajax({
type: 'POST',
url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'update_order_review' ),
data: data,
success: function( data ) {
// Reload the page if requested
if ( data && true === data.reload ) {
window.location.reload();
return;
}
// Remove any notices added previously
$( '.woocommerce-NoticeGroup-updateOrderReview' ).remove();
var termsCheckBoxChecked = $( '#terms' ).prop( 'checked' );
// Save payment details to a temporary object
var paymentDetails = {};
$( '.payment_box :input' ).each( function() {
var ID = $( this ).attr( 'id' );
if ( ID ) {
if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
paymentDetails[ ID ] = $( this ).prop( 'checked' );
} else {
paymentDetails[ ID ] = $( this ).val();
}
}
});
// Always update the fragments
if ( data && data.fragments ) {
$.each( data.fragments, function ( key, value ) {
if ( ! wc_checkout_form.fragments || wc_checkout_form.fragments[ key ] !== value ) {
$( key ).replaceWith( value );
}
$( key ).unblock();
} );
wc_checkout_form.fragments = data.fragments;
}
// Recheck the terms and conditions box, if needed
if ( termsCheckBoxChecked ) {
$( '#terms' ).prop( 'checked', true );
}
// Fill in the payment details if possible without overwriting data if set.
if ( ! $.isEmptyObject( paymentDetails ) ) {
$( '.payment_box :input' ).each( function() {
var ID = $( this ).attr( 'id' );
if ( ID ) {
if ( $.inArray( $( this ).attr( 'type' ), [ 'checkbox', 'radio' ] ) !== -1 ) {
$( this ).prop( 'checked', paymentDetails[ ID ] ).trigger( 'change' );
} else if ( $.inArray( $( this ).attr( 'type' ), [ 'select' ] ) !== -1 ) {
$( this ).val( paymentDetails[ ID ] ).trigger( 'change' );
} else if ( null !== $( this ).val() && 0 === $( this ).val().length ) {
$( this ).val( paymentDetails[ ID ] ).trigger( 'change' );
}
}
});
}
// Check for error
if ( data && 'failure' === data.result ) {
var $form = $( 'form.checkout' );
// Remove notices from all sources
$( '.woocommerce-error, .woocommerce-message' ).remove();
// Add new errors returned by this event
if ( data.messages ) {
$form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-updateOrderReview">' + data.messages + '</div>' ); // eslint-disable-line max-len
} else {
$form.prepend( data );
}
// Lose focus for all fields
$form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).trigger( 'blur' );
wc_checkout_form.scroll_to_notices();
}
// Re-init methods
wc_checkout_form.init_payment_methods();
// Fire updated_checkout event.
$( document.body ).trigger( 'updated_checkout', [ data ] );
}
});
},
handleUnloadEvent: function( e ) {
// Modern browsers have their own standard generic messages that they will display.
// Confirm, alert, prompt or custom message are not allowed during the unload event
// Browsers will display their own standard messages
// Check if the browser is Internet Explorer
if((navigator.userAgent.indexOf('MSIE') !== -1 ) || (!!document.documentMode)) {
// IE handles unload events differently than modern browsers
e.preventDefault();
return undefined;
}
return true;
},
attachUnloadEventsOnSubmit: function() {
$( window ).on('beforeunload', this.handleUnloadEvent);
},
detachUnloadEventsOnSubmit: function() {
$( window ).off('beforeunload', this.handleUnloadEvent);
},
blockOnSubmit: function( $form ) {
var isBlocked = $form.data( 'blockUI.isBlocked' );
if ( 1 !== isBlocked ) {
$form.block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
}
},
submitOrder: function() {
wc_checkout_form.blockOnSubmit( $( this ) );
},
submit: function() {
wc_checkout_form.reset_update_checkout_timer();
var $form = $( this );
if ( $form.is( '.processing' ) ) {
return false;
}
// Trigger a handler to let gateways manipulate the checkout if needed
// eslint-disable-next-line max-len
if ( $form.triggerHandler( 'checkout_place_order' ) !== false && $form.triggerHandler( 'checkout_place_order_' + wc_checkout_form.get_payment_method() ) !== false ) {
$form.addClass( 'processing' );
wc_checkout_form.blockOnSubmit( $form );
// Attach event to block reloading the page when the form has been submitted
wc_checkout_form.attachUnloadEventsOnSubmit();
// ajaxSetup is global, but we use it to ensure JSON is valid once returned.
$.ajaxSetup( {
dataFilter: function( raw_response, dataType ) {
// We only want to work with JSON
if ( 'json' !== dataType ) {
return raw_response;
}
if ( wc_checkout_form.is_valid_json( raw_response ) ) {
return raw_response;
} else {
// Attempt to fix the malformed JSON
var maybe_valid_json = raw_response.match( /{"result.*}/ );
if ( null === maybe_valid_json ) {
console.log( 'Unable to fix malformed JSON' );
} else if ( wc_checkout_form.is_valid_json( maybe_valid_json[0] ) ) {
console.log( 'Fixed malformed JSON. Original:' );
console.log( raw_response );
raw_response = maybe_valid_json[0];
} else {
console.log( 'Unable to fix malformed JSON' );
}
}
return raw_response;
}
} );
$.ajax({
type: 'POST',
url: wc_checkout_params.checkout_url,
data: $form.serialize(),
dataType: 'json',
success: function( result ) {
// Detach the unload handler that prevents a reload / redirect
wc_checkout_form.detachUnloadEventsOnSubmit();
try {
if ( 'success' === result.result && $form.triggerHandler( 'checkout_place_order_success', result ) !== false ) {
if ( -1 === result.redirect.indexOf( 'https://' ) || -1 === result.redirect.indexOf( 'http://' ) ) {
window.location = result.redirect;
} else {
window.location = decodeURI( result.redirect );
}
} else if ( 'failure' === result.result ) {
throw 'Result failure';
} else {
throw 'Invalid response';
}
} catch( err ) {
// Reload page
if ( true === result.reload ) {
window.location.reload();
return;
}
// Trigger update in case we need a fresh nonce
if ( true === result.refresh ) {
$( document.body ).trigger( 'update_checkout' );
}
// Add new errors
if ( result.messages ) {
wc_checkout_form.submit_error( result.messages );
} else {
wc_checkout_form.submit_error( '<div class="woocommerce-error">' + wc_checkout_params.i18n_checkout_error + '</div>' ); // eslint-disable-line max-len
}
}
},
error: function( jqXHR, textStatus, errorThrown ) {
// Detach the unload handler that prevents a reload / redirect
wc_checkout_form.detachUnloadEventsOnSubmit();
wc_checkout_form.submit_error( '<div class="woocommerce-error">' + errorThrown + '</div>' );
}
});
}
return false;
},
submit_error: function( error_message ) {
$( '.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message' ).remove();
wc_checkout_form.$checkout_form.prepend( '<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' + error_message + '</div>' ); // eslint-disable-line max-len
wc_checkout_form.$checkout_form.removeClass( 'processing' ).unblock();
wc_checkout_form.$checkout_form.find( '.input-text, select, input:checkbox' ).trigger( 'validate' ).trigger( 'blur' );
wc_checkout_form.scroll_to_notices();
$( document.body ).trigger( 'checkout_error' , [ error_message ] );
},
scroll_to_notices: function() {
var scrollElement = $( '.woocommerce-NoticeGroup-updateOrderReview, .woocommerce-NoticeGroup-checkout' );
if ( ! scrollElement.length ) {
scrollElement = $( '.form.checkout' );
}
$.scroll_to_notices( scrollElement );
}
};
var wc_checkout_coupons = {
init: function() {
$( document.body ).on( 'click', 'a.showcoupon', this.show_coupon_form );
$( document.body ).on( 'click', '.woocommerce-remove-coupon', this.remove_coupon );
$( 'form.checkout_coupon' ).hide().on( 'submit', this.submit );
},
show_coupon_form: function() {
$( '.checkout_coupon' ).slideToggle( 400, function() {
$( '.checkout_coupon' ).find( ':input:eq(0)' ).trigger( 'focus' );
});
return false;
},
submit: function() {
var $form = $( this );
if ( $form.is( '.processing' ) ) {
return false;
}
$form.addClass( 'processing' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var data = {
security: wc_checkout_params.apply_coupon_nonce,
coupon_code: $form.find( 'input[name="coupon_code"]' ).val()
};
$.ajax({
type: 'POST',
url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'apply_coupon' ),
data: data,
success: function( code ) {
$( '.woocommerce-error, .woocommerce-message' ).remove();
$form.removeClass( 'processing' ).unblock();
if ( code ) {
$form.before( code );
$form.slideUp();
$( document.body ).trigger( 'applied_coupon_in_checkout', [ data.coupon_code ] );
$( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
}
},
dataType: 'html'
});
return false;
},
remove_coupon: function( e ) {
e.preventDefault();
var container = $( this ).parents( '.woocommerce-checkout-review-order' ),
coupon = $( this ).data( 'coupon' );
container.addClass( 'processing' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var data = {
security: wc_checkout_params.remove_coupon_nonce,
coupon: coupon
};
$.ajax({
type: 'POST',
url: wc_checkout_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_coupon' ),
data: data,
success: function( code ) {
$( '.woocommerce-error, .woocommerce-message' ).remove();
container.removeClass( 'processing' ).unblock();
if ( code ) {
$( 'form.woocommerce-checkout' ).before( code );
$( document.body ).trigger( 'removed_coupon_in_checkout', [ data.coupon_code ] );
$( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
// Remove coupon code from coupon field
$( 'form.checkout_coupon' ).find( 'input[name="coupon_code"]' ).val( '' );
}
},
error: function ( jqXHR ) {
if ( wc_checkout_params.debug_mode ) {
/* jshint devel: true */
console.log( jqXHR.responseText );
}
},
dataType: 'html'
});
}
};
var wc_checkout_login_form = {
init: function() {
$( document.body ).on( 'click', 'a.showlogin', this.show_login_form );
},
show_login_form: function() {
$( 'form.login, form.woocommerce-form--login' ).slideToggle();
return false;
}
};
var wc_terms_toggle = {
init: function() {
$( document.body ).on( 'click', 'a.woocommerce-terms-and-conditions-link', this.toggle_terms );
},
toggle_terms: function() {
if ( $( '.woocommerce-terms-and-conditions' ).length ) {
$( '.woocommerce-terms-and-conditions' ).slideToggle( function() {
var link_toggle = $( '.woocommerce-terms-and-conditions-link' );
if ( $( '.woocommerce-terms-and-conditions' ).is( ':visible' ) ) {
link_toggle.addClass( 'woocommerce-terms-and-conditions-link--open' );
link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--closed' );
} else {
link_toggle.removeClass( 'woocommerce-terms-and-conditions-link--open' );
link_toggle.addClass( 'woocommerce-terms-and-conditions-link--closed' );
}
} );
return false;
}
}
};
wc_checkout_form.init();
wc_checkout_coupons.init();
wc_checkout_login_form.init();
wc_terms_toggle.init();
});

1
assets/js/frontend/checkout.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,181 @@
/*global wc_country_select_params */
jQuery( function( $ ) {
// wc_country_select_params is required to continue, ensure the object exists
if ( typeof wc_country_select_params === 'undefined' ) {
return false;
}
// Select2 Enhancement if it exists
if ( $().selectWoo ) {
var getEnhancedSelectFormatString = function() {
return {
'language': {
errorLoading: function() {
// Workaround for https://github.com/select2/select2/issues/4355 instead of i18n_ajax_error.
return wc_country_select_params.i18n_searching;
},
inputTooLong: function( args ) {
var overChars = args.input.length - args.maximum;
if ( 1 === overChars ) {
return wc_country_select_params.i18n_input_too_long_1;
}
return wc_country_select_params.i18n_input_too_long_n.replace( '%qty%', overChars );
},
inputTooShort: function( args ) {
var remainingChars = args.minimum - args.input.length;
if ( 1 === remainingChars ) {
return wc_country_select_params.i18n_input_too_short_1;
}
return wc_country_select_params.i18n_input_too_short_n.replace( '%qty%', remainingChars );
},
loadingMore: function() {
return wc_country_select_params.i18n_load_more;
},
maximumSelected: function( args ) {
if ( args.maximum === 1 ) {
return wc_country_select_params.i18n_selection_too_long_1;
}
return wc_country_select_params.i18n_selection_too_long_n.replace( '%qty%', args.maximum );
},
noResults: function() {
return wc_country_select_params.i18n_no_matches;
},
searching: function() {
return wc_country_select_params.i18n_searching;
}
}
};
};
var wc_country_select_select2 = function() {
$( 'select.country_select:visible, select.state_select:visible' ).each( function() {
var $this = $( this );
var select2_args = $.extend({
placeholder: $this.attr( 'data-placeholder' ) || $this.attr( 'placeholder' ) || '',
label: $this.attr( 'data-label' ) || null,
width: '100%'
}, getEnhancedSelectFormatString() );
$( this )
.on( 'select2:select', function() {
$( this ).trigger( 'focus' ); // Maintain focus after select https://github.com/select2/select2/issues/4384
} )
.selectWoo( select2_args );
});
};
wc_country_select_select2();
$( document.body ).on( 'country_to_state_changed', function() {
wc_country_select_select2();
});
}
/* State/Country select boxes */
var states_json = wc_country_select_params.countries.replace( /&quot;/g, '"' ),
states = JSON.parse( states_json ),
wrapper_selectors = '.woocommerce-billing-fields,' +
'.woocommerce-shipping-fields,' +
'.woocommerce-address-fields,' +
'.woocommerce-shipping-calculator';
$( document.body ).on( 'change refresh', 'select.country_to_state, input.country_to_state', function() {
// Grab wrapping element to target only stateboxes in same 'group'
var $wrapper = $( this ).closest( wrapper_selectors );
if ( ! $wrapper.length ) {
$wrapper = $( this ).closest('.form-row').parent();
}
var country = $( this ).val(),
$statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' ),
$parent = $statebox.closest( '.form-row' ),
input_name = $statebox.attr( 'name' ),
input_id = $statebox.attr('id'),
input_classes = $statebox.attr('data-input-classes'),
value = $statebox.val(),
placeholder = $statebox.attr( 'placeholder' ) || $statebox.attr( 'data-placeholder' ) || '',
$newstate;
if ( states[ country ] ) {
if ( $.isEmptyObject( states[ country ] ) ) {
$newstate = $( '<input type="hidden" />' )
.prop( 'id', input_id )
.prop( 'name', input_name )
.prop( 'placeholder', placeholder )
.attr( 'data-input-classes', input_classes )
.addClass( 'hidden ' + input_classes );
$parent.hide().find( '.select2-container' ).remove();
$statebox.replaceWith( $newstate );
$( document.body ).trigger( 'country_to_state_changed', [ country, $wrapper ] );
} else {
var state = states[ country ],
$defaultOption = $( '<option value=""></option>' ).text( wc_country_select_params.i18n_select_state_text );
if ( ! placeholder ) {
placeholder = wc_country_select_params.i18n_select_state_text;
}
$parent.show();
if ( $statebox.is( 'input' ) ) {
$newstate = $( '<select></select>' )
.prop( 'id', input_id )
.prop( 'name', input_name )
.data( 'placeholder', placeholder )
.attr( 'data-input-classes', input_classes )
.addClass( 'state_select ' + input_classes );
$statebox.replaceWith( $newstate );
$statebox = $wrapper.find( '#billing_state, #shipping_state, #calc_shipping_state' );
}
$statebox.empty().append( $defaultOption );
$.each( state, function( index ) {
var $option = $( '<option></option>' )
.prop( 'value', index )
.text( state[ index ] );
$statebox.append( $option );
} );
$statebox.val( value ).trigger( 'change' );
$( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] );
}
} else {
if ( $statebox.is( 'select, input[type="hidden"]' ) ) {
$newstate = $( '<input type="text" />' )
.prop( 'id', input_id )
.prop( 'name', input_name )
.prop('placeholder', placeholder)
.attr('data-input-classes', input_classes )
.addClass( 'input-text ' + input_classes );
$parent.show().find( '.select2-container' ).remove();
$statebox.replaceWith( $newstate );
$( document.body ).trigger( 'country_to_state_changed', [country, $wrapper ] );
}
}
$( document.body ).trigger( 'country_to_state_changing', [country, $wrapper ] );
});
$( document.body ).on( 'wc_address_i18n_ready', function() {
// Init country selects with their default value once the page loads.
$( wrapper_selectors ).each( function() {
var $country_input = $( this ).find( '#billing_country, #shipping_country, #calc_shipping_country' );
if ( 0 === $country_input.length || 0 === $country_input.val().length ) {
return;
}
$country_input.trigger( 'refresh' );
});
});
});

View File

@ -0,0 +1 @@
jQuery(function(u){if("undefined"==typeof wc_country_select_params)return!1;var t;u().selectWoo&&((t=function(){u("select.country_select:visible, select.state_select:visible").each(function(){var t=u(this),t=u.extend({placeholder:t.attr("data-placeholder")||t.attr("placeholder")||"",label:t.attr("data-label")||null,width:"100%"},{language:{errorLoading:function(){return wc_country_select_params.i18n_searching},inputTooLong:function(t){t=t.input.length-t.maximum;return 1==t?wc_country_select_params.i18n_input_too_long_1:wc_country_select_params.i18n_input_too_long_n.replace("%qty%",t)},inputTooShort:function(t){t=t.minimum-t.input.length;return 1==t?wc_country_select_params.i18n_input_too_short_1:wc_country_select_params.i18n_input_too_short_n.replace("%qty%",t)},loadingMore:function(){return wc_country_select_params.i18n_load_more},maximumSelected:function(t){return 1===t.maximum?wc_country_select_params.i18n_selection_too_long_1:wc_country_select_params.i18n_selection_too_long_n.replace("%qty%",t.maximum)},noResults:function(){return wc_country_select_params.i18n_no_matches},searching:function(){return wc_country_select_params.i18n_searching}}});u(this).on("select2:select",function(){u(this).trigger("focus")}).selectWoo(t)})})(),u(document.body).on("country_to_state_changed",function(){t()}));var e=wc_country_select_params.countries.replace(/&quot;/g,'"'),d=JSON.parse(e),h=".woocommerce-billing-fields,.woocommerce-shipping-fields,.woocommerce-address-fields,.woocommerce-shipping-calculator";u(document.body).on("change refresh","select.country_to_state, input.country_to_state",function(){var t=u(this).closest(h);t.length||(t=u(this).closest(".form-row").parent());var e,n,o,c=u(this).val(),a=t.find("#billing_state, #shipping_state, #calc_shipping_state"),r=a.closest(".form-row"),i=a.attr("name"),s=a.attr("id"),_=a.attr("data-input-classes"),l=a.val(),p=a.attr("placeholder")||a.attr("data-placeholder")||"";d[c]?(u.isEmptyObject(d[c])?(e=u('<input type="hidden" />').prop("id",s).prop("name",i).prop("placeholder",p).attr("data-input-classes",_).addClass("hidden "+_),r.hide().find(".select2-container").remove(),a.replaceWith(e)):(n=d[c],o=u('<option value=""></option>').text(wc_country_select_params.i18n_select_state_text),p=p||wc_country_select_params.i18n_select_state_text,r.show(),a.is("input")&&(e=u("<select></select>").prop("id",s).prop("name",i).data("placeholder",p).attr("data-input-classes",_).addClass("state_select "+_),a.replaceWith(e),a=t.find("#billing_state, #shipping_state, #calc_shipping_state")),a.empty().append(o),u.each(n,function(t){t=u("<option></option>").prop("value",t).text(n[t]);a.append(t)}),a.val(l).trigger("change")),u(document.body).trigger("country_to_state_changed",[c,t])):a.is('select, input[type="hidden"]')&&(e=u('<input type="text" />').prop("id",s).prop("name",i).prop("placeholder",p).attr("data-input-classes",_).addClass("input-text "+_),r.show().find(".select2-container").remove(),a.replaceWith(e),u(document.body).trigger("country_to_state_changed",[c,t])),u(document.body).trigger("country_to_state_changing",[c,t])}),u(document.body).on("wc_address_i18n_ready",function(){u(h).each(function(){var t=u(this).find("#billing_country, #shipping_country, #calc_shipping_country");0!==t.length&&0!==t.val().length&&t.trigger("refresh")})})});

View File

@ -0,0 +1,13 @@
jQuery( function( $ ) {
$( '.wc-credit-card-form-card-number' ).payment( 'formatCardNumber' );
$( '.wc-credit-card-form-card-expiry' ).payment( 'formatCardExpiry' );
$( '.wc-credit-card-form-card-cvc' ).payment( 'formatCardCVC' );
$( document.body )
.on( 'updated_checkout wc-credit-card-form-init', function() {
$( '.wc-credit-card-form-card-number' ).payment( 'formatCardNumber' );
$( '.wc-credit-card-form-card-expiry' ).payment( 'formatCardExpiry' );
$( '.wc-credit-card-form-card-cvc' ).payment( 'formatCardCVC' );
})
.trigger( 'wc-credit-card-form-init' );
} );

View File

@ -0,0 +1 @@
jQuery(function(r){r(".wc-credit-card-form-card-number").payment("formatCardNumber"),r(".wc-credit-card-form-card-expiry").payment("formatCardExpiry"),r(".wc-credit-card-form-card-cvc").payment("formatCardCVC"),r(document.body).on("updated_checkout wc-credit-card-form-init",function(){r(".wc-credit-card-form-card-number").payment("formatCardNumber"),r(".wc-credit-card-form-card-expiry").payment("formatCardExpiry"),r(".wc-credit-card-form-card-cvc").payment("formatCardCVC")}).trigger("wc-credit-card-form-init")});

View File

@ -0,0 +1,145 @@
/*global wc_geolocation_params */
jQuery( function( $ ) {
/**
* Contains the current geo hash (or false if the hash
* is not set/cannot be determined).
*
* @type {boolean|string}
*/
var geo_hash = false;
/**
* Obtains the current geo hash from the `woocommerce_geo_hash` cookie, if set.
*
* @returns {boolean}
*/
function get_geo_hash() {
var geo_hash_cookie = Cookies.get( 'woocommerce_geo_hash' );
if ( 'string' === typeof geo_hash_cookie && geo_hash_cookie.length ) {
geo_hash = geo_hash_cookie;
return true;
}
return false;
}
/**
* If we have an active geo hash value but it does not match the `?v=` query var in
* current page URL, that indicates that we need to refresh the page.
*
* @returns {boolean}
*/
function needs_refresh() {
return geo_hash && ( new URLSearchParams( window.location.search ) ).get( 'v' ) !== geo_hash;
}
/**
* Appends (or replaces) the geo hash used for links on the current page.
*/
var $append_hashes = function() {
if ( ! geo_hash ) {
return;
}
$( 'a[href^="' + wc_geolocation_params.home_url + '"]:not(a[href*="v="]), a[href^="/"]:not(a[href*="v="])' ).each( function() {
var $this = $( this ),
href = $this.attr( 'href' ),
href_parts = href.split( '#' );
href = href_parts[0];
if ( href.indexOf( '?' ) > 0 ) {
href = href + '&v=' + geo_hash;
} else {
href = href + '?v=' + geo_hash;
}
if ( typeof href_parts[1] !== 'undefined' && href_parts[1] !== null ) {
href = href + '#' + href_parts[1];
}
$this.attr( 'href', href );
});
};
var $geolocate_customer = {
url: wc_geolocation_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_customer_location' ),
type: 'GET',
success: function( response ) {
if ( response.success && response.data.hash && response.data.hash !== geo_hash ) {
$geolocation_redirect( response.data.hash );
}
}
};
/**
* Once we have a new hash, we redirect so a new version of the current page
* (with correct pricing for the current region, etc) is displayed.
*
* @param {string} hash
*/
var $geolocation_redirect = function( hash ) {
// Updates our (cookie-based) cache of the hash value. Expires in 1 hour.
Cookies.set( 'woocommerce_geo_hash', hash, { expires: 1 / 24 } );
var this_page = window.location.toString();
if ( this_page.indexOf( '?v=' ) > 0 || this_page.indexOf( '&v=' ) > 0 ) {
this_page = this_page.replace( /v=[^&]+/, 'v=' + hash );
} else if ( this_page.indexOf( '?' ) > 0 ) {
this_page = this_page + '&v=' + hash;
} else {
this_page = this_page + '?v=' + hash;
}
window.location = this_page;
};
/**
* Updates any forms on the page so they use the current geo hash.
*/
function update_forms() {
if ( ! geo_hash ) {
return;
}
$( 'form' ).each( function () {
var $this = $( this );
var method = $this.attr( 'method' );
var hasField = $this.find( 'input[name="v"]' ).length > 0;
if ( method && 'get' === method.toLowerCase() && ! hasField ) {
$this.append( '<input type="hidden" name="v" value="' + geo_hash + '" />' );
} else {
var href = $this.attr( 'action' );
if ( href ) {
if ( href.indexOf( '?' ) > 0 ) {
$this.attr( 'action', href + '&v=' + geo_hash );
} else {
$this.attr( 'action', href + '?v=' + geo_hash );
}
}
}
});
}
// Get the current geo hash. If it doesn't exist, or if it doesn't match the current
// page URL, perform a geolocation request.
if ( ! get_geo_hash() || needs_refresh() ) {
$.ajax( $geolocate_customer );
}
// Page updates.
update_forms();
$append_hashes();
$( document.body ).on( 'added_to_cart', function() {
$append_hashes();
});
// Enable user to trigger manual append hashes on AJAX operations
$( document.body ).on( 'woocommerce_append_geo_hashes', function() {
$append_hashes();
});
});

1
assets/js/frontend/geolocation.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(a){var n=!1;var e,o=function(){n&&a('a[href^="'+wc_geolocation_params.home_url+'"]:not(a[href*="v="]), a[href^="/"]:not(a[href*="v="])').each(function(){var e=a(this),o=e.attr("href"),t=o.split("#");o=0<(o=t[0]).indexOf("?")?o+"&v="+n:o+"?v="+n,"undefined"!=typeof t[1]&&null!==t[1]&&(o=o+"#"+t[1]),e.attr("href",o)})},t={url:wc_geolocation_params.wc_ajax_url.toString().replace("%%endpoint%%","get_customer_location"),type:"GET",success:function(e){e.success&&e.data.hash&&e.data.hash!==n&&c(e.data.hash)}},c=function(e){Cookies.set("woocommerce_geo_hash",e,{expires:1/24});var o=0<(o=window.location.toString()).indexOf("?v=")||0<o.indexOf("&v=")?o.replace(/v=[^&]+/,"v="+e):0<o.indexOf("?")?o+"&v="+e:o+"?v="+e;window.location=o};("string"!=typeof(e=Cookies.get("woocommerce_geo_hash"))||!e.length||(n=e,0)||n&&new URLSearchParams(window.location.search).get("v")!==n)&&a.ajax(t),n&&a("form").each(function(){var e=a(this),o=e.attr("method"),t=0<e.find('input[name="v"]').length;o&&"get"===o.toLowerCase()&&!t?e.append('<input type="hidden" name="v" value="'+n+'" />'):(t=e.attr("action"))&&(0<t.indexOf("?")?e.attr("action",t+"&v="+n):e.attr("action",t+"?v="+n))}),o(),a(document.body).on("added_to_cart",function(){o()}),a(document.body).on("woocommerce_append_geo_hashes",function(){o()})});

View File

@ -0,0 +1,5 @@
jQuery( function( $ ) {
$( '.lost_reset_password' ).on( 'submit', function () {
$( 'button[type="submit"]', this ).attr( 'disabled', 'disabled' );
});
});

View File

@ -0,0 +1 @@
jQuery(function(t){t(".lost_reset_password").on("submit",function(){t('button[type="submit"]',this).attr("disabled","disabled")})});

View File

@ -0,0 +1,132 @@
/* global wp, pwsL10n, wc_password_strength_meter_params */
( function( $ ) {
'use strict';
/**
* Password Strength Meter class.
*/
var wc_password_strength_meter = {
/**
* Initialize strength meter actions.
*/
init: function() {
$( document.body )
.on(
'keyup change',
'form.register #reg_password, form.checkout #account_password, ' +
'form.edit-account #password_1, form.lost_reset_password #password_1',
this.strengthMeter
);
$( 'form.checkout #createaccount' ).trigger( 'change' );
},
/**
* Strength Meter.
*/
strengthMeter: function() {
var wrapper = $( 'form.register, form.checkout, form.edit-account, form.lost_reset_password' ),
submit = $( 'button[type="submit"]', wrapper ),
field = $( '#reg_password, #account_password, #password_1', wrapper ),
strength = 1,
fieldValue = field.val(),
stop_checkout = ! wrapper.is( 'form.checkout' ); // By default is disabled on checkout.
wc_password_strength_meter.includeMeter( wrapper, field );
strength = wc_password_strength_meter.checkPasswordStrength( wrapper, field );
// Allow password strength meter stop checkout.
if ( wc_password_strength_meter_params.stop_checkout ) {
stop_checkout = true;
}
if (
fieldValue.length > 0 &&
strength < wc_password_strength_meter_params.min_password_strength &&
-1 !== strength &&
stop_checkout
) {
submit.attr( 'disabled', 'disabled' ).addClass( 'disabled' );
} else {
submit.prop( 'disabled', false ).removeClass( 'disabled' );
}
},
/**
* Include meter HTML.
*
* @param {Object} wrapper
* @param {Object} field
*/
includeMeter: function( wrapper, field ) {
var meter = wrapper.find( '.woocommerce-password-strength' );
if ( '' === field.val() ) {
meter.hide();
$( document.body ).trigger( 'wc-password-strength-hide' );
} else if ( 0 === meter.length ) {
field.after( '<div class="woocommerce-password-strength" aria-live="polite"></div>' );
$( document.body ).trigger( 'wc-password-strength-added' );
} else {
meter.show();
$( document.body ).trigger( 'wc-password-strength-show' );
}
},
/**
* Check password strength.
*
* @param {Object} field
*
* @return {Int}
*/
checkPasswordStrength: function( wrapper, field ) {
var meter = wrapper.find( '.woocommerce-password-strength' ),
hint = wrapper.find( '.woocommerce-password-hint' ),
hint_html = '<small class="woocommerce-password-hint">' + wc_password_strength_meter_params.i18n_password_hint + '</small>',
strength = wp.passwordStrength.meter( field.val(), wp.passwordStrength.userInputDisallowedList() ),
error = '';
// Reset.
meter.removeClass( 'short bad good strong' );
hint.remove();
if ( meter.is( ':hidden' ) ) {
return strength;
}
// Error to append
if ( strength < wc_password_strength_meter_params.min_password_strength ) {
error = ' - ' + wc_password_strength_meter_params.i18n_password_error;
}
switch ( strength ) {
case 0 :
meter.addClass( 'short' ).html( pwsL10n['short'] + error );
meter.after( hint_html );
break;
case 1 :
meter.addClass( 'bad' ).html( pwsL10n.bad + error );
meter.after( hint_html );
break;
case 2 :
meter.addClass( 'bad' ).html( pwsL10n.bad + error );
meter.after( hint_html );
break;
case 3 :
meter.addClass( 'good' ).html( pwsL10n.good + error );
break;
case 4 :
meter.addClass( 'strong' ).html( pwsL10n.strong + error );
break;
case 5 :
meter.addClass( 'short' ).html( pwsL10n.mismatch );
break;
}
return strength;
}
};
wc_password_strength_meter.init();
})( jQuery );

View File

@ -0,0 +1 @@
!function(d){"use strict";var n={init:function(){d(document.body).on("keyup change","form.register #reg_password, form.checkout #account_password, form.edit-account #password_1, form.lost_reset_password #password_1",this.strengthMeter),d("form.checkout #createaccount").trigger("change")},strengthMeter:function(){var s,r=d("form.register, form.checkout, form.edit-account, form.lost_reset_password"),e=d('button[type="submit"]',r),t=d("#reg_password, #account_password, #password_1",r),o=t.val(),a=!r.is("form.checkout");n.includeMeter(r,t),s=n.checkPasswordStrength(r,t),wc_password_strength_meter_params.stop_checkout&&(a=!0),0<o.length&&s<wc_password_strength_meter_params.min_password_strength&&-1!==s&&a?e.attr("disabled","disabled").addClass("disabled"):e.prop("disabled",!1).removeClass("disabled")},includeMeter:function(s,r){s=s.find(".woocommerce-password-strength");""===r.val()?(s.hide(),d(document.body).trigger("wc-password-strength-hide")):0===s.length?(r.after('<div class="woocommerce-password-strength" aria-live="polite"></div>'),d(document.body).trigger("wc-password-strength-added")):(s.show(),d(document.body).trigger("wc-password-strength-show"))},checkPasswordStrength:function(s,r){var e=s.find(".woocommerce-password-strength"),t=s.find(".woocommerce-password-hint"),o='<small class="woocommerce-password-hint">'+wc_password_strength_meter_params.i18n_password_hint+"</small>",s=wp.passwordStrength.meter(r.val(),wp.passwordStrength.userInputDisallowedList()),r="";if(e.removeClass("short bad good strong"),t.remove(),e.is(":hidden"))return s;switch(s<wc_password_strength_meter_params.min_password_strength&&(r=" - "+wc_password_strength_meter_params.i18n_password_error),s){case 0:e.addClass("short").html(pwsL10n["short"]+r),e.after(o);break;case 1:case 2:e.addClass("bad").html(pwsL10n.bad+r),e.after(o);break;case 3:e.addClass("good").html(pwsL10n.good+r);break;case 4:e.addClass("strong").html(pwsL10n.strong+r);break;case 5:e.addClass("short").html(pwsL10n.mismatch)}return s}};n.init()}(jQuery);

View File

@ -0,0 +1,83 @@
/* global woocommerce_price_slider_params, accounting */
jQuery( function( $ ) {
// woocommerce_price_slider_params is required to continue, ensure the object exists
if ( typeof woocommerce_price_slider_params === 'undefined' ) {
return false;
}
$( document.body ).on( 'price_slider_create price_slider_slide', function( event, min, max ) {
$( '.price_slider_amount span.from' ).html( accounting.formatMoney( min, {
symbol: woocommerce_price_slider_params.currency_format_symbol,
decimal: woocommerce_price_slider_params.currency_format_decimal_sep,
thousand: woocommerce_price_slider_params.currency_format_thousand_sep,
precision: woocommerce_price_slider_params.currency_format_num_decimals,
format: woocommerce_price_slider_params.currency_format
} ) );
$( '.price_slider_amount span.to' ).html( accounting.formatMoney( max, {
symbol: woocommerce_price_slider_params.currency_format_symbol,
decimal: woocommerce_price_slider_params.currency_format_decimal_sep,
thousand: woocommerce_price_slider_params.currency_format_thousand_sep,
precision: woocommerce_price_slider_params.currency_format_num_decimals,
format: woocommerce_price_slider_params.currency_format
} ) );
$( document.body ).trigger( 'price_slider_updated', [ min, max ] );
});
function init_price_filter() {
$( 'input#min_price, input#max_price' ).hide();
$( '.price_slider, .price_label' ).show();
var min_price = $( '.price_slider_amount #min_price' ).data( 'min' ),
max_price = $( '.price_slider_amount #max_price' ).data( 'max' ),
step = $( '.price_slider_amount' ).data( 'step' ) || 1,
current_min_price = $( '.price_slider_amount #min_price' ).val(),
current_max_price = $( '.price_slider_amount #max_price' ).val();
$( '.price_slider:not(.ui-slider)' ).slider({
range: true,
animate: true,
min: min_price,
max: max_price,
step: step,
values: [ current_min_price, current_max_price ],
create: function() {
$( '.price_slider_amount #min_price' ).val( current_min_price );
$( '.price_slider_amount #max_price' ).val( current_max_price );
$( document.body ).trigger( 'price_slider_create', [ current_min_price, current_max_price ] );
},
slide: function( event, ui ) {
$( 'input#min_price' ).val( ui.values[0] );
$( 'input#max_price' ).val( ui.values[1] );
$( document.body ).trigger( 'price_slider_slide', [ ui.values[0], ui.values[1] ] );
},
change: function( event, ui ) {
$( document.body ).trigger( 'price_slider_change', [ ui.values[0], ui.values[1] ] );
}
});
}
init_price_filter();
$( document.body ).on( 'init_price_filter', init_price_filter );
var hasSelectiveRefresh = (
'undefined' !== typeof wp &&
wp.customize &&
wp.customize.selectiveRefresh &&
wp.customize.widgetsPreview &&
wp.customize.widgetsPreview.WidgetPartial
);
if ( hasSelectiveRefresh ) {
wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() {
init_price_filter();
} );
}
});

View File

@ -0,0 +1 @@
jQuery(function(_){return"undefined"!=typeof woocommerce_price_slider_params&&(_(document.body).on("price_slider_create price_slider_slide",function(e,r,i){_(".price_slider_amount span.from").html(accounting.formatMoney(r,{symbol:woocommerce_price_slider_params.currency_format_symbol,decimal:woocommerce_price_slider_params.currency_format_decimal_sep,thousand:woocommerce_price_slider_params.currency_format_thousand_sep,precision:woocommerce_price_slider_params.currency_format_num_decimals,format:woocommerce_price_slider_params.currency_format})),_(".price_slider_amount span.to").html(accounting.formatMoney(i,{symbol:woocommerce_price_slider_params.currency_format_symbol,decimal:woocommerce_price_slider_params.currency_format_decimal_sep,thousand:woocommerce_price_slider_params.currency_format_thousand_sep,precision:woocommerce_price_slider_params.currency_format_num_decimals,format:woocommerce_price_slider_params.currency_format})),_(document.body).trigger("price_slider_updated",[r,i])}),e(),_(document.body).on("init_price_filter",e),void("undefined"!=typeof wp&&wp.customize&&wp.customize.selectiveRefresh&&wp.customize.widgetsPreview&&wp.customize.widgetsPreview.WidgetPartial&&wp.customize.selectiveRefresh.bind("partial-content-rendered",function(){e()})));function e(){_("input#min_price, input#max_price").hide(),_(".price_slider, .price_label").show();var e=_(".price_slider_amount #min_price").data("min"),r=_(".price_slider_amount #max_price").data("max"),i=_(".price_slider_amount").data("step")||1,c=_(".price_slider_amount #min_price").val(),o=_(".price_slider_amount #max_price").val();_(".price_slider:not(.ui-slider)").slider({range:!0,animate:!0,min:e,max:r,step:i,values:[c,o],create:function(){_(".price_slider_amount #min_price").val(c),_(".price_slider_amount #max_price").val(o),_(document.body).trigger("price_slider_create",[c,o])},slide:function(e,r){_("input#min_price").val(r.values[0]),_("input#max_price").val(r.values[1]),_(document.body).trigger("price_slider_slide",[r.values[0],r.values[1]])},change:function(e,r){_(document.body).trigger("price_slider_change",[r.values[0],r.values[1]])}})}});

View File

@ -0,0 +1,346 @@
/*global wc_single_product_params, PhotoSwipe, PhotoSwipeUI_Default */
jQuery( function( $ ) {
// wc_single_product_params is required to continue.
if ( typeof wc_single_product_params === 'undefined' ) {
return false;
}
$( 'body' )
// Tabs
.on( 'init', '.wc-tabs-wrapper, .woocommerce-tabs', function() {
$( this ).find( '.wc-tab, .woocommerce-tabs .panel:not(.panel .panel)' ).hide();
var hash = window.location.hash;
var url = window.location.href;
var $tabs = $( this ).find( '.wc-tabs, ul.tabs' ).first();
if ( hash.toLowerCase().indexOf( 'comment-' ) >= 0 || hash === '#reviews' || hash === '#tab-reviews' ) {
$tabs.find( 'li.reviews_tab a' ).trigger( 'click' );
} else if ( url.indexOf( 'comment-page-' ) > 0 || url.indexOf( 'cpage=' ) > 0 ) {
$tabs.find( 'li.reviews_tab a' ).trigger( 'click' );
} else if ( hash === '#tab-additional_information' ) {
$tabs.find( 'li.additional_information_tab a' ).trigger( 'click' );
} else {
$tabs.find( 'li:first a' ).trigger( 'click' );
}
} )
.on( 'click', '.wc-tabs li a, ul.tabs li a', function( e ) {
e.preventDefault();
var $tab = $( this );
var $tabs_wrapper = $tab.closest( '.wc-tabs-wrapper, .woocommerce-tabs' );
var $tabs = $tabs_wrapper.find( '.wc-tabs, ul.tabs' );
$tabs.find( 'li' ).removeClass( 'active' );
$tabs_wrapper.find( '.wc-tab, .panel:not(.panel .panel)' ).hide();
$tab.closest( 'li' ).addClass( 'active' );
$tabs_wrapper.find( $tab.attr( 'href' ) ).show();
} )
// Review link
.on( 'click', 'a.woocommerce-review-link', function() {
$( '.reviews_tab a' ).trigger( 'click' );
return true;
} )
// Star ratings for comments
.on( 'init', '#rating', function() {
$( '#rating' )
.hide()
.before(
'<p class="stars">\
<span>\
<a class="star-1" href="#">1</a>\
<a class="star-2" href="#">2</a>\
<a class="star-3" href="#">3</a>\
<a class="star-4" href="#">4</a>\
<a class="star-5" href="#">5</a>\
</span>\
</p>'
);
} )
.on( 'click', '#respond p.stars a', function() {
var $star = $( this ),
$rating = $( this ).closest( '#respond' ).find( '#rating' ),
$container = $( this ).closest( '.stars' );
$rating.val( $star.text() );
$star.siblings( 'a' ).removeClass( 'active' );
$star.addClass( 'active' );
$container.addClass( 'selected' );
return false;
} )
.on( 'click', '#respond #submit', function() {
var $rating = $( this ).closest( '#respond' ).find( '#rating' ),
rating = $rating.val();
if ( $rating.length > 0 && ! rating && wc_single_product_params.review_rating_required === 'yes' ) {
window.alert( wc_single_product_params.i18n_required_rating_text );
return false;
}
} );
// Init Tabs and Star Ratings
$( '.wc-tabs-wrapper, .woocommerce-tabs, #rating' ).trigger( 'init' );
/**
* Product gallery class.
*/
var ProductGallery = function( $target, args ) {
this.$target = $target;
this.$images = $( '.woocommerce-product-gallery__image', $target );
// No images? Abort.
if ( 0 === this.$images.length ) {
this.$target.css( 'opacity', 1 );
return;
}
// Make this object available.
$target.data( 'product_gallery', this );
// Pick functionality to initialize...
this.flexslider_enabled = 'function' === typeof $.fn.flexslider && wc_single_product_params.flexslider_enabled;
this.zoom_enabled = 'function' === typeof $.fn.zoom && wc_single_product_params.zoom_enabled;
this.photoswipe_enabled = typeof PhotoSwipe !== 'undefined' && wc_single_product_params.photoswipe_enabled;
// ...also taking args into account.
if ( args ) {
this.flexslider_enabled = false === args.flexslider_enabled ? false : this.flexslider_enabled;
this.zoom_enabled = false === args.zoom_enabled ? false : this.zoom_enabled;
this.photoswipe_enabled = false === args.photoswipe_enabled ? false : this.photoswipe_enabled;
}
// ...and what is in the gallery.
if ( 1 === this.$images.length ) {
this.flexslider_enabled = false;
}
// Bind functions to this.
this.initFlexslider = this.initFlexslider.bind( this );
this.initZoom = this.initZoom.bind( this );
this.initZoomForTarget = this.initZoomForTarget.bind( this );
this.initPhotoswipe = this.initPhotoswipe.bind( this );
this.onResetSlidePosition = this.onResetSlidePosition.bind( this );
this.getGalleryItems = this.getGalleryItems.bind( this );
this.openPhotoswipe = this.openPhotoswipe.bind( this );
if ( this.flexslider_enabled ) {
this.initFlexslider( args.flexslider );
$target.on( 'woocommerce_gallery_reset_slide_position', this.onResetSlidePosition );
} else {
this.$target.css( 'opacity', 1 );
}
if ( this.zoom_enabled ) {
this.initZoom();
$target.on( 'woocommerce_gallery_init_zoom', this.initZoom );
}
if ( this.photoswipe_enabled ) {
this.initPhotoswipe();
}
};
/**
* Initialize flexSlider.
*/
ProductGallery.prototype.initFlexslider = function( args ) {
var $target = this.$target,
gallery = this;
var options = $.extend( {
selector: '.woocommerce-product-gallery__wrapper > .woocommerce-product-gallery__image',
start: function() {
$target.css( 'opacity', 1 );
},
after: function( slider ) {
gallery.initZoomForTarget( gallery.$images.eq( slider.currentSlide ) );
}
}, args );
$target.flexslider( options );
// Trigger resize after main image loads to ensure correct gallery size.
$( '.woocommerce-product-gallery__wrapper .woocommerce-product-gallery__image:eq(0) .wp-post-image' ).one( 'load', function() {
var $image = $( this );
if ( $image ) {
setTimeout( function() {
var setHeight = $image.closest( '.woocommerce-product-gallery__image' ).height();
var $viewport = $image.closest( '.flex-viewport' );
if ( setHeight && $viewport ) {
$viewport.height( setHeight );
}
}, 100 );
}
} ).each( function() {
if ( this.complete ) {
$( this ).trigger( 'load' );
}
} );
};
/**
* Init zoom.
*/
ProductGallery.prototype.initZoom = function() {
this.initZoomForTarget( this.$images.first() );
};
/**
* Init zoom.
*/
ProductGallery.prototype.initZoomForTarget = function( zoomTarget ) {
if ( ! this.zoom_enabled ) {
return false;
}
var galleryWidth = this.$target.width(),
zoomEnabled = false;
$( zoomTarget ).each( function( index, target ) {
var image = $( target ).find( 'img' );
if ( image.data( 'large_image_width' ) > galleryWidth ) {
zoomEnabled = true;
return false;
}
} );
// But only zoom if the img is larger than its container.
if ( zoomEnabled ) {
var zoom_options = $.extend( {
touch: false
}, wc_single_product_params.zoom_options );
if ( 'ontouchstart' in document.documentElement ) {
zoom_options.on = 'click';
}
zoomTarget.trigger( 'zoom.destroy' );
zoomTarget.zoom( zoom_options );
setTimeout( function() {
if ( zoomTarget.find(':hover').length ) {
zoomTarget.trigger( 'mouseover' );
}
}, 100 );
}
};
/**
* Init PhotoSwipe.
*/
ProductGallery.prototype.initPhotoswipe = function() {
if ( this.zoom_enabled && this.$images.length > 0 ) {
this.$target.prepend( '<a href="#" class="woocommerce-product-gallery__trigger">🔍</a>' );
this.$target.on( 'click', '.woocommerce-product-gallery__trigger', this.openPhotoswipe );
this.$target.on( 'click', '.woocommerce-product-gallery__image a', function( e ) {
e.preventDefault();
});
// If flexslider is disabled, gallery images also need to trigger photoswipe on click.
if ( ! this.flexslider_enabled ) {
this.$target.on( 'click', '.woocommerce-product-gallery__image a', this.openPhotoswipe );
}
} else {
this.$target.on( 'click', '.woocommerce-product-gallery__image a', this.openPhotoswipe );
}
};
/**
* Reset slide position to 0.
*/
ProductGallery.prototype.onResetSlidePosition = function() {
this.$target.flexslider( 0 );
};
/**
* Get product gallery image items.
*/
ProductGallery.prototype.getGalleryItems = function() {
var $slides = this.$images,
items = [];
if ( $slides.length > 0 ) {
$slides.each( function( i, el ) {
var img = $( el ).find( 'img' );
if ( img.length ) {
var large_image_src = img.attr( 'data-large_image' ),
large_image_w = img.attr( 'data-large_image_width' ),
large_image_h = img.attr( 'data-large_image_height' ),
alt = img.attr( 'alt' ),
item = {
alt : alt,
src : large_image_src,
w : large_image_w,
h : large_image_h,
title: img.attr( 'data-caption' ) ? img.attr( 'data-caption' ) : img.attr( 'title' )
};
items.push( item );
}
} );
}
return items;
};
/**
* Open photoswipe modal.
*/
ProductGallery.prototype.openPhotoswipe = function( e ) {
e.preventDefault();
var pswpElement = $( '.pswp' )[0],
items = this.getGalleryItems(),
eventTarget = $( e.target ),
clicked;
if ( eventTarget.is( '.woocommerce-product-gallery__trigger' ) || eventTarget.is( '.woocommerce-product-gallery__trigger img' ) ) {
clicked = this.$target.find( '.flex-active-slide' );
} else {
clicked = eventTarget.closest( '.woocommerce-product-gallery__image' );
}
var options = $.extend( {
index: $( clicked ).index(),
addCaptionHTMLFn: function( item, captionEl ) {
if ( ! item.title ) {
captionEl.children[0].textContent = '';
return false;
}
captionEl.children[0].textContent = item.title;
return true;
}
}, wc_single_product_params.photoswipe_options );
// Initializes and opens PhotoSwipe.
var photoswipe = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options );
photoswipe.init();
};
/**
* Function to call wc_product_gallery on jquery selector.
*/
$.fn.wc_product_gallery = function( args ) {
new ProductGallery( this, args || wc_single_product_params );
return this;
};
/*
* Initialize all galleries on page.
*/
$( '.woocommerce-product-gallery' ).each( function() {
$( this ).trigger( 'wc-product-gallery-before-init', [ this, wc_single_product_params ] );
$( this ).wc_product_gallery( wc_single_product_params );
$( this ).trigger( 'wc-product-gallery-after-init', [ this, wc_single_product_params ] );
} );
} );

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,117 @@
/*global wc_tokenization_form_params */
jQuery( function( $ ) {
/**
* WCTokenizationForm class.
*/
var TokenizationForm = function( $target ) {
this.$target = $target;
this.$formWrap = $target.closest( '.payment_box' );
// Params.
this.params = $.extend( {}, {
'is_registration_required': false,
'is_logged_in' : false
}, wc_tokenization_form_params );
// Bind functions to this.
this.onDisplay = this.onDisplay.bind( this );
this.hideForm = this.hideForm.bind( this );
this.showForm = this.showForm.bind( this );
this.showSaveNewCheckbox = this.showSaveNewCheckbox.bind( this );
this.hideSaveNewCheckbox = this.hideSaveNewCheckbox.bind( this );
// When a radio button is changed, make sure to show/hide our new CC info area.
this.$target.on(
'click change',
':input.woocommerce-SavedPaymentMethods-tokenInput',
{ tokenizationForm: this },
this.onTokenChange
);
// OR if create account is checked.
$( 'input#createaccount' ).on( 'change', { tokenizationForm: this }, this.onCreateAccountChange );
// First display.
this.onDisplay();
};
TokenizationForm.prototype.onDisplay = function() {
// Make sure a radio button is selected if there is no is_default for this payment method..
if ( 0 === $( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', this.$target ).length ) {
$( ':input.woocommerce-SavedPaymentMethods-tokenInput:last', this.$target ).prop( 'checked', true );
}
// Don't show the "use new" radio button if we only have one method..
if ( 0 === this.$target.data( 'count' ) ) {
$( '.woocommerce-SavedPaymentMethods-new', this.$target ).remove();
}
// Hide "save card" if "Create Account" is not checked and registration is not forced.
var hasCreateAccountCheckbox = 0 < $( 'input#createaccount' ).length,
createAccount = hasCreateAccountCheckbox && $( 'input#createaccount' ).is( ':checked' );
if ( createAccount || this.params.is_logged_in || this.params.is_registration_required ) {
this.showSaveNewCheckbox();
} else {
this.hideSaveNewCheckbox();
}
// Trigger change event
$( ':input.woocommerce-SavedPaymentMethods-tokenInput:checked', this.$target ).trigger( 'change' );
};
TokenizationForm.prototype.onTokenChange = function( event ) {
if ( 'new' === $( this ).val() ) {
event.data.tokenizationForm.showForm();
event.data.tokenizationForm.showSaveNewCheckbox();
} else {
event.data.tokenizationForm.hideForm();
event.data.tokenizationForm.hideSaveNewCheckbox();
}
};
TokenizationForm.prototype.onCreateAccountChange = function( event ) {
if ( $( this ).is( ':checked' ) ) {
event.data.tokenizationForm.showSaveNewCheckbox();
} else {
event.data.tokenizationForm.hideSaveNewCheckbox();
}
};
TokenizationForm.prototype.hideForm = function() {
$( '.wc-payment-form', this.$formWrap ).hide();
};
TokenizationForm.prototype.showForm = function() {
$( '.wc-payment-form', this.$formWrap ).show();
};
TokenizationForm.prototype.showSaveNewCheckbox = function() {
$( '.woocommerce-SavedPaymentMethods-saveNew', this.$formWrap ).show();
};
TokenizationForm.prototype.hideSaveNewCheckbox = function() {
$( '.woocommerce-SavedPaymentMethods-saveNew', this.$formWrap ).hide();
};
/**
* Function to call wc_product_gallery on jquery selector.
*/
$.fn.wc_tokenization_form = function( args ) {
new TokenizationForm( this, args );
return this;
};
/**
* Initialize.
*/
$( document.body ).on( 'updated_checkout wc-credit-card-form-init', function() {
// Loop over gateways with saved payment methods
var $saved_payment_methods = $( 'ul.woocommerce-SavedPaymentMethods' );
$saved_payment_methods.each( function() {
$( this ).wc_tokenization_form();
} );
} );
} );

View File

@ -0,0 +1 @@
jQuery(function(t){var o=function(e){this.$target=e,this.$formWrap=e.closest(".payment_box"),this.params=t.extend({},{is_registration_required:!1,is_logged_in:!1},wc_tokenization_form_params),this.onDisplay=this.onDisplay.bind(this),this.hideForm=this.hideForm.bind(this),this.showForm=this.showForm.bind(this),this.showSaveNewCheckbox=this.showSaveNewCheckbox.bind(this),this.hideSaveNewCheckbox=this.hideSaveNewCheckbox.bind(this),this.$target.on("click change",":input.woocommerce-SavedPaymentMethods-tokenInput",{tokenizationForm:this},this.onTokenChange),t("input#createaccount").on("change",{tokenizationForm:this},this.onCreateAccountChange),this.onDisplay()};o.prototype.onDisplay=function(){0===t(":input.woocommerce-SavedPaymentMethods-tokenInput:checked",this.$target).length&&t(":input.woocommerce-SavedPaymentMethods-tokenInput:last",this.$target).prop("checked",!0),0===this.$target.data("count")&&t(".woocommerce-SavedPaymentMethods-new",this.$target).remove(),0<t("input#createaccount").length&&t("input#createaccount").is(":checked")||this.params.is_logged_in||this.params.is_registration_required?this.showSaveNewCheckbox():this.hideSaveNewCheckbox(),t(":input.woocommerce-SavedPaymentMethods-tokenInput:checked",this.$target).trigger("change")},o.prototype.onTokenChange=function(e){"new"===t(this).val()?(e.data.tokenizationForm.showForm(),e.data.tokenizationForm.showSaveNewCheckbox()):(e.data.tokenizationForm.hideForm(),e.data.tokenizationForm.hideSaveNewCheckbox())},o.prototype.onCreateAccountChange=function(e){t(this).is(":checked")?e.data.tokenizationForm.showSaveNewCheckbox():e.data.tokenizationForm.hideSaveNewCheckbox()},o.prototype.hideForm=function(){t(".wc-payment-form",this.$formWrap).hide()},o.prototype.showForm=function(){t(".wc-payment-form",this.$formWrap).show()},o.prototype.showSaveNewCheckbox=function(){t(".woocommerce-SavedPaymentMethods-saveNew",this.$formWrap).show()},o.prototype.hideSaveNewCheckbox=function(){t(".woocommerce-SavedPaymentMethods-saveNew",this.$formWrap).hide()},t.fn.wc_tokenization_form=function(e){return new o(this),this},t(document.body).on("updated_checkout wc-credit-card-form-init",function(){t("ul.woocommerce-SavedPaymentMethods").each(function(){t(this).wc_tokenization_form()})})});

View File

@ -0,0 +1,102 @@
/* global Cookies */
jQuery( function( $ ) {
// Orderby
$( '.woocommerce-ordering' ).on( 'change', 'select.orderby', function() {
$( this ).closest( 'form' ).trigger( 'submit' );
});
// Target quantity inputs on product pages
$( 'input.qty:not(.product-quantity input.qty)' ).each( function() {
var min = parseFloat( $( this ).attr( 'min' ) );
if ( min >= 0 && parseFloat( $( this ).val() ) < min ) {
$( this ).val( min );
}
});
var noticeID = $( '.woocommerce-store-notice' ).data( 'noticeId' ) || '',
cookieName = 'store_notice' + noticeID;
// Check the value of that cookie and show/hide the notice accordingly
if ( 'hidden' === Cookies.get( cookieName ) ) {
$( '.woocommerce-store-notice' ).hide();
} else {
$( '.woocommerce-store-notice' ).show();
}
// Set a cookie and hide the store notice when the dismiss button is clicked
$( '.woocommerce-store-notice__dismiss-link' ).on( 'click', function( event ) {
Cookies.set( cookieName, 'hidden', { path: '/' } );
$( '.woocommerce-store-notice' ).hide();
event.preventDefault();
});
// Make form field descriptions toggle on focus.
if ( $( '.woocommerce-input-wrapper span.description' ).length ) {
$( document.body ).on( 'click', function() {
$( '.woocommerce-input-wrapper span.description:visible' ).prop( 'aria-hidden', true ).slideUp( 250 );
} );
}
$( '.woocommerce-input-wrapper' ).on( 'click', function( event ) {
event.stopPropagation();
} );
$( '.woocommerce-input-wrapper :input' )
.on( 'keydown', function( event ) {
var input = $( this ),
parent = input.parent(),
description = parent.find( 'span.description' );
if ( 27 === event.which && description.length && description.is( ':visible' ) ) {
description.prop( 'aria-hidden', true ).slideUp( 250 );
event.preventDefault();
return false;
}
} )
.on( 'click focus', function() {
var input = $( this ),
parent = input.parent(),
description = parent.find( 'span.description' );
parent.addClass( 'currentTarget' );
$( '.woocommerce-input-wrapper:not(.currentTarget) span.description:visible' ).prop( 'aria-hidden', true ).slideUp( 250 );
if ( description.length && description.is( ':hidden' ) ) {
description.prop( 'aria-hidden', false ).slideDown( 250 );
}
parent.removeClass( 'currentTarget' );
} );
// Common scroll to element code.
$.scroll_to_notices = function( scrollElement ) {
if ( scrollElement.length ) {
$( 'html, body' ).animate( {
scrollTop: ( scrollElement.offset().top - 100 )
}, 1000 );
}
};
// Show password visiblity hover icon on woocommerce forms
$( '.woocommerce form .woocommerce-Input[type="password"]' ).wrap( '<span class="password-input"></span>' );
// Add 'password-input' class to the password wrapper in checkout page.
$( '.woocommerce form input' ).filter(':password').parent('span').addClass('password-input');
$( '.password-input' ).append( '<span class="show-password-input"></span>' );
$( '.show-password-input' ).on( 'click',
function() {
if ( $( this ).hasClass( 'display-password' ) ) {
$( this ).removeClass( 'display-password' );
} else {
$( this ).addClass( 'display-password' );
}
if ( $( this ).hasClass( 'display-password' ) ) {
$( this ).siblings( ['input[type="password"]'] ).prop( 'type', 'text' );
} else {
$( this ).siblings( 'input[type="text"]' ).prop( 'type', 'password' );
}
}
);
});

1
assets/js/frontend/woocommerce.min.js vendored Normal file
View File

@ -0,0 +1 @@
jQuery(function(s){s(".woocommerce-ordering").on("change","select.orderby",function(){s(this).closest("form").trigger("submit")}),s("input.qty:not(.product-quantity input.qty)").each(function(){var o=parseFloat(s(this).attr("min"));0<=o&&parseFloat(s(this).val())<o&&s(this).val(o)});var e="store_notice"+(s(".woocommerce-store-notice").data("noticeId")||"");"hidden"===Cookies.get(e)?s(".woocommerce-store-notice").hide():s(".woocommerce-store-notice").show(),s(".woocommerce-store-notice__dismiss-link").on("click",function(o){Cookies.set(e,"hidden",{path:"/"}),s(".woocommerce-store-notice").hide(),o.preventDefault()}),s(".woocommerce-input-wrapper span.description").length&&s(document.body).on("click",function(){s(".woocommerce-input-wrapper span.description:visible").prop("aria-hidden",!0).slideUp(250)}),s(".woocommerce-input-wrapper").on("click",function(o){o.stopPropagation()}),s(".woocommerce-input-wrapper :input").on("keydown",function(o){var e=s(this).parent().find("span.description");if(27===o.which&&e.length&&e.is(":visible"))return e.prop("aria-hidden",!0).slideUp(250),o.preventDefault(),!1}).on("click focus",function(){var o=s(this).parent(),e=o.find("span.description");o.addClass("currentTarget"),s(".woocommerce-input-wrapper:not(.currentTarget) span.description:visible").prop("aria-hidden",!0).slideUp(250),e.length&&e.is(":hidden")&&e.prop("aria-hidden",!1).slideDown(250),o.removeClass("currentTarget")}),s.scroll_to_notices=function(o){o.length&&s("html, body").animate({scrollTop:o.offset().top-100},1e3)},s('.woocommerce form .woocommerce-Input[type="password"]').wrap('<span class="password-input"></span>'),s(".woocommerce form input").filter(":password").parent("span").addClass("password-input"),s(".password-input").append('<span class="show-password-input"></span>'),s(".show-password-input").on("click",function(){s(this).hasClass("display-password")?s(this).removeClass("display-password"):s(this).addClass("display-password"),s(this).hasClass("display-password")?s(this).siblings(['input[type="password"]']).prop("type","text"):s(this).siblings('input[type="text"]').prop("type","password")})});

View File

@ -0,0 +1,619 @@
/*!
* jQuery blockUI plugin
* Version 2.70.0-2014.11.23
* Requires jQuery v1.7 or later
*
* Examples at: http://malsup.com/jquery/block/
* Copyright (c) 2007-2013 M. Alsup
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Thanks to Amir-Hossein Sobhi for some excellent contributions!
*/
;(function() {
/*jshint eqeqeq:false curly:false latedef:false */
"use strict";
function setup($) {
$.fn._fadeIn = $.fn.fadeIn;
var noOp = $.noop || function() {};
// this bit is to ensure we don't call setExpression when we shouldn't (with extra muscle to handle
// confusing userAgent strings on Vista)
var msie = /MSIE/.test(navigator.userAgent);
var ie6 = /MSIE 6.0/.test(navigator.userAgent) && ! /MSIE 8.0/.test(navigator.userAgent);
var mode = document.documentMode || 0;
var setExpr = 'function' === typeof document.createElement('div').style.setExpression ? document.createElement('div').style.setExpression : false;
// global $ methods for blocking/unblocking the entire page
$.blockUI = function(opts) { install(window, opts); };
$.unblockUI = function(opts) { remove(window, opts); };
// convenience method for quick growl-like notifications (http://www.google.com/search?q=growl)
$.growlUI = function(title, message, timeout, onClose) {
var $m = $('<div class="growlUI"></div>');
if (title) $m.append('<h1>'+title+'</h1>');
if (message) $m.append('<h2>'+message+'</h2>');
if (timeout === undefined) timeout = 3000;
// Added by konapun: Set timeout to 30 seconds if this growl is moused over, like normal toast notifications
var callBlock = function(opts) {
opts = opts || {};
$.blockUI({
message: $m,
fadeIn : typeof opts.fadeIn !== 'undefined' ? opts.fadeIn : 700,
fadeOut: typeof opts.fadeOut !== 'undefined' ? opts.fadeOut : 1000,
timeout: typeof opts.timeout !== 'undefined' ? opts.timeout : timeout,
centerY: false,
showOverlay: false,
onUnblock: onClose,
css: $.blockUI.defaults.growlCSS
});
};
callBlock();
var nonmousedOpacity = $m.css('opacity');
$m.on( 'mouseover', function() {
callBlock({
fadeIn: 0,
timeout: 30000
});
var displayBlock = $('.blockMsg');
displayBlock.stop(); // cancel fadeout if it has started
displayBlock.fadeTo(300, 1); // make it easier to read the message by removing transparency
}).on( 'mouseout', function() {
$('.blockMsg').fadeOut(1000);
});
// End konapun additions
};
// plugin method for blocking element content
$.fn.block = function(opts) {
if ( this[0] === window ) {
$.blockUI( opts );
return this;
}
var fullOpts = $.extend({}, $.blockUI.defaults, opts || {});
this.each(function() {
var $el = $(this);
if (fullOpts.ignoreIfBlocked && $el.data('blockUI.isBlocked'))
return;
$el.unblock({ fadeOut: 0 });
});
return this.each(function() {
if ($.css(this,'position') == 'static') {
this.style.position = 'relative';
$(this).data('blockUI.static', true);
}
this.style.zoom = 1; // force 'hasLayout' in ie
install(this, opts);
});
};
// plugin method for unblocking element content
$.fn.unblock = function(opts) {
if ( this[0] === window ) {
$.unblockUI( opts );
return this;
}
return this.each(function() {
remove(this, opts);
});
};
$.blockUI.version = 2.70; // 2nd generation blocking at no extra cost!
// override these in your code to change the default behavior and style
$.blockUI.defaults = {
// message displayed when blocking (use null for no message)
message: '<h1>Please wait...</h1>',
title: null, // title string; only used when theme == true
draggable: true, // only used when theme == true (requires jquery-ui.js to be loaded)
theme: false, // set to true to use with jQuery UI themes
// styles for the message when blocking; if you wish to disable
// these and use an external stylesheet then do this in your code:
// $.blockUI.defaults.css = {};
css: {
padding: 0,
margin: 0,
width: '30%',
top: '40%',
left: '35%',
textAlign: 'center',
color: '#000',
border: '3px solid #aaa',
backgroundColor:'#fff',
cursor: 'wait'
},
// minimal style set used when themes are used
themedCSS: {
width: '30%',
top: '40%',
left: '35%'
},
// styles for the overlay
overlayCSS: {
backgroundColor: '#000',
opacity: 0.6,
cursor: 'wait'
},
// style to replace wait cursor before unblocking to correct issue
// of lingering wait cursor
cursorReset: 'default',
// styles applied when using $.growlUI
growlCSS: {
width: '350px',
top: '10px',
left: '',
right: '10px',
border: 'none',
padding: '5px',
opacity: 0.6,
cursor: 'default',
color: '#fff',
backgroundColor: '#000',
'-webkit-border-radius':'10px',
'-moz-border-radius': '10px',
'border-radius': '10px'
},
// IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w
// (hat tip to Jorge H. N. de Vasconcelos)
/*jshint scripturl:true */
iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',
// force usage of iframe in non-IE browsers (handy for blocking applets)
forceIframe: false,
// z-index for the blocking overlay
baseZ: 1000,
// set these to true to have the message automatically centered
centerX: true, // <-- only effects element blocking (page block controlled via css above)
centerY: true,
// allow body element to be stetched in ie6; this makes blocking look better
// on "short" pages. disable if you wish to prevent changes to the body height
allowBodyStretch: true,
// enable if you want key and mouse events to be disabled for content that is blocked
bindEvents: true,
// be default blockUI will supress tab navigation from leaving blocking content
// (if bindEvents is true)
constrainTabKey: true,
// fadeIn time in millis; set to 0 to disable fadeIn on block
fadeIn: 200,
// fadeOut time in millis; set to 0 to disable fadeOut on unblock
fadeOut: 400,
// time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
timeout: 0,
// disable if you don't want to show the overlay
showOverlay: true,
// if true, focus will be placed in the first available input field when
// page blocking
focusInput: true,
// elements that can receive focus
focusableElements: ':input:enabled:visible',
// suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
// no longer needed in 2012
// applyPlatformOpacityRules: true,
// callback method invoked when fadeIn has completed and blocking message is visible
onBlock: null,
// callback method invoked when unblocking has completed; the callback is
// passed the element that has been unblocked (which is the window object for page
// blocks) and the options that were passed to the unblock call:
// onUnblock(element, options)
onUnblock: null,
// callback method invoked when the overlay area is clicked.
// setting this will turn the cursor to a pointer, otherwise cursor defined in overlayCss will be used.
onOverlayClick: null,
// don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
quirksmodeOffsetHack: 4,
// class name of the message block
blockMsgClass: 'blockMsg',
// if it is already blocked, then ignore it (don't unblock and reblock)
ignoreIfBlocked: false
};
// private data and functions follow...
var pageBlock = null;
var pageBlockEls = [];
function install(el, opts) {
var css, themedCSS;
var full = (el == window);
var msg = (opts && opts.message !== undefined ? opts.message : undefined);
opts = $.extend({}, $.blockUI.defaults, opts || {});
if (opts.ignoreIfBlocked && $(el).data('blockUI.isBlocked'))
return;
opts.overlayCSS = $.extend({}, $.blockUI.defaults.overlayCSS, opts.overlayCSS || {});
css = $.extend({}, $.blockUI.defaults.css, opts.css || {});
if (opts.onOverlayClick)
opts.overlayCSS.cursor = 'pointer';
themedCSS = $.extend({}, $.blockUI.defaults.themedCSS, opts.themedCSS || {});
msg = msg === undefined ? opts.message : msg;
// remove the current block (if there is one)
if (full && pageBlock)
remove(window, {fadeOut:0});
// if an existing element is being used as the blocking content then we capture
// its current place in the DOM (and current display style) so we can restore
// it when we unblock
if (msg && typeof msg != 'string' && (msg.parentNode || msg.jquery)) {
var node = msg.jquery ? msg[0] : msg;
var data = {};
$(el).data('blockUI.history', data);
data.el = node;
data.parent = node.parentNode;
data.display = node.style.display;
data.position = node.style.position;
if (data.parent)
data.parent.removeChild(node);
}
$(el).data('blockUI.onUnblock', opts.onUnblock);
var z = opts.baseZ;
// blockUI uses 3 layers for blocking, for simplicity they are all used on every platform;
// layer1 is the iframe layer which is used to supress bleed through of underlying content
// layer2 is the overlay layer which has opacity and a wait cursor (by default)
// layer3 is the message content that is displayed while blocking
var lyr1, lyr2, lyr3, s;
if (msie || opts.forceIframe)
lyr1 = $('<iframe class="blockUI" style="z-index:'+ (z++) +';display:none;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="'+opts.iframeSrc+'"></iframe>');
else
lyr1 = $('<div class="blockUI" style="display:none"></div>');
if (opts.theme)
lyr2 = $('<div class="blockUI blockOverlay ui-widget-overlay" style="z-index:'+ (z++) +';display:none"></div>');
else
lyr2 = $('<div class="blockUI blockOverlay" style="z-index:'+ (z++) +';display:none;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');
if (opts.theme && full) {
s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage ui-dialog ui-widget ui-corner-all" style="z-index:'+(z+10)+';display:none;position:fixed">';
if ( opts.title ) {
s += '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || '&nbsp;')+'</div>';
}
s += '<div class="ui-widget-content ui-dialog-content"></div>';
s += '</div>';
}
else if (opts.theme) {
s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement ui-dialog ui-widget ui-corner-all" style="z-index:'+(z+10)+';display:none;position:absolute">';
if ( opts.title ) {
s += '<div class="ui-widget-header ui-dialog-titlebar ui-corner-all blockTitle">'+(opts.title || '&nbsp;')+'</div>';
}
s += '<div class="ui-widget-content ui-dialog-content"></div>';
s += '</div>';
}
else if (full) {
s = '<div class="blockUI ' + opts.blockMsgClass + ' blockPage" style="z-index:'+(z+10)+';display:none;position:fixed"></div>';
}
else {
s = '<div class="blockUI ' + opts.blockMsgClass + ' blockElement" style="z-index:'+(z+10)+';display:none;position:absolute"></div>';
}
lyr3 = $(s);
// if we have a message, style it
if (msg) {
if (opts.theme) {
lyr3.css(themedCSS);
lyr3.addClass('ui-widget-content');
}
else
lyr3.css(css);
}
// style the overlay
if (!opts.theme /*&& (!opts.applyPlatformOpacityRules)*/)
lyr2.css(opts.overlayCSS);
lyr2.css('position', full ? 'fixed' : 'absolute');
// make iframe layer transparent in IE
if (msie || opts.forceIframe)
lyr1.css('opacity',0.0);
//$([lyr1[0],lyr2[0],lyr3[0]]).appendTo(full ? 'body' : el);
var layers = [lyr1,lyr2,lyr3], $par = full ? $('body') : $(el);
$.each(layers, function() {
this.appendTo($par);
});
if (opts.theme && opts.draggable && $.fn.draggable) {
lyr3.draggable({
handle: '.ui-dialog-titlebar',
cancel: 'li'
});
}
// ie7 must use absolute positioning in quirks mode and to account for activex issues (when scrolling)
var expr = setExpr && (!$.support.boxModel || $('object,embed', full ? null : el).length > 0);
if (ie6 || expr) {
// give body 100% height
if (full && opts.allowBodyStretch && $.support.boxModel)
$('html,body').css('height','100%');
// fix ie6 issue when blocked element has a border width
if ((ie6 || !$.support.boxModel) && !full) {
var t = sz(el,'borderTopWidth'), l = sz(el,'borderLeftWidth');
var fixT = t ? '(0 - '+t+')' : 0;
var fixL = l ? '(0 - '+l+')' : 0;
}
// simulate fixed position
$.each(layers, function(i,o) {
var s = o[0].style;
s.position = 'absolute';
if (i < 2) {
if (full)
s.setExpression('height','Math.max(document.body.scrollHeight, document.body.offsetHeight) - (jQuery.support.boxModel?0:'+opts.quirksmodeOffsetHack+') + "px"');
else
s.setExpression('height','this.parentNode.offsetHeight + "px"');
if (full)
s.setExpression('width','jQuery.support.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"');
else
s.setExpression('width','this.parentNode.offsetWidth + "px"');
if (fixL) s.setExpression('left', fixL);
if (fixT) s.setExpression('top', fixT);
}
else if (opts.centerY) {
if (full) s.setExpression('top','(document.documentElement.clientHeight || document.body.clientHeight) / 2 - (this.offsetHeight / 2) + (blah = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + "px"');
s.marginTop = 0;
}
else if (!opts.centerY && full) {
var top = (opts.css && opts.css.top) ? parseInt(opts.css.top, 10) : 0;
var expression = '((document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + '+top+') + "px"';
s.setExpression('top',expression);
}
});
}
// show the message
if (msg) {
if (opts.theme)
lyr3.find('.ui-widget-content').append(msg);
else
lyr3.append(msg);
if (msg.jquery || msg.nodeType)
$(msg).show();
}
if ((msie || opts.forceIframe) && opts.showOverlay)
lyr1.show(); // opacity is zero
if (opts.fadeIn) {
var cb = opts.onBlock ? opts.onBlock : noOp;
var cb1 = (opts.showOverlay && !msg) ? cb : noOp;
var cb2 = msg ? cb : noOp;
if (opts.showOverlay)
lyr2._fadeIn(opts.fadeIn, cb1);
if (msg)
lyr3._fadeIn(opts.fadeIn, cb2);
}
else {
if (opts.showOverlay)
lyr2.show();
if (msg)
lyr3.show();
if (opts.onBlock)
opts.onBlock.bind(lyr3)();
}
// bind key and mouse events
bind(1, el, opts);
if (full) {
pageBlock = lyr3[0];
pageBlockEls = $(opts.focusableElements,pageBlock);
if (opts.focusInput)
setTimeout(focus, 20);
}
else
center(lyr3[0], opts.centerX, opts.centerY);
if (opts.timeout) {
// auto-unblock
var to = setTimeout(function() {
if (full)
$.unblockUI(opts);
else
$(el).unblock(opts);
}, opts.timeout);
$(el).data('blockUI.timeout', to);
}
}
// remove the block
function remove(el, opts) {
var count;
var full = (el == window);
var $el = $(el);
var data = $el.data('blockUI.history');
var to = $el.data('blockUI.timeout');
if (to) {
clearTimeout(to);
$el.removeData('blockUI.timeout');
}
opts = $.extend({}, $.blockUI.defaults, opts || {});
bind(0, el, opts); // unbind events
if (opts.onUnblock === null) {
opts.onUnblock = $el.data('blockUI.onUnblock');
$el.removeData('blockUI.onUnblock');
}
var els;
if (full) // crazy selector to handle odd field errors in ie6/7
els = $(document.body).children().filter('.blockUI').add('body > .blockUI');
else
els = $el.find('>.blockUI');
// fix cursor issue
if ( opts.cursorReset ) {
if ( els.length > 1 )
els[1].style.cursor = opts.cursorReset;
if ( els.length > 2 )
els[2].style.cursor = opts.cursorReset;
}
if (full)
pageBlock = pageBlockEls = null;
if (opts.fadeOut) {
count = els.length;
els.stop().fadeOut(opts.fadeOut, function() {
if ( --count === 0)
reset(els,data,opts,el);
});
}
else
reset(els, data, opts, el);
}
// move blocking element back into the DOM where it started
function reset(els,data,opts,el) {
var $el = $(el);
if ( $el.data('blockUI.isBlocked') )
return;
els.each(function(i,o) {
// remove via DOM calls so we don't lose event handlers
if (this.parentNode)
this.parentNode.removeChild(this);
});
if (data && data.el) {
data.el.style.display = data.display;
data.el.style.position = data.position;
data.el.style.cursor = 'default'; // #59
if (data.parent)
data.parent.appendChild(data.el);
$el.removeData('blockUI.history');
}
if ($el.data('blockUI.static')) {
$el.css('position', 'static'); // #22
}
if (typeof opts.onUnblock == 'function')
opts.onUnblock(el,opts);
// fix issue in Safari 6 where block artifacts remain until reflow
var body = $(document.body), w = body.width(), cssW = body[0].style.width;
body.width(w-1).width(w);
body[0].style.width = cssW;
}
// bind/unbind the handler
function bind(b, el, opts) {
var full = el == window, $el = $(el);
// don't bother unbinding if there is nothing to unbind
if (!b && (full && !pageBlock || !full && !$el.data('blockUI.isBlocked')))
return;
$el.data('blockUI.isBlocked', b);
// don't bind events when overlay is not in use or if bindEvents is false
if (!full || !opts.bindEvents || (b && !opts.showOverlay))
return;
// bind anchors and inputs for mouse and key events
var events = 'mousedown mouseup keydown keypress keyup touchstart touchend touchmove';
if (b)
$(document).on(events, opts, handler);
else
$(document).off(events, handler);
// former impl...
// var $e = $('a,:input');
// b ? $e.bind(events, opts, handler) : $e.unbind(events, handler);
}
// event handler to suppress keyboard/mouse events when blocking
function handler(e) {
// allow tab navigation (conditionally)
if (e.type === 'keydown' && e.keyCode && e.keyCode == 9) {
if (pageBlock && e.data.constrainTabKey) {
var els = pageBlockEls;
var fwd = !e.shiftKey && e.target === els[els.length-1];
var back = e.shiftKey && e.target === els[0];
if (fwd || back) {
setTimeout(function(){focus(back);},10);
return false;
}
}
}
var opts = e.data;
var target = $(e.target);
if (target.hasClass('blockOverlay') && opts.onOverlayClick)
opts.onOverlayClick(e);
// allow events within the message content
if (target.parents('div.' + opts.blockMsgClass).length > 0)
return true;
// allow events for content that is not being blocked
return target.parents().children().filter('div.blockUI').length === 0;
}
function focus(back) {
if (!pageBlockEls)
return;
var e = pageBlockEls[back===true ? pageBlockEls.length-1 : 0];
if (e)
e.trigger( 'focus' );
}
function center(el, x, y) {
var p = el.parentNode, s = el.style;
var l = ((p.offsetWidth - el.offsetWidth)/2) - sz(p,'borderLeftWidth');
var t = ((p.offsetHeight - el.offsetHeight)/2) - sz(p,'borderTopWidth');
if (x) s.left = l > 0 ? (l+'px') : '0';
if (y) s.top = t > 0 ? (t+'px') : '0';
}
function sz(el, p) {
return parseInt($.css(el,p),10)||0;
}
}
/*global define:true */
if (typeof define === 'function' && define.amd && define.amd.jQuery) {
define(['jquery'], setup);
} else {
setup(jQuery);
}
})();

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,117 @@
/*!
* jQuery Cookie Plugin v1.4.1
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2013 Klaus Hartl
* Released under the MIT license
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// CommonJS
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var pluses = /\+/g;
function encode(s) {
return config.raw ? s : encodeURIComponent(s);
}
function decode(s) {
return config.raw ? s : decodeURIComponent(s);
}
function stringifyCookieValue(value) {
return encode(config.json ? JSON.stringify(value) : String(value));
}
function parseCookieValue(s) {
if (s.indexOf('"') === 0) {
// This is a quoted cookie as according to RFC2068, unescape...
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}
try {
// Replace server-side written pluses with spaces.
// If we can't decode the cookie, ignore it, it's unusable.
// If we can't parse the cookie, ignore it, it's unusable.
s = decodeURIComponent(s.replace(pluses, ' '));
return config.json ? JSON.parse(s) : s;
} catch(e) {}
}
function read(s, converter) {
var value = config.raw ? s : parseCookieValue(s);
return 'function' === typeof converter ? converter(value) : value;
}
var config = $.cookie = function (key, value, options) {
// Write
if (value !== undefined && 'function' !== typeof value) {
options = $.extend({}, config.defaults, options);
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setTime(+t + days * 864e+5);
}
return (document.cookie = [
encode(key), '=', stringifyCookieValue(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// Read
var result = key ? undefined : {};
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all. Also prevents odd result when
// calling $.cookie().
var cookies = document.cookie ? document.cookie.split('; ') : [];
for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
var name = decode(parts.shift());
var cookie = parts.join('=');
if (key && key === name) {
// If second argument (value) is a function it's a converter...
result = read(cookie, value);
break;
}
// Prevent storing a cookie that we couldn't decode.
if (!key && (cookie = read(cookie)) !== undefined) {
result[name] = cookie;
}
}
return result;
};
config.defaults = {};
$.removeCookie = function (key, options) {
if ($.cookie(key) === undefined) {
return false;
}
// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};
}));

View File

@ -0,0 +1,8 @@
/*!
* jQuery Cookie Plugin v1.4.1
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2013 Klaus Hartl
* Released under the MIT license
*/
!function(e){"function"==typeof define&&define.amd?define(["jquery"],e):"object"==typeof exports?e(require("jquery")):e(jQuery)}(function(a){var o=/\+/g;function s(e){return x.raw?e:encodeURIComponent(e)}function m(e,n){e=x.raw?e:function(e){0===e.indexOf('"')&&(e=e.slice(1,-1).replace(/\\"/g,'"').replace(/\\\\/g,"\\"));try{return e=decodeURIComponent(e.replace(o," ")),x.json?JSON.parse(e):e}catch(n){}}(e);return"function"==typeof n?n(e):e}var x=a.cookie=function(e,n,o){var i,t;if(n!==undefined&&"function"!=typeof n)return"number"==typeof(o=a.extend({},x.defaults,o)).expires&&(t=o.expires,(i=o.expires=new Date).setTime(+i+864e5*t)),document.cookie=[s(e),"=",(t=n,s(x.json?JSON.stringify(t):String(t))),o.expires?"; expires="+o.expires.toUTCString():"",o.path?"; path="+o.path:"",o.domain?"; domain="+o.domain:"",o.secure?"; secure":""].join("");for(var r=e?undefined:{},c=document.cookie?document.cookie.split("; "):[],u=0,f=c.length;u<f;u++){var d=c[u].split("="),p=(p=d.shift(),x.raw?p:decodeURIComponent(p)),d=d.join("=");if(e&&e===p){r=m(d,n);break}e||(d=m(d))===undefined||(r[p]=d)}return r};x.defaults={},a.removeCookie=function(e,n){return a.cookie(e)!==undefined&&(a.cookie(e,"",a.extend({},n,{expires:-1})),!a.cookie(e))}});

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,817 @@
/* Flot plugin for rendering pie charts.
Copyright (c) 2007-2013 IOLA and Ole Laursen.
Licensed under the MIT license.
The plugin assumes that each series has a single data value, and that each
value is a positive integer or zero. Negative numbers don't make sense for a
pie chart, and have unpredictable results. The values do NOT need to be
passed in as percentages; the plugin will calculate the total and per-slice
percentages internally.
* Created by Brian Medendorp
* Updated with contributions from btburnett3, Anthony Aragues and Xavi Ivars
The plugin supports these options:
series: {
pie: {
show: true/false
radius: 0-1 for percentage of fullsize, or a specified pixel length, or 'auto'
innerRadius: 0-1 for percentage of fullsize or a specified pixel length, for creating a donut effect
startAngle: 0-2 factor of PI used for starting angle (in radians) i.e 3/2 starts at the top, 0 and 2 have the same result
tilt: 0-1 for percentage to tilt the pie, where 1 is no tilt, and 0 is completely flat (nothing will show)
offset: {
top: integer value to move the pie up or down
left: integer value to move the pie left or right, or 'auto'
},
stroke: {
color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#FFF')
width: integer pixel width of the stroke
},
label: {
show: true/false, or 'auto'
formatter: a user-defined function that modifies the text/style of the label text
radius: 0-1 for percentage of fullsize, or a specified pixel length
background: {
color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#000')
opacity: 0-1
},
threshold: 0-1 for the percentage value at which to hide labels (if they're too small)
},
combine: {
threshold: 0-1 for the percentage value at which to combine slices (if they're too small)
color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#CCC'), if null, the plugin will automatically use the color of the first slice to be combined
label: any text value of what the combined slice should be labeled
}
highlight: {
opacity: 0-1
}
}
}
More detail and specific examples can be found in the included HTML file.
*/
(function($) {
// Maximum redraw attempts when fitting labels within the plot
var REDRAW_ATTEMPTS = 10;
// Factor by which to shrink the pie when fitting labels within the plot
var REDRAW_SHRINK = 0.95;
function init(plot) {
var canvas = null,
target = null,
maxRadius = null,
centerLeft = null,
centerTop = null,
processed = false,
ctx = null;
// interactive variables
var highlights = [];
// add hook to determine if pie plugin in enabled, and then perform necessary operations
plot.hooks.processOptions.push(function(plot, options) {
if (options.series.pie.show) {
options.grid.show = false;
// set labels.show
if (options.series.pie.label.show == "auto") {
if (options.legend.show) {
options.series.pie.label.show = false;
} else {
options.series.pie.label.show = true;
}
}
// set radius
if (options.series.pie.radius == "auto") {
if (options.series.pie.label.show) {
options.series.pie.radius = 3/4;
} else {
options.series.pie.radius = 1;
}
}
// ensure sane tilt
if (options.series.pie.tilt > 1) {
options.series.pie.tilt = 1;
} else if (options.series.pie.tilt < 0) {
options.series.pie.tilt = 0;
}
}
});
plot.hooks.bindEvents.push(function(plot, eventHolder) {
var options = plot.getOptions();
if (options.series.pie.show) {
if (options.grid.hoverable) {
eventHolder.off("mousemove").on( 'mousemove', onMouseMove );
}
if (options.grid.clickable) {
eventHolder.off("click").on( 'click', onClick );
}
}
});
plot.hooks.processDatapoints.push(function(plot, series, data, datapoints) {
var options = plot.getOptions();
if (options.series.pie.show) {
processDatapoints(plot, series, data, datapoints);
}
});
plot.hooks.drawOverlay.push(function(plot, octx) {
var options = plot.getOptions();
if (options.series.pie.show) {
drawOverlay(plot, octx);
}
});
plot.hooks.draw.push(function(plot, newCtx) {
var options = plot.getOptions();
if (options.series.pie.show) {
draw(plot, newCtx);
}
});
function processDatapoints(plot, series, datapoints) {
if (!processed) {
processed = true;
canvas = plot.getCanvas();
target = $(canvas).parent();
options = plot.getOptions();
plot.setData(combine(plot.getData()));
}
}
function combine(data) {
var total = 0,
combined = 0,
numCombined = 0,
color = options.series.pie.combine.color,
newdata = [];
// Fix up the raw data from Flot, ensuring the data is numeric
for (var i = 0; i < data.length; ++i) {
var value = data[i].data;
// If the data is an array, we'll assume that it's a standard
// Flot x-y pair, and are concerned only with the second value.
// Note how we use the original array, rather than creating a
// new one; this is more efficient and preserves any extra data
// that the user may have stored in higher indexes.
if (Array.isArray(value) && value.length == 1) {
value = value[0];
}
if (Array.isArray(value)) {
// Equivalent to $.isNumeric() but compatible with jQuery < 1.7
if (!isNaN(parseFloat(value[1])) && isFinite(value[1])) {
value[1] = +value[1];
} else {
value[1] = 0;
}
} else if (!isNaN(parseFloat(value)) && isFinite(value)) {
value = [1, +value];
} else {
value = [1, 0];
}
data[i].data = [value];
}
// Sum up all the slices, so we can calculate percentages for each
for (var i = 0; i < data.length; ++i) {
total += data[i].data[0][1];
}
// Count the number of slices with percentages below the combine
// threshold; if it turns out to be just one, we won't combine.
for (var i = 0; i < data.length; ++i) {
var value = data[i].data[0][1];
if (value / total <= options.series.pie.combine.threshold) {
combined += value;
numCombined++;
if (!color) {
color = data[i].color;
}
}
}
for (var i = 0; i < data.length; ++i) {
var value = data[i].data[0][1];
if (numCombined < 2 || value / total > options.series.pie.combine.threshold) {
newdata.push({
data: [[1, value]],
color: data[i].color,
label: data[i].label,
angle: value * Math.PI * 2 / total,
percent: value / (total / 100)
});
}
}
if (numCombined > 1) {
newdata.push({
data: [[1, combined]],
color: color,
label: options.series.pie.combine.label,
angle: combined * Math.PI * 2 / total,
percent: combined / (total / 100)
});
}
return newdata;
}
function draw(plot, newCtx) {
if (!target) {
return; // if no series were passed
}
var canvasWidth = plot.getPlaceholder().width(),
canvasHeight = plot.getPlaceholder().height(),
legendWidth = target.children().filter(".legend").children().width() || 0;
ctx = newCtx;
// WARNING: HACK! REWRITE THIS CODE AS SOON AS POSSIBLE!
// When combining smaller slices into an 'other' slice, we need to
// add a new series. Since Flot gives plugins no way to modify the
// list of series, the pie plugin uses a hack where the first call
// to processDatapoints results in a call to setData with the new
// list of series, then subsequent processDatapoints do nothing.
// The plugin-global 'processed' flag is used to control this hack;
// it starts out false, and is set to true after the first call to
// processDatapoints.
// Unfortunately this turns future setData calls into no-ops; they
// call processDatapoints, the flag is true, and nothing happens.
// To fix this we'll set the flag back to false here in draw, when
// all series have been processed, so the next sequence of calls to
// processDatapoints once again starts out with a slice-combine.
// This is really a hack; in 0.9 we need to give plugins a proper
// way to modify series before any processing begins.
processed = false;
// calculate maximum radius and center point
maxRadius = Math.min(canvasWidth, canvasHeight / options.series.pie.tilt) / 2;
centerTop = canvasHeight / 2 + options.series.pie.offset.top;
centerLeft = canvasWidth / 2;
if (options.series.pie.offset.left == "auto") {
if (options.legend.position.match("w")) {
centerLeft += legendWidth / 2;
} else {
centerLeft -= legendWidth / 2;
}
} else {
centerLeft += options.series.pie.offset.left;
}
if (centerLeft < maxRadius) {
centerLeft = maxRadius;
} else if (centerLeft > canvasWidth - maxRadius) {
centerLeft = canvasWidth - maxRadius;
}
var slices = plot.getData(),
attempts = 0;
// Keep shrinking the pie's radius until drawPie returns true,
// indicating that all the labels fit, or we try too many times.
do {
if (attempts > 0) {
maxRadius *= REDRAW_SHRINK;
}
attempts += 1;
clear();
if (options.series.pie.tilt <= 0.8) {
drawShadow();
}
} while (!drawPie() && attempts < REDRAW_ATTEMPTS)
if (attempts >= REDRAW_ATTEMPTS) {
clear();
target.prepend("<div class='error'>Could not draw pie with labels contained inside canvas</div>");
}
if (plot.setSeries && plot.insertLegend) {
plot.setSeries(slices);
plot.insertLegend();
}
// we're actually done at this point, just defining internal functions at this point
function clear() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
target.children().filter(".pieLabel, .pieLabelBackground").remove();
}
function drawShadow() {
var shadowLeft = options.series.pie.shadow.left;
var shadowTop = options.series.pie.shadow.top;
var edge = 10;
var alpha = options.series.pie.shadow.alpha;
var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
if (radius >= canvasWidth / 2 - shadowLeft || radius * options.series.pie.tilt >= canvasHeight / 2 - shadowTop || radius <= edge) {
return; // shadow would be outside canvas, so don't draw it
}
ctx.save();
ctx.translate(shadowLeft,shadowTop);
ctx.globalAlpha = alpha;
ctx.fillStyle = "#000";
// center and rotate to starting position
ctx.translate(centerLeft,centerTop);
ctx.scale(1, options.series.pie.tilt);
//radius -= edge;
for (var i = 1; i <= edge; i++) {
ctx.beginPath();
ctx.arc(0, 0, radius, 0, Math.PI * 2, false);
ctx.fill();
radius -= i;
}
ctx.restore();
}
function drawPie() {
var startAngle = Math.PI * options.series.pie.startAngle;
var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
// center and rotate to starting position
ctx.save();
ctx.translate(centerLeft,centerTop);
ctx.scale(1, options.series.pie.tilt);
//ctx.rotate(startAngle); // start at top; -- This doesn't work properly in Opera
// draw slices
ctx.save();
var currentAngle = startAngle;
for (var i = 0; i < slices.length; ++i) {
slices[i].startAngle = currentAngle;
drawSlice(slices[i].angle, slices[i].color, true);
}
ctx.restore();
// draw slice outlines
if (options.series.pie.stroke.width > 0) {
ctx.save();
ctx.lineWidth = options.series.pie.stroke.width;
currentAngle = startAngle;
for (var i = 0; i < slices.length; ++i) {
drawSlice(slices[i].angle, options.series.pie.stroke.color, false);
}
ctx.restore();
}
// draw donut hole
drawDonutHole(ctx);
ctx.restore();
// Draw the labels, returning true if they fit within the plot
if (options.series.pie.label.show) {
return drawLabels();
} else return true;
function drawSlice(angle, color, fill) {
if (angle <= 0 || isNaN(angle)) {
return;
}
if (fill) {
ctx.fillStyle = color;
} else {
ctx.strokeStyle = color;
ctx.lineJoin = "round";
}
ctx.beginPath();
if (Math.abs(angle - Math.PI * 2) > 0.000000001) {
ctx.moveTo(0, 0); // Center of the pie
}
//ctx.arc(0, 0, radius, 0, angle, false); // This doesn't work properly in Opera
ctx.arc(0, 0, radius,currentAngle, currentAngle + angle / 2, false);
ctx.arc(0, 0, radius,currentAngle + angle / 2, currentAngle + angle, false);
ctx.closePath();
//ctx.rotate(angle); // This doesn't work properly in Opera
currentAngle += angle;
if (fill) {
ctx.fill();
} else {
ctx.stroke();
}
}
function drawLabels() {
var currentAngle = startAngle;
var radius = options.series.pie.label.radius > 1 ? options.series.pie.label.radius : maxRadius * options.series.pie.label.radius;
for (var i = 0; i < slices.length; ++i) {
if (slices[i].percent >= options.series.pie.label.threshold * 100) {
if (!drawLabel(slices[i], currentAngle, i)) {
return false;
}
}
currentAngle += slices[i].angle;
}
return true;
function drawLabel(slice, startAngle, index) {
if (slice.data[0][1] == 0) {
return true;
}
// format label text
var lf = options.legend.labelFormatter, text, plf = options.series.pie.label.formatter;
if (lf) {
text = lf(slice.label, slice);
} else {
text = slice.label;
}
if (plf) {
text = plf(text, slice);
}
var halfAngle = ((startAngle + slice.angle) + startAngle) / 2;
var x = centerLeft + Math.round(Math.cos(halfAngle) * radius);
var y = centerTop + Math.round(Math.sin(halfAngle) * radius) * options.series.pie.tilt;
var html = "<span class='pieLabel' id='pieLabel" + index + "' style='position:absolute;top:" + y + "px;left:" + x + "px;'>" + text + "</span>";
target.append(html);
var label = target.children("#pieLabel" + index);
var labelTop = (y - label.height() / 2);
var labelLeft = (x - label.width() / 2);
label.css("top", labelTop);
label.css("left", labelLeft);
// check to make sure that the label is not outside the canvas
if (0 - labelTop > 0 || 0 - labelLeft > 0 || canvasHeight - (labelTop + label.height()) < 0 || canvasWidth - (labelLeft + label.width()) < 0) {
return false;
}
if (options.series.pie.label.background.opacity != 0) {
// put in the transparent background separately to avoid blended labels and label boxes
var c = options.series.pie.label.background.color;
if (c == null) {
c = slice.color;
}
var pos = "top:" + labelTop + "px;left:" + labelLeft + "px;";
$("<div class='pieLabelBackground' style='position:absolute;width:" + label.width() + "px;height:" + label.height() + "px;" + pos + "background-color:" + c + ";'></div>")
.css("opacity", options.series.pie.label.background.opacity)
.insertBefore(label);
}
return true;
} // end individual label function
} // end drawLabels function
} // end drawPie function
} // end draw function
// Placed here because it needs to be accessed from multiple locations
function drawDonutHole(layer) {
if (options.series.pie.innerRadius > 0) {
// subtract the center
layer.save();
var innerRadius = options.series.pie.innerRadius > 1 ? options.series.pie.innerRadius : maxRadius * options.series.pie.innerRadius;
layer.globalCompositeOperation = "destination-out"; // this does not work with excanvas, but it will fall back to using the stroke color
layer.beginPath();
layer.fillStyle = options.series.pie.stroke.color;
layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
layer.fill();
layer.closePath();
layer.restore();
// add inner stroke
layer.save();
layer.beginPath();
layer.strokeStyle = options.series.pie.stroke.color;
layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
layer.stroke();
layer.closePath();
layer.restore();
// TODO: add extra shadow inside hole (with a mask) if the pie is tilted.
}
}
//-- Additional Interactive related functions --
function isPointInPoly(poly, pt) {
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) || (poly[j][1] <= pt[1] && pt[1]< poly[i][1]))
&& (pt[0] < (poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0])
&& (c = !c);
return c;
}
function findNearbySlice(mouseX, mouseY) {
var slices = plot.getData(),
options = plot.getOptions(),
radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius,
x, y;
for (var i = 0; i < slices.length; ++i) {
var s = slices[i];
if (s.pie.show) {
ctx.save();
ctx.beginPath();
ctx.moveTo(0, 0); // Center of the pie
//ctx.scale(1, options.series.pie.tilt); // this actually seems to break everything when here.
ctx.arc(0, 0, radius, s.startAngle, s.startAngle + s.angle / 2, false);
ctx.arc(0, 0, radius, s.startAngle + s.angle / 2, s.startAngle + s.angle, false);
ctx.closePath();
x = mouseX - centerLeft;
y = mouseY - centerTop;
if (ctx.isPointInPath) {
if (ctx.isPointInPath(mouseX - centerLeft, mouseY - centerTop)) {
ctx.restore();
return {
datapoint: [s.percent, s.data],
dataIndex: 0,
series: s,
seriesIndex: i
};
}
} else {
// excanvas for IE doesn;t support isPointInPath, this is a workaround.
var p1X = radius * Math.cos(s.startAngle),
p1Y = radius * Math.sin(s.startAngle),
p2X = radius * Math.cos(s.startAngle + s.angle / 4),
p2Y = radius * Math.sin(s.startAngle + s.angle / 4),
p3X = radius * Math.cos(s.startAngle + s.angle / 2),
p3Y = radius * Math.sin(s.startAngle + s.angle / 2),
p4X = radius * Math.cos(s.startAngle + s.angle / 1.5),
p4Y = radius * Math.sin(s.startAngle + s.angle / 1.5),
p5X = radius * Math.cos(s.startAngle + s.angle),
p5Y = radius * Math.sin(s.startAngle + s.angle),
arrPoly = [[0, 0], [p1X, p1Y], [p2X, p2Y], [p3X, p3Y], [p4X, p4Y], [p5X, p5Y]],
arrPoint = [x, y];
// TODO: perhaps do some mathmatical trickery here with the Y-coordinate to compensate for pie tilt?
if (isPointInPoly(arrPoly, arrPoint)) {
ctx.restore();
return {
datapoint: [s.percent, s.data],
dataIndex: 0,
series: s,
seriesIndex: i
};
}
}
ctx.restore();
}
}
return null;
}
function onMouseMove(e) {
triggerClickHoverEvent("plothover", e);
}
function onClick(e) {
triggerClickHoverEvent("plotclick", e);
}
// trigger click or hover event (they send the same parameters so we share their code)
function triggerClickHoverEvent(eventname, e) {
var offset = plot.offset();
var canvasX = parseInt(e.pageX - offset.left);
var canvasY = parseInt(e.pageY - offset.top);
var item = findNearbySlice(canvasX, canvasY);
if (options.grid.autoHighlight) {
// clear auto-highlights
for (var i = 0; i < highlights.length; ++i) {
var h = highlights[i];
if (h.auto == eventname && !(item && h.series == item.series)) {
unhighlight(h.series);
}
}
}
// highlight the slice
if (item) {
highlight(item.series, eventname);
}
// trigger any hover bind events
var pos = { pageX: e.pageX, pageY: e.pageY };
target.trigger(eventname, [pos, item]);
}
function highlight(s, auto) {
//if (typeof s == "number") {
// s = series[s];
//}
var i = indexOfHighlight(s);
if (i == -1) {
highlights.push({ series: s, auto: auto });
plot.triggerRedrawOverlay();
} else if (!auto) {
highlights[i].auto = false;
}
}
function unhighlight(s) {
if (s == null) {
highlights = [];
plot.triggerRedrawOverlay();
}
//if (typeof s == "number") {
// s = series[s];
//}
var i = indexOfHighlight(s);
if (i != -1) {
highlights.splice(i, 1);
plot.triggerRedrawOverlay();
}
}
function indexOfHighlight(s) {
for (var i = 0; i < highlights.length; ++i) {
var h = highlights[i];
if (h.series == s)
return i;
}
return -1;
}
function drawOverlay(plot, octx) {
var options = plot.getOptions();
var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
octx.save();
octx.translate(centerLeft, centerTop);
octx.scale(1, options.series.pie.tilt);
for (var i = 0; i < highlights.length; ++i) {
drawHighlight(highlights[i].series);
}
drawDonutHole(octx);
octx.restore();
function drawHighlight(series) {
if (series.angle <= 0 || isNaN(series.angle)) {
return;
}
//octx.fillStyle = parseColor(options.series.pie.highlight.color).scale(null, null, null, options.series.pie.highlight.opacity).toString();
octx.fillStyle = "rgba(255, 255, 255, " + options.series.pie.highlight.opacity + ")"; // this is temporary until we have access to parseColor
octx.beginPath();
if (Math.abs(series.angle - Math.PI * 2) > 0.000000001) {
octx.moveTo(0, 0); // Center of the pie
}
octx.arc(0, 0, radius, series.startAngle, series.startAngle + series.angle / 2, false);
octx.arc(0, 0, radius, series.startAngle + series.angle / 2, series.startAngle + series.angle, false);
octx.closePath();
octx.fill();
}
}
} // end init (plugin body)
// define pie specific options and their default values
var options = {
series: {
pie: {
show: false,
radius: "auto", // actual radius of the visible pie (based on full calculated radius if <=1, or hard pixel value)
innerRadius: 0, /* for donut */
startAngle: 3/2,
tilt: 1,
shadow: {
left: 5, // shadow left offset
top: 15, // shadow top offset
alpha: 0.02 // shadow alpha
},
offset: {
top: 0,
left: "auto"
},
stroke: {
color: "#fff",
width: 1
},
label: {
show: "auto",
formatter: function(label, slice) {
return "<div style='font-size:x-small;text-align:center;padding:2px;color:" + slice.color + ";'>" + label + "<br/>" + Math.round(slice.percent) + "%</div>";
}, // formatter function
radius: 1, // radius at which to place the labels (based on full calculated radius if <=1, or hard pixel value)
background: {
color: null,
opacity: 0
},
threshold: 0 // percentage at which to hide the label (i.e. the slice is too narrow)
},
combine: {
threshold: -1, // percentage at which to combine little slices into one larger slice
color: null, // color to give the new slice (auto-generated if null)
label: "Other" // label to give the new slice
},
highlight: {
//color: "#fff", // will add this functionality once parseColor is available
opacity: 0.5
}
}
}
};
$.plot.plugins.push({
init: init,
options: options,
name: "pie",
version: "1.1"
});
})(jQuery);

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show More