ES5 vars-on-top
This commit is contained in:
@ -46,7 +46,6 @@
|
||||
"camelcase": 0,
|
||||
"padded-blocks": 0,
|
||||
"strict": 0,
|
||||
"vars-on-top": 0,
|
||||
"no-var": 0,
|
||||
"no-unused-vars": 0,
|
||||
"object-shorthand": 0,
|
||||
|
@ -2,11 +2,10 @@ function requestFailed(errorMessage, xhr) {
|
||||
if (xhr.responseJSON) {
|
||||
return xhr.responseJSON;
|
||||
}
|
||||
var message = errorMessage.replace('%d', xhr.status);
|
||||
return {
|
||||
errors: [
|
||||
{
|
||||
message: message
|
||||
message: errorMessage.replace('%d', xhr.status)
|
||||
}
|
||||
]
|
||||
};
|
||||
@ -53,12 +52,14 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function (mp, jQuery, _) {
|
||||
data: this.options.data || {}
|
||||
};
|
||||
},
|
||||
request: function (method, options) {
|
||||
request: function(method, options) {
|
||||
var params;
|
||||
var deferred;
|
||||
// set options
|
||||
this.init(options);
|
||||
|
||||
// set request params
|
||||
var params = this.getParams();
|
||||
params = this.getParams();
|
||||
|
||||
// remove null values from the data object
|
||||
if (_.isObject(params.data)) {
|
||||
@ -68,7 +69,7 @@ define('ajax', ['mailpoet', 'jquery', 'underscore'], function (mp, jQuery, _) {
|
||||
}
|
||||
|
||||
// ajax request
|
||||
var deferred = jQuery.post(
|
||||
deferred = jQuery.post(
|
||||
this.options.url,
|
||||
params,
|
||||
null,
|
||||
|
@ -43,9 +43,10 @@ define('date',
|
||||
},
|
||||
format: function (date, opts) {
|
||||
var options = opts || {};
|
||||
var momentDate;
|
||||
this.init(options);
|
||||
|
||||
var momentDate = Moment(date, this.convertFormat(options.parseFormat));
|
||||
momentDate = Moment(date, this.convertFormat(options.parseFormat));
|
||||
if (options.offset === 0) momentDate = momentDate.utc();
|
||||
return momentDate.format(this.convertFormat(this.options.format));
|
||||
},
|
||||
@ -70,7 +71,12 @@ define('date',
|
||||
format: 'H:i:s'
|
||||
});
|
||||
},
|
||||
convertFormat: function (format) {
|
||||
convertFormat: function(format) {
|
||||
var replacements;
|
||||
var convertedFormat;
|
||||
var escapeToken;
|
||||
var index;
|
||||
var token;
|
||||
var format_mappings = {
|
||||
date: {
|
||||
d: 'DD',
|
||||
@ -140,12 +146,11 @@ define('date',
|
||||
|
||||
if (!format || format.length <= 0) return format;
|
||||
|
||||
var replacements = format_mappings['date'];
|
||||
replacements = format_mappings['date'];
|
||||
convertedFormat = [];
|
||||
escapeToken = false;
|
||||
|
||||
var convertedFormat = [];
|
||||
var escapeToken = false;
|
||||
|
||||
for (var index = 0, token = ''; format.charAt(index); index += 1) {
|
||||
for(index = 0, token = ''; format.charAt(index); index += 1){
|
||||
token = format.charAt(index);
|
||||
if (escapeToken === true) {
|
||||
convertedFormat.push('[' + token + ']');
|
||||
|
@ -7,6 +7,10 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var Observable;
|
||||
var WysijaHistory;
|
||||
var WysijaForm;
|
||||
|
||||
Event.cacheDelegated = {};
|
||||
Object.extend(document, (function () {
|
||||
var cache = Event.cacheDelegated;
|
||||
@ -30,20 +34,22 @@ Object.extend(document, (function () {
|
||||
}
|
||||
|
||||
function destroyWrapper(selector, eventName, handler) {
|
||||
var wrapper;
|
||||
var c = getCacheForSelector(selector);
|
||||
if (!c[eventName]) return false;
|
||||
var wrapper = findWrapper(selector, eventName, handler);
|
||||
if(!c[eventName]) return false;
|
||||
wrapper = findWrapper(selector, eventName, handler);
|
||||
c[eventName] = c[eventName].without(wrapper);
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
function createWrapper(selector, eventName, handler, context) {
|
||||
var wrapper;
|
||||
var element;
|
||||
var c = getWrappersForSelector(selector, eventName);
|
||||
if(c.pluck('handler').include(handler)) return false;
|
||||
wrapper = function(event) {
|
||||
var element = event.findElement(selector);
|
||||
if (element) handler.call(context || element, event, element);
|
||||
element = event.findElement(selector);
|
||||
if(element) handler.call(context || element, event, element);
|
||||
};
|
||||
wrapper.handler = handler;
|
||||
c.push(wrapper);
|
||||
@ -57,7 +63,8 @@ Object.extend(document, (function () {
|
||||
},
|
||||
stopDelegating: function (selector, eventName, handler) {
|
||||
var length = arguments.length;
|
||||
switch (length) {
|
||||
var wrapper;
|
||||
switch(length) {
|
||||
case 2:
|
||||
getWrappersForSelector(selector, eventName).each(function (wrapper) {
|
||||
document.stopDelegating(selector, eventName, wrapper.handler);
|
||||
@ -74,15 +81,15 @@ Object.extend(document, (function () {
|
||||
});
|
||||
break;
|
||||
default:
|
||||
var wrapper = destroyWrapper.apply(null, arguments);
|
||||
if (wrapper) document.stopObserving(eventName, wrapper);
|
||||
wrapper = destroyWrapper.apply(null, arguments);
|
||||
if(wrapper) document.stopObserving(eventName, wrapper);
|
||||
}
|
||||
return document;
|
||||
}
|
||||
};
|
||||
})());
|
||||
|
||||
var Observable = (function () {
|
||||
Observable = (function() {
|
||||
function getEventName(nameA, namespace) {
|
||||
var name = nameA.substring(2);
|
||||
if (namespace) name = namespace + ':' + name;
|
||||
@ -111,12 +118,12 @@ var Observable = (function () {
|
||||
});
|
||||
}
|
||||
return {
|
||||
observe: function (selector) {
|
||||
if (!this.handlers) this.handlers = {};
|
||||
if (this.handlers[selector]) return;
|
||||
observe: function(selector) {
|
||||
var klass = this;
|
||||
if (this.prototype.onDomLoaded) {
|
||||
if (document.loaded) {
|
||||
if(!this.handlers) this.handlers = {};
|
||||
if(this.handlers[selector]) return;
|
||||
if(this.prototype.onDomLoaded) {
|
||||
if(document.loaded) {
|
||||
onDomLoad(selector, klass);
|
||||
} else {
|
||||
document.observe('dom:loaded', onDomLoad.curry(selector, klass));
|
||||
@ -147,9 +154,9 @@ Object.extend(window.Droppables, {
|
||||
return proceed(drop);
|
||||
}),
|
||||
show: function(point, element) {
|
||||
if(!this.drops.length) return;
|
||||
var drop;
|
||||
var affected = [];
|
||||
if(!this.drops.length) return;
|
||||
this.drops.each(function(drop) {
|
||||
if(window.Droppables.isAffected(point, element, drop)) affected.push(drop);
|
||||
});
|
||||
@ -197,7 +204,7 @@ Object.extend(window.Droppables, {
|
||||
- set a maximum number of items to be stored
|
||||
|
||||
*/
|
||||
var WysijaHistory = {
|
||||
WysijaHistory = {
|
||||
container: 'mailpoet_form_history',
|
||||
size: 30,
|
||||
enqueue: function (element) {
|
||||
@ -241,7 +248,7 @@ var WysijaHistory = {
|
||||
};
|
||||
|
||||
/* MailPoet Form */
|
||||
var WysijaForm = {
|
||||
WysijaForm = {
|
||||
version: '0.7',
|
||||
options: {
|
||||
container: 'mailpoet_form_container',
|
||||
@ -293,8 +300,9 @@ var WysijaForm = {
|
||||
WysijaForm.Block.create(block, window.$('block_placeholder'));
|
||||
});
|
||||
},
|
||||
load: function (data) {
|
||||
if (data === undefined) return;
|
||||
load: function(data) {
|
||||
var settings_elements;
|
||||
if(data === undefined) return;
|
||||
|
||||
// load body
|
||||
if (data.body !== undefined) {
|
||||
@ -304,8 +312,8 @@ var WysijaForm = {
|
||||
});
|
||||
|
||||
// load settings
|
||||
var settings_elements = window.$('mailpoet_form_settings').getElements();
|
||||
settings_elements.each(function (setting) {
|
||||
settings_elements = window.$('mailpoet_form_settings').getElements();
|
||||
settings_elements.each(function(setting) {
|
||||
// skip lists
|
||||
if (setting.name === 'segments') {
|
||||
return true;
|
||||
@ -402,7 +410,8 @@ var WysijaForm = {
|
||||
}
|
||||
return data;
|
||||
},
|
||||
toggleWidgets: function () {
|
||||
toggleWidgets: function() {
|
||||
var hasSegmentSelection;
|
||||
window.$$('a[wysija_unique="1"]').invoke('removeClassName', 'disabled');
|
||||
|
||||
// loop through each unique field already inserted in the editor and disable its toolbar equivalent
|
||||
@ -413,7 +422,7 @@ var WysijaForm = {
|
||||
}
|
||||
});
|
||||
|
||||
var hasSegmentSelection = WysijaForm.hasSegmentSelection();
|
||||
hasSegmentSelection = WysijaForm.hasSegmentSelection();
|
||||
|
||||
if (hasSegmentSelection) {
|
||||
window.$('mailpoet_form_segments').writeAttribute('required', false).disable();
|
||||
@ -428,8 +437,9 @@ var WysijaForm = {
|
||||
},
|
||||
isSegmentSelectionValid: function () {
|
||||
var segment_selection = window.$$('#' + WysijaForm.options.editor + ' [wysija_id="segments"]')[0];
|
||||
if (segment_selection !== undefined) {
|
||||
var block = WysijaForm.get(segment_selection).block.getData();
|
||||
var block;
|
||||
if(segment_selection !== undefined) {
|
||||
block = WysijaForm.get(segment_selection).block.getData();
|
||||
return (
|
||||
(block.params.values !== undefined)
|
||||
&&
|
||||
@ -438,12 +448,14 @@ var WysijaForm = {
|
||||
}
|
||||
return false;
|
||||
},
|
||||
setBlockPositions: function (event, target) {
|
||||
setBlockPositions: function(event, target) {
|
||||
var index = 1;
|
||||
var block_placeholder;
|
||||
var previous_placeholder;
|
||||
// release dragging lock
|
||||
WysijaForm.locks.dragging = false;
|
||||
|
||||
var index = 1;
|
||||
WysijaForm.getBlocks().each(function (container) {
|
||||
WysijaForm.getBlocks().each(function(container) {
|
||||
container.setPosition(index++);
|
||||
// remove z-index value to avoid issues when resizing images
|
||||
if (container['block'] !== undefined) {
|
||||
@ -455,8 +467,8 @@ var WysijaForm = {
|
||||
|
||||
if (target !== undefined) {
|
||||
// get placeholders (previous placeholder matches the placeholder linked to the next block)
|
||||
var block_placeholder = window.$(target.element.readAttribute('wysija_placeholder'));
|
||||
var previous_placeholder = target.element.previous('.block_placeholder');
|
||||
block_placeholder = window.$(target.element.readAttribute('wysija_placeholder'));
|
||||
previous_placeholder = target.element.previous('.block_placeholder');
|
||||
|
||||
if (block_placeholder !== null) {
|
||||
// put block placeholder before the current block
|
||||
@ -491,12 +503,15 @@ var WysijaForm = {
|
||||
var is_visible = (parentPos.top <= (WysijaForm.scroll.top + viewportHeight));
|
||||
var buttonMargin = 5;
|
||||
var relativeTop = buttonMargin;
|
||||
var absoluteTop;
|
||||
var parentTop;
|
||||
var parentBottom;
|
||||
|
||||
if (is_visible) {
|
||||
// desired position is set to center of viewport
|
||||
var absoluteTop = parseInt(WysijaForm.scroll.top + ((viewportHeight / 2) - (element.getHeight() / 2)), 10);
|
||||
var parentTop = parseInt(parentPos.top - blockPadding, 10);
|
||||
var parentBottom = parseInt(parentPos.top + parentDim.height - blockPadding, 10);
|
||||
absoluteTop = parseInt(WysijaForm.scroll.top + ((viewportHeight / 2) - (element.getHeight() / 2)), 10);
|
||||
parentTop = parseInt(parentPos.top - blockPadding, 10);
|
||||
parentBottom = parseInt(parentPos.top + parentDim.height - blockPadding, 10);
|
||||
|
||||
// always center
|
||||
relativeTop = parseInt((parentDim.height / 2) - (element.getHeight() / 2), 10);
|
||||
@ -520,10 +535,11 @@ var WysijaForm = {
|
||||
if (WysijaForm.toolbar.x === null) WysijaForm.toolbar.x = parseInt(WysijaForm.toolbar.left + window.$(WysijaForm.options.container).getDimensions().width + 15);
|
||||
|
||||
},
|
||||
setToolbarPosition: function () {
|
||||
setToolbarPosition: function() {
|
||||
var position;
|
||||
WysijaForm.initToolbarPosition();
|
||||
|
||||
var position = {
|
||||
position = {
|
||||
top: WysijaForm.toolbar.y + 'px',
|
||||
visibility: 'visible'
|
||||
};
|
||||
@ -587,10 +603,12 @@ var WysijaForm = {
|
||||
instances: {},
|
||||
get: function (element, typ) {
|
||||
var type = typ;
|
||||
if (type === undefined) type = 'block';
|
||||
var id;
|
||||
var instance;
|
||||
if(type === undefined) type = 'block';
|
||||
// identify element
|
||||
var id = element.identify();
|
||||
var instance = WysijaForm.instances[id] || new WysijaForm[type.capitalize().camelize()](id);
|
||||
id = element.identify();
|
||||
instance = WysijaForm.instances[id] || new WysijaForm[type.capitalize().camelize()](id);
|
||||
|
||||
WysijaForm.instances[id] = instance;
|
||||
return instance;
|
||||
@ -768,16 +786,18 @@ WysijaForm.Block = window.Class.create({
|
||||
}
|
||||
}
|
||||
},
|
||||
makeBlockDroppable: function () {
|
||||
if (this.isBlockDroppableEnabled() === false) {
|
||||
var block_placeholder = this.getBlockDroppable();
|
||||
makeBlockDroppable: function() {
|
||||
var block_placeholder;
|
||||
if(this.isBlockDroppableEnabled() === false) {
|
||||
block_placeholder = this.getBlockDroppable();
|
||||
window.Droppables.add(block_placeholder.identify(), WysijaForm.blockDropOptions);
|
||||
block_placeholder.addClassName('enabled');
|
||||
}
|
||||
},
|
||||
removeBlockDroppable: function () {
|
||||
if (this.isBlockDroppableEnabled()) {
|
||||
var block_placeholder = this.getBlockDroppable();
|
||||
removeBlockDroppable: function() {
|
||||
var block_placeholder;
|
||||
if(this.isBlockDroppableEnabled()) {
|
||||
block_placeholder = this.getBlockDroppable();
|
||||
window.Droppables.remove(block_placeholder.identify());
|
||||
block_placeholder.removeClassName('enabled');
|
||||
}
|
||||
@ -808,7 +828,9 @@ WysijaForm.Block = window.Class.create({
|
||||
getControls: function () {
|
||||
return this.element.down('.wysija_controls');
|
||||
},
|
||||
setupControls: function () {
|
||||
setupControls: function() {
|
||||
var block;
|
||||
var field;
|
||||
// enable controls
|
||||
this.controls = this.getControls();
|
||||
|
||||
@ -859,9 +881,9 @@ WysijaForm.Block = window.Class.create({
|
||||
if (this.settingsButton !== null) {
|
||||
this.settingsButton.observe('click', function (event) {
|
||||
// TODO: refactor
|
||||
var block = window.$(event.target).up('.mailpoet_form_block') || null;
|
||||
if (block !== null) {
|
||||
var field = WysijaForm.getFieldData(block);
|
||||
block = window.$(event.target).up('.mailpoet_form_block') || null;
|
||||
if(block !== null) {
|
||||
field = WysijaForm.getFieldData(block);
|
||||
this.editSettings();
|
||||
}
|
||||
}.bind(this));
|
||||
@ -907,20 +929,26 @@ WysijaForm.Block = window.Class.create({
|
||||
/* Invoked on item dropped */
|
||||
WysijaForm.Block.create = function (createBlock, target) {
|
||||
var block = createBlock;
|
||||
if (window.$('form_template_' + block.type) === null) {
|
||||
var body;
|
||||
var block_template;
|
||||
var template;
|
||||
var output;
|
||||
var settings_segments;
|
||||
var element;
|
||||
if(window.$('form_template_' + block.type) === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var body = window.$(WysijaForm.options.body);
|
||||
var block_template = window.Handlebars.compile(window.$('form_template_block').innerHTML);
|
||||
var template = window.Handlebars.compile(window.$('form_template_' + block.type).innerHTML);
|
||||
var output = '';
|
||||
body = window.$(WysijaForm.options.body);
|
||||
block_template = window.Handlebars.compile(window.$('form_template_block').innerHTML);
|
||||
template = window.Handlebars.compile(window.$('form_template_' + block.type).innerHTML);
|
||||
output = '';
|
||||
|
||||
if (block.type === 'segment') {
|
||||
if (block.params.values === undefined) {
|
||||
var settings_segments = window.jQuery('#mailpoet_form_segments').val();
|
||||
if (settings_segments !== null && settings_segments.length > 0) {
|
||||
block.params.values = window.mailpoet_segments.filter(function (segment) {
|
||||
if(block.type === 'segment') {
|
||||
if(block.params.values === undefined) {
|
||||
settings_segments = window.jQuery('#mailpoet_form_segments').val();
|
||||
if(settings_segments !== null && settings_segments.length > 0){
|
||||
block.params.values = window.mailpoet_segments.filter(function(segment) {
|
||||
return (settings_segments.indexOf(segment.id) !== -1);
|
||||
});
|
||||
}
|
||||
@ -938,8 +966,8 @@ WysijaForm.Block.create = function (createBlock, target) {
|
||||
}
|
||||
|
||||
// if the drop target was the bottom placeholder
|
||||
var element = null;
|
||||
if (target.identify() === 'block_placeholder') {
|
||||
element = null;
|
||||
if(target.identify() === 'block_placeholder') {
|
||||
// insert block at the bottom
|
||||
element = body.insert(output);
|
||||
// block = body.childElements().last();
|
||||
@ -985,9 +1013,9 @@ WysijaForm.Widget = window.Class.create(WysijaForm.Block, {
|
||||
info('widget -> setup');
|
||||
this.setupControls();
|
||||
},
|
||||
save: function () {
|
||||
info('widget -> save');
|
||||
save: function() {
|
||||
var data = this.getData();
|
||||
info('widget -> save');
|
||||
|
||||
if (data.element !== undefined) {
|
||||
delete data.element;
|
||||
@ -1021,17 +1049,21 @@ WysijaForm.Widget = window.Class.create(WysijaForm.Block, {
|
||||
remove: function () {
|
||||
this.removeBlock();
|
||||
},
|
||||
redraw: function (data) {
|
||||
redraw: function(data) {
|
||||
var options;
|
||||
var block_template;
|
||||
var template;
|
||||
var params;
|
||||
// set parameters
|
||||
this.setData(data);
|
||||
var options = this.getData();
|
||||
options = this.getData();
|
||||
// redraw block
|
||||
var block_template = window.Handlebars.compile(window.$('form_template_block').innerHTML);
|
||||
var template = window.Handlebars.compile(window.$('form_template_' + options.type).innerHTML);
|
||||
var data = window.$H(options).merge({
|
||||
block_template = window.Handlebars.compile(window.$('form_template_block').innerHTML);
|
||||
template = window.Handlebars.compile(window.$('form_template_' + options.type).innerHTML);
|
||||
params = window.$H(options).merge({
|
||||
template: template(options)
|
||||
}).toObject();
|
||||
this.element.replace(block_template(data));
|
||||
this.element.replace(block_template(params));
|
||||
|
||||
WysijaForm.init();
|
||||
},
|
||||
@ -1063,9 +1095,9 @@ function info(value) {
|
||||
var noop = function () {};
|
||||
var methods = ['assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error', 'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline', 'profile', 'profileEnd', 'markTimeline', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'];
|
||||
var length = methods.length;
|
||||
window.console = {};
|
||||
var console = {};
|
||||
while (length--) {
|
||||
window.console = {};
|
||||
while(length--) {
|
||||
console[methods[length]] = noop;
|
||||
}
|
||||
}());
|
||||
|
@ -3,7 +3,8 @@ define('handlebars_helpers', ['handlebars'], function (Handlebars) {
|
||||
Handlebars.registerHelper('concat', function() {
|
||||
var size = (arguments.length - 1);
|
||||
var output = '';
|
||||
for(var i = 0; i < size; i++) {
|
||||
var i;
|
||||
for(i = 0; i < size; i++) {
|
||||
output += arguments[i];
|
||||
}
|
||||
return output;
|
||||
@ -12,16 +13,17 @@ define('handlebars_helpers', ['handlebars'], function (Handlebars) {
|
||||
Handlebars.registerHelper('number_format', function (value, block) {
|
||||
return Number(value).toLocaleString();
|
||||
});
|
||||
Handlebars.registerHelper('date_format', function (timestamp, block) {
|
||||
if (window.moment) {
|
||||
if (timestamp === undefined || isNaN(timestamp) || timestamp <= 0) {
|
||||
Handlebars.registerHelper('date_format', function(timestamp, block) {
|
||||
var f;
|
||||
if(window.moment) {
|
||||
if(timestamp === undefined || isNaN(timestamp) || timestamp <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// set date format
|
||||
var f = block.hash.format || 'MMM Do, YYYY';
|
||||
f = block.hash.format || 'MMM Do, YYYY';
|
||||
// check if we passed a timestamp
|
||||
if (parseInt(timestamp, 10) == timestamp) {
|
||||
if(parseInt(timestamp, 10) == timestamp) {
|
||||
return window.moment.unix(timestamp).format(f);
|
||||
} else {
|
||||
return window.moment.utc(timestamp).format(f);
|
||||
@ -37,6 +39,7 @@ define('handlebars_helpers', ['handlebars'], function (Handlebars) {
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
|
||||
var values;
|
||||
switch (operator) {
|
||||
case '==':
|
||||
return (v1 == v2) ? options.fn(this) : options.inverse(this);
|
||||
@ -59,7 +62,7 @@ define('handlebars_helpers', ['handlebars'], function (Handlebars) {
|
||||
case '||':
|
||||
return (v1 || v2) ? options.fn(this) : options.inverse(this);
|
||||
case 'in':
|
||||
var values = v2.split(',');
|
||||
values = v2.split(',');
|
||||
return (v2.indexOf(v1) !== -1) ? options.fn(this) : options.inverse(this);
|
||||
default:
|
||||
return options.inverse(this);
|
||||
@ -95,11 +98,12 @@ define('handlebars_helpers', ['handlebars'], function (Handlebars) {
|
||||
});
|
||||
|
||||
|
||||
Handlebars.registerHelper('rsa_key', function (value, block) {
|
||||
Handlebars.registerHelper('rsa_key', function(value, block) {
|
||||
var lines;
|
||||
// extract all lines into an array
|
||||
if (value === undefined) return '';
|
||||
if(value === undefined) return '';
|
||||
|
||||
var lines = value.trim().split('\n');
|
||||
lines = value.trim().split('\n');
|
||||
|
||||
// remove header & footer
|
||||
lines.shift();
|
||||
@ -126,10 +130,10 @@ define('handlebars_helpers', ['handlebars'], function (Handlebars) {
|
||||
*/
|
||||
Handlebars.registerHelper('ellipsis', function (str, limit, append) {
|
||||
var strAppend = append;
|
||||
var sanitized = str.replace(/(<([^>]+)>)/g, '');
|
||||
if (strAppend === undefined) {
|
||||
strAppend = '';
|
||||
}
|
||||
var sanitized = str.replace(/(<([^>]+)>)/g, '');
|
||||
if (sanitized.length > limit) {
|
||||
return sanitized.substr(0, limit - strAppend.length) + strAppend;
|
||||
} else {
|
||||
|
@ -115,8 +115,9 @@ define('modal', ['mailpoet', 'jquery'],
|
||||
return window.Handlebars.compile(template);
|
||||
}
|
||||
},
|
||||
init: function (options) {
|
||||
if (this.initialized === true) {
|
||||
init: function(options) {
|
||||
var modal;
|
||||
if(this.initialized === true) {
|
||||
this.close();
|
||||
}
|
||||
// merge options
|
||||
@ -133,8 +134,8 @@ define('modal', ['mailpoet', 'jquery'],
|
||||
|
||||
if (this.options.type !== null) {
|
||||
// insert modal depending on its type
|
||||
if (this.options.type === 'popup') {
|
||||
var modal = this.compileTemplate(
|
||||
if(this.options.type === 'popup') {
|
||||
modal = this.compileTemplate(
|
||||
this.templates[this.options.type]
|
||||
);
|
||||
// create modal
|
||||
@ -342,21 +343,22 @@ define('modal', ['mailpoet', 'jquery'],
|
||||
|
||||
return this;
|
||||
},
|
||||
setPosition: function () {
|
||||
switch (this.options.type) {
|
||||
setPosition: function() {
|
||||
var screenWidth;
|
||||
var screenHeight;
|
||||
var modalWidth;
|
||||
var modalHeight;
|
||||
switch(this.options.type) {
|
||||
case 'popup':
|
||||
var screenWidth = jQuery(window).width();
|
||||
var screenHeight = jQuery(window).height();
|
||||
var modalWidth = jQuery('.mailpoet_'+ this.options.type +'_wrapper').width();
|
||||
var modalHeight = jQuery('.mailpoet_'+ this.options.type +'_wrapper').height();
|
||||
|
||||
var top = Math.max(48, parseInt((screenHeight / 2) - (modalHeight / 2)));
|
||||
var left = Math.max(0, parseInt((screenWidth / 2) - (modalWidth / 2)));
|
||||
screenWidth = jQuery(window).width();
|
||||
screenHeight = jQuery(window).height();
|
||||
modalWidth = jQuery('.mailpoet_'+ this.options.type +'_wrapper').width();
|
||||
modalHeight = jQuery('.mailpoet_'+ this.options.type +'_wrapper').height();
|
||||
|
||||
// set position of popup depending on screen dimensions.
|
||||
jQuery('#mailpoet_popup').css({
|
||||
top: top,
|
||||
left: left
|
||||
top: Math.max(48, parseInt((screenHeight / 2) - (modalHeight / 2))),
|
||||
left: Math.max(0, parseInt((screenWidth / 2) - (modalWidth / 2)))
|
||||
});
|
||||
break;
|
||||
case 'panel':
|
||||
|
@ -197,6 +197,7 @@ define([
|
||||
var index;
|
||||
var tempCollection;
|
||||
var tempCollection2;
|
||||
var tempModel;
|
||||
|
||||
if (dropPosition === undefined) return;
|
||||
|
||||
@ -220,7 +221,7 @@ define([
|
||||
} else {
|
||||
// Special insertion by replacing target block with collection
|
||||
// and inserting dropModel into that
|
||||
var tempModel = viewCollection.at(dropPosition.index);
|
||||
tempModel = viewCollection.at(dropPosition.index);
|
||||
|
||||
tempCollection = new (window.EditorApplication.getBlockTypeModel('container'))({
|
||||
orientation: (view.model.get('orientation') === 'vertical') ? 'horizontal' : 'vertical'
|
||||
|
@ -49,18 +49,21 @@ define([
|
||||
|
||||
onstart: function (startEvent) {
|
||||
var event = startEvent;
|
||||
|
||||
if (that.options.cloneOriginal === true) {
|
||||
// Use substitution instead of a clone
|
||||
var tempClone = (_.isFunction(that.options.onDragSubstituteBy)) ? that.options.onDragSubstituteBy(that) : undefined;
|
||||
// Or use a clone
|
||||
var clone = tempClone || event.target.cloneNode(true);
|
||||
|
||||
var $original = jQuery(event.target);
|
||||
var $clone = jQuery(clone);
|
||||
var centerXOffset;
|
||||
var centerYOffset;
|
||||
var parentOffset;
|
||||
var tempClone;
|
||||
var clone;
|
||||
var $original;
|
||||
var $clone;
|
||||
|
||||
if (that.options.cloneOriginal === true) {
|
||||
// Use substitution instead of a clone
|
||||
tempClone = (_.isFunction(that.options.onDragSubstituteBy)) ? that.options.onDragSubstituteBy(that) : undefined;
|
||||
// Or use a clone
|
||||
clone = tempClone || event.target.cloneNode(true);
|
||||
$original = jQuery(event.target);
|
||||
$clone = jQuery(clone);
|
||||
|
||||
$clone.addClass('mailpoet_droppable_active');
|
||||
$clone.css('position', 'absolute');
|
||||
|
@ -42,13 +42,14 @@ define([
|
||||
_.debounce(this.refresh, DELAY_REFRESH_FOR_MS)
|
||||
);
|
||||
},
|
||||
refresh: function () {
|
||||
var models = App.findModels(function (model) {
|
||||
refresh: function() {
|
||||
var blocks;
|
||||
var models = App.findModels(function(model) {
|
||||
return model.get('type') === 'automatedLatestContent';
|
||||
}) || [];
|
||||
|
||||
if (models.length === 0) return;
|
||||
var blocks = _.map(models, function (model) {
|
||||
blocks = _.map(models, function(model) {
|
||||
return model.toJSON();
|
||||
});
|
||||
|
||||
@ -213,12 +214,13 @@ define([
|
||||
},
|
||||
transport: function (options, success, failure) {
|
||||
var taxonomies;
|
||||
var termsPromise;
|
||||
var promise = CommunicationComponent.getTaxonomies(
|
||||
that.model.get('contentType')
|
||||
).then(function (tax) {
|
||||
taxonomies = tax;
|
||||
// Fetch available terms based on the list of taxonomies already fetched
|
||||
var promise = CommunicationComponent.getTerms({
|
||||
termsPromise = CommunicationComponent.getTerms({
|
||||
search: options.data.term,
|
||||
taxonomies: _.keys(taxonomies)
|
||||
}).then(function (terms) {
|
||||
@ -227,7 +229,7 @@ define([
|
||||
terms: terms
|
||||
};
|
||||
});
|
||||
return promise;
|
||||
return termsPromise;
|
||||
});
|
||||
|
||||
promise.then(success);
|
||||
|
@ -240,9 +240,10 @@ define([
|
||||
behaviors: {
|
||||
ColorPickerBehavior: {}
|
||||
},
|
||||
initialize: function (params) {
|
||||
initialize: function(params) {
|
||||
var panelParams;
|
||||
this.model.trigger('startEditing');
|
||||
var panelParams = {
|
||||
panelParams = {
|
||||
element: this.$el,
|
||||
template: '',
|
||||
position: 'right',
|
||||
|
@ -47,10 +47,10 @@ define([
|
||||
ignoreFrom: '.mailpoet_resize_handle'
|
||||
}
|
||||
}, base.BlockView.prototype.behaviors),
|
||||
onDragSubstituteBy: function () { return Module.DividerWidgetView; },
|
||||
initialize: function () {
|
||||
base.BlockView.prototype.initialize.apply(this, arguments);
|
||||
onDragSubstituteBy: function() { return Module.DividerWidgetView; },
|
||||
initialize: function() {
|
||||
var that = this;
|
||||
base.BlockView.prototype.initialize.apply(this, arguments);
|
||||
|
||||
// Listen for attempts to change all dividers in one go
|
||||
this._replaceDividerHandler = function (data) { that.model.set(data); that.model.trigger('applyToAll'); };
|
||||
|
@ -126,14 +126,17 @@ define([
|
||||
this.showMediaManager();
|
||||
}
|
||||
},
|
||||
showMediaManager: function () {
|
||||
showMediaManager: function() {
|
||||
var that = this;
|
||||
var MediaManager;
|
||||
var theFrame;
|
||||
if (this._mediaManager) {
|
||||
this._mediaManager.resetSelections();
|
||||
this._mediaManager.open();
|
||||
return;
|
||||
}
|
||||
|
||||
var MediaManager = window.wp.media.view.MediaFrame.Select.extend({
|
||||
MediaManager = window.wp.media.view.MediaFrame.Select.extend({
|
||||
|
||||
initialize: function () {
|
||||
window.wp.media.view.MediaFrame.prototype.initialize.apply(this, arguments);
|
||||
@ -195,7 +198,8 @@ define([
|
||||
}
|
||||
},
|
||||
|
||||
bindHandlers: function () {
|
||||
bindHandlers: function() {
|
||||
var handlers;
|
||||
// from Select
|
||||
this.on('router:create:browse', this.createRouter, this);
|
||||
this.on('router:render:browse', this.browseRouter, this);
|
||||
@ -210,7 +214,7 @@ define([
|
||||
|
||||
this.on('updateExcluded', this.browseContent, this);
|
||||
|
||||
var handlers = {
|
||||
handlers = {
|
||||
content: {
|
||||
embed: 'embedContent',
|
||||
'edit-selection': 'editSelectionContent'
|
||||
@ -321,7 +325,7 @@ define([
|
||||
|
||||
});
|
||||
|
||||
var theFrame = new MediaManager({
|
||||
theFrame = new MediaManager({
|
||||
id: 'mailpoet-media-manager',
|
||||
frame: 'select',
|
||||
title: 'Select image',
|
||||
@ -335,7 +339,6 @@ define([
|
||||
text: 'Select'
|
||||
}
|
||||
});
|
||||
var that = this;
|
||||
this._mediaManager = theFrame;
|
||||
|
||||
this._mediaManager.on('insert', function () {
|
||||
|
@ -41,6 +41,11 @@ define([
|
||||
|
||||
var Module = {};
|
||||
var base = BaseBlock;
|
||||
var PostsDisplayOptionsSettingsView;
|
||||
var SinglePostSelectionSettingsView;
|
||||
var EmptyPostSelectionSettingsView;
|
||||
var PostSelectionSettingsView;
|
||||
var PostsSelectionCollectionView;
|
||||
|
||||
Module.PostsBlockModel = base.BlockModel.extend({
|
||||
stale: ['_selectedPosts', '_availablePosts', '_transformedPosts'],
|
||||
@ -189,14 +194,16 @@ define([
|
||||
this.toolsView = new Module.PostsBlockToolsView({ model: this.model });
|
||||
this.model.reply('blockView', this.notifyAboutSelf, this);
|
||||
},
|
||||
onRender: function () {
|
||||
onRender: function() {
|
||||
var ContainerView;
|
||||
var renderOptions;
|
||||
if (!this.getRegion('toolsRegion').hasView()) {
|
||||
this.showChildView('toolsRegion', this.toolsView);
|
||||
}
|
||||
this.trigger('showSettings');
|
||||
|
||||
var ContainerView = App.getBlockTypeView('container');
|
||||
var renderOptions = {
|
||||
ContainerView = App.getBlockTypeView('container');
|
||||
renderOptions = {
|
||||
disableTextEditor: true,
|
||||
disableDragAndDrop: true,
|
||||
emptyContainerMessage: MailPoet.I18n.t('noPostsToDisplay')
|
||||
@ -283,7 +290,7 @@ define([
|
||||
}
|
||||
});
|
||||
|
||||
var PostsSelectionCollectionView = Marionette.CollectionView.extend({
|
||||
PostsSelectionCollectionView = Marionette.CollectionView.extend({
|
||||
className: 'mailpoet_post_scroll_container',
|
||||
childView: function () { return SinglePostSelectionSettingsView; },
|
||||
emptyView: function () { return EmptyPostSelectionSettingsView; },
|
||||
@ -307,8 +314,8 @@ define([
|
||||
}
|
||||
});
|
||||
|
||||
var PostSelectionSettingsView = Marionette.View.extend({
|
||||
getTemplate: function () { return window.templates.postSelectionPostsBlockSettings; },
|
||||
PostSelectionSettingsView = Marionette.View.extend({
|
||||
getTemplate: function() { return window.templates.postSelectionPostsBlockSettings; },
|
||||
regions: {
|
||||
posts: '.mailpoet_post_selection_container'
|
||||
},
|
||||
@ -333,10 +340,11 @@ define([
|
||||
this.$('.mailpoet_post_selection_loading').css('visibility', 'hidden');
|
||||
}
|
||||
},
|
||||
onRender: function () {
|
||||
onRender: function() {
|
||||
var postsView;
|
||||
// Dynamically update available post types
|
||||
CommunicationComponent.getPostTypes().done(_.bind(this._updateContentTypes, this));
|
||||
var postsView = new PostsSelectionCollectionView({
|
||||
postsView = new PostsSelectionCollectionView({
|
||||
collection: this.model.get('_availablePosts'),
|
||||
blockModel: this.model
|
||||
});
|
||||
@ -358,12 +366,13 @@ define([
|
||||
},
|
||||
transport: function (options, success, failure) {
|
||||
var taxonomies;
|
||||
var termsPromise;
|
||||
var promise = CommunicationComponent.getTaxonomies(
|
||||
that.model.get('contentType')
|
||||
).then(function (tax) {
|
||||
taxonomies = tax;
|
||||
// Fetch available terms based on the list of taxonomies already fetched
|
||||
var promise = CommunicationComponent.getTerms({
|
||||
termsPromise = CommunicationComponent.getTerms({
|
||||
search: options.data.term,
|
||||
taxonomies: _.keys(taxonomies)
|
||||
}).then(function (terms) {
|
||||
@ -372,7 +381,7 @@ define([
|
||||
terms: terms
|
||||
};
|
||||
});
|
||||
return promise;
|
||||
return termsPromise;
|
||||
});
|
||||
|
||||
promise.then(success);
|
||||
@ -427,13 +436,13 @@ define([
|
||||
}
|
||||
});
|
||||
|
||||
var EmptyPostSelectionSettingsView = Marionette.View.extend({
|
||||
getTemplate: function () { return window.templates.emptyPostPostsBlockSettings; }
|
||||
EmptyPostSelectionSettingsView = Marionette.View.extend({
|
||||
getTemplate: function() { return window.templates.emptyPostPostsBlockSettings; }
|
||||
});
|
||||
|
||||
var SinglePostSelectionSettingsView = Marionette.View.extend({
|
||||
getTemplate: function () { return window.templates.singlePostPostsBlockSettings; },
|
||||
events: function () {
|
||||
SinglePostSelectionSettingsView = Marionette.View.extend({
|
||||
getTemplate: function() { return window.templates.singlePostPostsBlockSettings; },
|
||||
events: function() {
|
||||
return {
|
||||
'change .mailpoet_select_post_checkbox': 'postSelectionChange'
|
||||
};
|
||||
@ -458,9 +467,9 @@ define([
|
||||
}
|
||||
});
|
||||
|
||||
var PostsDisplayOptionsSettingsView = base.BlockSettingsView.extend({
|
||||
getTemplate: function () { return window.templates.displayOptionsPostsBlockSettings; },
|
||||
events: function () {
|
||||
PostsDisplayOptionsSettingsView = base.BlockSettingsView.extend({
|
||||
getTemplate: function() { return window.templates.displayOptionsPostsBlockSettings; },
|
||||
events: function() {
|
||||
return {
|
||||
'click .mailpoet_posts_select_button': 'showButtonSettings',
|
||||
'click .mailpoet_posts_select_divider': 'showDividerSettings',
|
||||
|
@ -19,6 +19,7 @@ define([
|
||||
var SocialBlockSettingsIconView;
|
||||
var SocialBlockSettingsIconCollectionView;
|
||||
var SocialBlockSettingsStylesView;
|
||||
var SocialIconView;
|
||||
|
||||
Module.SocialIconModel = SuperModel.extend({
|
||||
defaults: function () {
|
||||
@ -83,7 +84,7 @@ define([
|
||||
}
|
||||
});
|
||||
|
||||
var SocialIconView = Marionette.View.extend({
|
||||
SocialIconView = Marionette.View.extend({
|
||||
tagName: 'span',
|
||||
getTemplate: function () { return window.templates.socialIconBlock; },
|
||||
modelEvents: {
|
||||
|
@ -6,16 +6,16 @@
|
||||
* Courtesy of https://gist.github.com/jmeas/7992474cdb1c5672d88b
|
||||
*/
|
||||
|
||||
(function (root, factory) {
|
||||
(function(root, factory) {
|
||||
var Marionette = require('backbone.marionette');
|
||||
var Radio = require('backbone.radio');
|
||||
var _ = require('underscore');
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define(['backbone.marionette', 'backbone.radio', 'underscore'], function (Marionette, Radio, _) {
|
||||
return factory(Marionette, Radio, _);
|
||||
});
|
||||
}
|
||||
else if (typeof exports !== 'undefined') {
|
||||
var Marionette = require('backbone.marionette');
|
||||
var Radio = require('backbone.radio');
|
||||
var _ = require('underscore');
|
||||
module.exports = factory(Marionette, Radio, _);
|
||||
}
|
||||
else {
|
||||
|
@ -290,13 +290,14 @@ define([
|
||||
});
|
||||
}
|
||||
},
|
||||
validateNewsletter: function (jsonObject) {
|
||||
validateNewsletter: function(jsonObject) {
|
||||
var contents;
|
||||
if (!App._contentContainer.isValid()) {
|
||||
this.showValidationError(App._contentContainer.validationError);
|
||||
return;
|
||||
}
|
||||
|
||||
var contents = JSON.stringify(jsonObject);
|
||||
contents = JSON.stringify(jsonObject);
|
||||
if (App.getConfig().get('validation.validateUnsubscribeLinkPresent') &&
|
||||
contents.indexOf('[link:subscription_unsubscribe_url]') < 0 &&
|
||||
contents.indexOf('[link:subscription_unsubscribe]') < 0) {
|
||||
@ -339,10 +340,12 @@ define([
|
||||
saveTimeout = undefined;
|
||||
};
|
||||
|
||||
Module.beforeExitWithUnsavedChanges = function (e) {
|
||||
Module.beforeExitWithUnsavedChanges = function(e) {
|
||||
var message;
|
||||
var event;
|
||||
if (saveTimeout) {
|
||||
var message = MailPoet.I18n.t('unsavedChangesWillBeLost');
|
||||
var event = e || window.event;
|
||||
message = MailPoet.I18n.t('unsavedChangesWillBeLost');
|
||||
event = e || window.event;
|
||||
|
||||
if (event) {
|
||||
event.returnValue = message;
|
||||
|
@ -23,7 +23,7 @@ define([
|
||||
'use strict';
|
||||
|
||||
var Module = {};
|
||||
|
||||
var SidebarView;
|
||||
// Widget handlers for use to create new content blocks via drag&drop
|
||||
Module._contentWidgets = new (Backbone.Collection.extend({
|
||||
model: SuperModel.extend({
|
||||
@ -52,8 +52,8 @@ define([
|
||||
Module.registerLayoutWidget = function (widget) { return Module._layoutWidgets.add(widget); };
|
||||
Module.getLayoutWidgets = function () { return Module._layoutWidgets; };
|
||||
|
||||
var SidebarView = Marionette.View.extend({
|
||||
getTemplate: function () { return window.templates.sidebar; },
|
||||
SidebarView = Marionette.View.extend({
|
||||
getTemplate: function() { return window.templates.sidebar; },
|
||||
regions: {
|
||||
contentRegion: '.mailpoet_content_region',
|
||||
layoutRegion: '.mailpoet_layout_region',
|
||||
@ -265,12 +265,13 @@ define([
|
||||
data: json
|
||||
}).always(function () {
|
||||
MailPoet.Modal.loading(false);
|
||||
}).done(function (response) {
|
||||
}).done(function(response) {
|
||||
var view;
|
||||
this.previewView = new Module.NewsletterPreviewView({
|
||||
previewUrl: response.meta.preview_url
|
||||
});
|
||||
|
||||
var view = this.previewView.render();
|
||||
view = this.previewView.render();
|
||||
this.previewView.$el.css('height', '100%');
|
||||
|
||||
MailPoet.Modal.popup({
|
||||
|
@ -71,13 +71,15 @@ define([
|
||||
|
||||
App.on('before:start', function (App, options) {
|
||||
var Application = App;
|
||||
var body;
|
||||
var globalStyles;
|
||||
// Expose style methods to global application
|
||||
Application.getGlobalStyles = Module.getGlobalStyles;
|
||||
Application.setGlobalStyles = Module.setGlobalStyles;
|
||||
Application.getAvailableStyles = Module.getAvailableStyles;
|
||||
|
||||
var body = options.newsletter.body;
|
||||
var globalStyles = (_.has(body, 'globalStyles')) ? body.globalStyles : {};
|
||||
body = options.newsletter.body;
|
||||
globalStyles = (_.has(body, 'globalStyles')) ? body.globalStyles : {};
|
||||
this.setGlobalStyles(globalStyles);
|
||||
});
|
||||
|
||||
|
@ -26,15 +26,17 @@ tinymce.PluginManager.add('mailpoet_shortcodes', function(editor, url) {
|
||||
onclick: function() {
|
||||
var shortcodes = [];
|
||||
var configShortcodes = editor.settings.mailpoet_shortcodes;
|
||||
var segment;
|
||||
var i;
|
||||
|
||||
for (var segment in configShortcodes) {
|
||||
for (segment in configShortcodes) {
|
||||
if (configShortcodes.hasOwnProperty(segment)) {
|
||||
shortcodes.push({
|
||||
type: 'label',
|
||||
text: segment
|
||||
});
|
||||
|
||||
for (var i = 0; i < configShortcodes[segment].length; i += 1) {
|
||||
for (i = 0; i < configShortcodes[segment].length; i += 1) {
|
||||
shortcodes.push({
|
||||
type: 'button',
|
||||
text: configShortcodes[segment][i].text,
|
||||
|
@ -46,7 +46,9 @@ define('notice', ['mailpoet', 'jquery'], function (mp, jQuery) {
|
||||
|
||||
return this;
|
||||
},
|
||||
createNotice: function () {
|
||||
createNotice: function() {
|
||||
var onClose;
|
||||
var positionAfter;
|
||||
// clone element
|
||||
this.element = jQuery('#mailpoet_notice_' + this.options.type).clone();
|
||||
|
||||
@ -62,7 +64,6 @@ define('notice', ['mailpoet', 'jquery'], function (mp, jQuery) {
|
||||
this.element.removeAttr('id');
|
||||
|
||||
// insert notice after its parent
|
||||
var positionAfter;
|
||||
if (typeof this.options.positionAfter === 'object') {
|
||||
positionAfter = this.options.positionAfter;
|
||||
} else if (typeof this.options.positionAfter === 'string') {
|
||||
@ -73,7 +74,7 @@ define('notice', ['mailpoet', 'jquery'], function (mp, jQuery) {
|
||||
positionAfter.after(this.element);
|
||||
|
||||
// setup onClose callback
|
||||
var onClose = null;
|
||||
onClose = null;
|
||||
if (this.options.onClose !== null) {
|
||||
onClose = this.options.onClose;
|
||||
}
|
||||
@ -176,13 +177,14 @@ define('notice', ['mailpoet', 'jquery'], function (mp, jQuery) {
|
||||
this.options.onOpen(this.element);
|
||||
}
|
||||
},
|
||||
hide: function (all) {
|
||||
hide: function(all) {
|
||||
var id;
|
||||
if (all !== undefined && all === true) {
|
||||
// all notices
|
||||
jQuery('.mailpoet_notice:not([id])').trigger('close');
|
||||
} else if (all !== undefined && jQuery.isArray(all)) {
|
||||
// array of ids
|
||||
for (var id in all) {
|
||||
for (id in all) {
|
||||
jQuery('[data-id="' + all[id] + '"]').trigger('close');
|
||||
}
|
||||
} if (all !== undefined) {
|
||||
|
@ -5,7 +5,7 @@ define(
|
||||
function (
|
||||
MailPoet
|
||||
) {
|
||||
|
||||
var element;
|
||||
function eventHandler() {
|
||||
if (confirm(MailPoet.I18n.t('reinstallConfirmation'))) {
|
||||
MailPoet.trackEvent(
|
||||
@ -36,7 +36,7 @@ define(
|
||||
return false;
|
||||
}
|
||||
|
||||
var element = document.getElementById('mailpoet_reinstall');
|
||||
element = document.getElementById('mailpoet_reinstall');
|
||||
if (element) {
|
||||
element.addEventListener('click', eventHandler, false);
|
||||
}
|
||||
|
@ -17,22 +17,29 @@ define(
|
||||
return;
|
||||
}
|
||||
jQuery(document).ready(function () {
|
||||
var segmentsContainerElement;
|
||||
var subscriberFieldsContainerElement;
|
||||
var exportConfirmedOptionElement;
|
||||
var groupBySegmentOptionElement;
|
||||
var nextStepButton;
|
||||
var renderSegmentsAndFields;
|
||||
var subscribers_export_template;
|
||||
if (!window.exportData.segments) {
|
||||
return;
|
||||
}
|
||||
var subscribers_export_template =
|
||||
subscribers_export_template =
|
||||
Handlebars.compile(jQuery('#mailpoet_subscribers_export_template').html());
|
||||
|
||||
// render template
|
||||
jQuery('#mailpoet_subscribers_export > div.inside').html(subscribers_export_template(window.exportData));
|
||||
|
||||
// define reusable variables
|
||||
var segmentsContainerElement = jQuery('#export_lists');
|
||||
var subscriberFieldsContainerElement = jQuery('#export_columns');
|
||||
var exportConfirmedOptionElement = jQuery(':radio[name="option_confirmed"]');
|
||||
var groupBySegmentOptionElement = jQuery(':checkbox[name="option_group_by_list"]');
|
||||
var nextStepButton = jQuery('a.mailpoet_export_process');
|
||||
var renderSegmentsAndFields = function (container, data) {
|
||||
segmentsContainerElement = jQuery('#export_lists');
|
||||
subscriberFieldsContainerElement = jQuery('#export_columns');
|
||||
exportConfirmedOptionElement = jQuery(':radio[name="option_confirmed"]');
|
||||
groupBySegmentOptionElement = jQuery(':checkbox[name="option_group_by_list"]');
|
||||
nextStepButton = jQuery('a.mailpoet_export_process');
|
||||
renderSegmentsAndFields = function (container, data) {
|
||||
if (container.data('select2')) {
|
||||
container
|
||||
.html('')
|
||||
@ -60,12 +67,13 @@ define(
|
||||
'select',
|
||||
'deselect'
|
||||
];
|
||||
var allOptions;
|
||||
if (_.contains(fieldsToExclude, selectedOptionId)) {
|
||||
selectEvent.preventDefault();
|
||||
if (selectedOptionId === 'deselect') {
|
||||
jQuery(selectElement).val('').trigger('change');
|
||||
} else {
|
||||
var allOptions = [];
|
||||
allOptions = [];
|
||||
_.each(container.find('option'), function (field) {
|
||||
if (!_.contains(fieldsToExclude, field.value)) {
|
||||
allOptions.push(field.value);
|
||||
@ -133,11 +141,12 @@ define(
|
||||
}
|
||||
|
||||
nextStepButton.click(function () {
|
||||
var exportFormat;
|
||||
if (jQuery(this).hasClass('button-disabled')) {
|
||||
return;
|
||||
}
|
||||
MailPoet.Modal.loading(true);
|
||||
var exportFormat = jQuery(':radio[name="option_format"]:checked').val();
|
||||
exportFormat = jQuery(':radio[name="option_format"]:checked').val();
|
||||
MailPoet.Ajax.post({
|
||||
api_version: window.mailpoet_api_version,
|
||||
endpoint: 'ImportExport',
|
||||
|
@ -25,9 +25,10 @@ define(
|
||||
return;
|
||||
}
|
||||
jQuery(document).ready(function () {
|
||||
var router;
|
||||
jQuery('input[name="select_method"]').attr('checked', false);
|
||||
// configure router
|
||||
var router = new (Backbone.Router.extend({
|
||||
router = new (Backbone.Router.extend({
|
||||
routes: {
|
||||
'': 'home',
|
||||
step1: 'step1',
|
||||
@ -50,6 +51,18 @@ define(
|
||||
* STEP 1 (upload or copy/paste)
|
||||
*/
|
||||
router.on('route:step1', function () {
|
||||
var methodProcessContainerTemplate;
|
||||
var currentStepE;
|
||||
var methodSelectionElement;
|
||||
var pasteInputElement;
|
||||
var pasteInputPlaceholderElement;
|
||||
var pasteProcessButtonElement;
|
||||
var mailChimpKeyInputElement;
|
||||
var mailChimpKeyVerifyButtonElement;
|
||||
var mailChimpListsContainerElement;
|
||||
var mailChimpProcessButtonElement;
|
||||
var uploadElement;
|
||||
var uploadProcessButtonElement;
|
||||
// set or reset temporary validation rule on all columns
|
||||
window.mailpoetColumns = jQuery.map(window.mailpoetColumns, function (column, columnIndex) {
|
||||
var col = column;
|
||||
@ -63,34 +76,34 @@ define(
|
||||
}
|
||||
|
||||
// render process button for each method
|
||||
var methodProcessContainerTemplate =
|
||||
methodProcessContainerTemplate =
|
||||
Handlebars.compile(jQuery('#method_process_template').html());
|
||||
jQuery('.mailpoet_method_process').html(methodProcessContainerTemplate());
|
||||
|
||||
// define reusable variables
|
||||
var currentStepE = jQuery(location.hash);
|
||||
var methodSelectionElement = jQuery('#select_method');
|
||||
var pasteInputElement = jQuery('#paste_input');
|
||||
var pasteInputPlaceholderElement =
|
||||
currentStepE = jQuery(location.hash);
|
||||
methodSelectionElement = jQuery('#select_method');
|
||||
pasteInputElement = jQuery('#paste_input');
|
||||
pasteInputPlaceholderElement =
|
||||
pasteInputElement.data('placeholder').replace(/\\n/g, '\n');
|
||||
var pasteProcessButtonElement =
|
||||
pasteProcessButtonElement =
|
||||
jQuery('#method_paste > div.mailpoet_method_process')
|
||||
.find('a.mailpoet_process');
|
||||
var mailChimpKeyInputElement = jQuery('#mailchimp_key');
|
||||
var mailChimpKeyVerifyButtonElement = jQuery('#mailchimp_key_verify');
|
||||
var mailChimpListsContainerElement = jQuery('#mailchimp_lists');
|
||||
var mailChimpProcessButtonElement = jQuery('#method_mailchimp > div.mailpoet_method_process')
|
||||
mailChimpKeyInputElement = jQuery('#mailchimp_key');
|
||||
mailChimpKeyVerifyButtonElement = jQuery('#mailchimp_key_verify');
|
||||
mailChimpListsContainerElement = jQuery('#mailchimp_lists');
|
||||
mailChimpProcessButtonElement = jQuery('#method_mailchimp > div.mailpoet_method_process')
|
||||
.find('a.mailpoet_process');
|
||||
var uploadElement = jQuery('#file_local');
|
||||
var uploadProcessButtonElement =
|
||||
uploadElement = jQuery('#file_local');
|
||||
uploadProcessButtonElement =
|
||||
jQuery('#method_file > div.mailpoet_method_process')
|
||||
.find('a.mailpoet_process');
|
||||
|
||||
// define method change behavior
|
||||
methodSelectionElement.change(function () {
|
||||
MailPoet.Notice.hide();
|
||||
var available_methods = jQuery(':radio[name="select_method"]');
|
||||
var selected_method = available_methods.index(available_methods.filter(':checked'));
|
||||
MailPoet.Notice.hide();
|
||||
// hide all methods
|
||||
currentStepE.find('.inside')
|
||||
.children('div[id^="method_"]')
|
||||
@ -129,9 +142,9 @@ define(
|
||||
});
|
||||
|
||||
pasteProcessButtonElement.click(function () {
|
||||
var pasteSize = encodeURI(pasteInputElement.val()).split(/%..|./).length - 1;
|
||||
MailPoet.Notice.hide();
|
||||
// get an approximate size of textarea paste in bytes
|
||||
var pasteSize = encodeURI(pasteInputElement.val()).split(/%..|./).length - 1;
|
||||
if (pasteSize > window.maxPostSizeBytes) {
|
||||
MailPoet.Notice.error(MailPoet.I18n.t('maxPostSizeNotice'));
|
||||
return;
|
||||
@ -147,8 +160,8 @@ define(
|
||||
* CSV file
|
||||
*/
|
||||
uploadElement.change(function () {
|
||||
MailPoet.Notice.hide();
|
||||
var ext = this.value.match(/\.(.+)$/);
|
||||
MailPoet.Notice.hide();
|
||||
if (ext === null || ext[1].toLowerCase() !== 'csv') {
|
||||
this.value = '';
|
||||
MailPoet.Notice.error(MailPoet.I18n.t('wrongFileFormat'));
|
||||
@ -345,11 +358,18 @@ define(
|
||||
MailPoet.Notice.error(MailPoet.I18n.t('dataProcessingError'));
|
||||
},
|
||||
complete: function (CSV) {
|
||||
for (var rowCount in CSV.data) {
|
||||
var rowData = CSV.data[rowCount].map(function (el) {
|
||||
var email;
|
||||
var emailAddress;
|
||||
var column;
|
||||
var rowCount;
|
||||
var rowData;
|
||||
var rowColumnCount;
|
||||
var errorNotice;
|
||||
for (rowCount in CSV.data) {
|
||||
rowData = CSV.data[rowCount].map(function (el) {
|
||||
return el.trim();
|
||||
});
|
||||
var rowColumnCount = rowData.length;
|
||||
rowColumnCount = rowData.length;
|
||||
// set the number of row elements based on the first non-empty row
|
||||
if (columnCount === null) {
|
||||
columnCount = rowColumnCount;
|
||||
@ -363,8 +383,8 @@ define(
|
||||
// determine position of email address inside an array; this is
|
||||
// done once and then email regex is run just on that element for each row
|
||||
if (emailColumnPosition === null) {
|
||||
for (var column in rowData) {
|
||||
var emailAddress = detectAndCleanupEmail(rowData[column]);
|
||||
for (column in rowData) {
|
||||
emailAddress = detectAndCleanupEmail(rowData[column]);
|
||||
if (emailColumnPosition === null
|
||||
&& window.emailRegex.test(emailAddress)) {
|
||||
emailColumnPosition = column;
|
||||
@ -381,7 +401,7 @@ define(
|
||||
}
|
||||
}
|
||||
else if (rowData[emailColumnPosition] !== '') {
|
||||
var email = detectAndCleanupEmail(rowData[emailColumnPosition]);
|
||||
email = detectAndCleanupEmail(rowData[emailColumnPosition]);
|
||||
if (_.has(parsedEmails, email)) {
|
||||
duplicateEmails.push(email);
|
||||
}
|
||||
@ -425,7 +445,7 @@ define(
|
||||
}
|
||||
else {
|
||||
MailPoet.Modal.loading(false);
|
||||
var errorNotice = MailPoet.I18n.t('noValidRecords');
|
||||
errorNotice = MailPoet.I18n.t('noValidRecords');
|
||||
errorNotice = errorNotice.replace('[link]', MailPoet.I18n.t('csvKBLink'));
|
||||
errorNotice = errorNotice.replace('[/link]', '</a>');
|
||||
MailPoet.Notice.error(errorNotice);
|
||||
@ -436,28 +456,41 @@ define(
|
||||
});
|
||||
|
||||
router.on('route:step2', function () {
|
||||
var nextStepButton;
|
||||
var previousStepButton;
|
||||
var subscribers;
|
||||
var subscribersDataTemplate;
|
||||
var subscribersDataTemplatePartial;
|
||||
var subscribersDataParseResultsTemplate;
|
||||
var segmentSelectElement;
|
||||
var maxRowsToShow;
|
||||
var filler;
|
||||
var fillerArray;
|
||||
var fillerPosition;
|
||||
var import_results;
|
||||
var duplicates;
|
||||
var email;
|
||||
if (typeof (window.importData.step1) === 'undefined') {
|
||||
router.navigate('step1', { trigger: true });
|
||||
return;
|
||||
}
|
||||
// define reusable variables
|
||||
var nextStepButton = jQuery('#step2_process');
|
||||
var previousStepButton = jQuery('#return_to_step1');
|
||||
nextStepButton = jQuery('#step2_process');
|
||||
previousStepButton = jQuery('#return_to_step1');
|
||||
// create a copy of subscribers object for further manipulation
|
||||
var subscribers = jQuery.extend(true, {}, window.importData.step1);
|
||||
var subscribersDataTemplate = Handlebars.compile(jQuery('#subscribers_data_template').html());
|
||||
var subscribersDataTemplatePartial = Handlebars.compile(jQuery('#subscribers_data_template_partial').html());
|
||||
var subscribersDataParseResultsTemplate = Handlebars.compile(jQuery('#subscribers_data_parse_results_template').html());
|
||||
var segmentSelectElement = jQuery('#mailpoet_segments_select');
|
||||
var maxRowsToShow = 10;
|
||||
var filler = '. . .';
|
||||
subscribers = jQuery.extend(true, {}, window.importData.step1);
|
||||
subscribersDataTemplate = Handlebars.compile(jQuery('#subscribers_data_template').html());
|
||||
subscribersDataTemplatePartial = Handlebars.compile(jQuery('#subscribers_data_template_partial').html());
|
||||
subscribersDataParseResultsTemplate = Handlebars.compile(jQuery('#subscribers_data_parse_results_template').html());
|
||||
segmentSelectElement = jQuery('#mailpoet_segments_select');
|
||||
maxRowsToShow = 10;
|
||||
filler = '. . .';
|
||||
// create an array of filler data with the same number of
|
||||
// elements as in the subscribers' data row
|
||||
var fillerArray = Array.apply(
|
||||
fillerArray = Array.apply(
|
||||
null,
|
||||
new Array(subscribers.subscribers[0].length)
|
||||
).map(String.prototype.valueOf, filler);
|
||||
var fillerPosition;
|
||||
|
||||
showCurrentStep();
|
||||
|
||||
@ -469,12 +502,12 @@ define(
|
||||
if (subscribers.invalid.length || subscribers.duplicate.length) {
|
||||
// count repeating e-mails inside duplicate array and present them in
|
||||
// 'email (xN)' format
|
||||
var duplicates = {};
|
||||
duplicates = {};
|
||||
subscribers.duplicate.forEach(function (email) {
|
||||
duplicates[email] = (duplicates[email] || 0) + 1;
|
||||
});
|
||||
subscribers.duplicate = [];
|
||||
for (var email in duplicates) {
|
||||
for (email in duplicates) {
|
||||
if (duplicates[email] > 1) {
|
||||
subscribers.duplicate.push(email + ' (x' + duplicates[email] + ')');
|
||||
}
|
||||
@ -483,7 +516,7 @@ define(
|
||||
}
|
||||
}
|
||||
|
||||
var import_results = {
|
||||
import_results = {
|
||||
notice: MailPoet.I18n.t('importNoticeSkipped').replace(
|
||||
'%1$s',
|
||||
'<strong>' + (subscribers.invalid.length + subscribers.duplicate.length) + '</strong>'
|
||||
@ -587,14 +620,15 @@ define(
|
||||
name: segmentName,
|
||||
description: segmentDescription
|
||||
}
|
||||
}).done(function (response) {
|
||||
}).done(function(response) {
|
||||
var selected_values;
|
||||
window.mailpoetSegments.push({
|
||||
id: response.data.id,
|
||||
name: response.data.name,
|
||||
subscriberCount: 0
|
||||
});
|
||||
|
||||
var selected_values = segmentSelectElement.val();
|
||||
selected_values = segmentSelectElement.val();
|
||||
if (selected_values === null) {
|
||||
selected_values = [response.data.id];
|
||||
} else {
|
||||
@ -633,16 +667,21 @@ define(
|
||||
function (subscribers, options) {
|
||||
var displayedColumns = [];
|
||||
var displayedColumnsIds = [];
|
||||
var i;
|
||||
var columnData;
|
||||
var columnId;
|
||||
var headerName;
|
||||
var headerNameMatch;
|
||||
// go through all elements of the first row in subscribers data
|
||||
for (var i in subscribers.subscribers[0]) {
|
||||
var columnData = subscribers.subscribers[0][i];
|
||||
var columnId = 'ignore'; // set default column type
|
||||
for (i in subscribers.subscribers[0]) {
|
||||
columnData = subscribers.subscribers[0][i];
|
||||
columnId = 'ignore'; // set default column type
|
||||
// if the column is not undefined and has a valid e-mail, set type as email
|
||||
if (columnData % 1 !== 0 && window.emailRegex.test(columnData)) {
|
||||
columnId = 'email';
|
||||
} else if (subscribers.header) {
|
||||
var headerName = subscribers.header[i];
|
||||
var headerNameMatch = window.mailpoetColumns.map(function (el) {
|
||||
headerName = subscribers.header[i];
|
||||
headerNameMatch = window.mailpoetColumns.map(function (el) {
|
||||
return el.name;
|
||||
}).indexOf(headerName);
|
||||
// set column type using header
|
||||
@ -821,12 +860,13 @@ define(
|
||||
|
||||
// filter subscribers' data to detect dates, emails, etc.
|
||||
function filterSubscribers() {
|
||||
var subscribersClone = jQuery.extend(true, {}, subscribers);
|
||||
var preventNextStep = false;
|
||||
var displayedColumns;
|
||||
jQuery(
|
||||
'[data-id="notice_invalidEmail"], [data-id="notice_invalidDate"]')
|
||||
.remove();
|
||||
var subscribersClone = jQuery.extend(true, {}, subscribers);
|
||||
var preventNextStep = false;
|
||||
var displayedColumns = jQuery.map(
|
||||
displayedColumns = jQuery.map(
|
||||
jQuery('.mailpoet_subscribers_column_data_match'), function (element, elementIndex) {
|
||||
var columnId = jQuery(element).data('column-id');
|
||||
var validationRule = jQuery(element).data('validation-rule');
|
||||
@ -835,6 +875,11 @@ define(
|
||||
});
|
||||
// iterate through the object of mailpoet columns
|
||||
jQuery.map(window.mailpoetColumns, function (column, columnIndex) {
|
||||
var firstRowData;
|
||||
var validationRule;
|
||||
var testedFormat;
|
||||
var format;
|
||||
var allowedDateFormats;
|
||||
// check if the column id matches the selected id of one of the
|
||||
// subscriber's data columns
|
||||
var matchedColumn = _.find(displayedColumns, function (data) { return data.id === column.id; });
|
||||
@ -858,7 +903,7 @@ define(
|
||||
}
|
||||
// DATE filter: if column type is date, check if we can recognize it
|
||||
if (column.type === 'date' && matchedColumn) {
|
||||
var allowedDateFormats = [
|
||||
allowedDateFormats = [
|
||||
Moment.ISO_8601,
|
||||
'YYYY/MM/DD',
|
||||
'MM/DD/YYYY',
|
||||
@ -869,9 +914,8 @@ define(
|
||||
'YYYY/MM',
|
||||
'YYYY'
|
||||
];
|
||||
var firstRowData = subscribersClone.subscribers[0][matchedColumn.index];
|
||||
var validationRule = false;
|
||||
var testedFormat;
|
||||
firstRowData = subscribersClone.subscribers[0][matchedColumn.index];
|
||||
validationRule = false;
|
||||
// check if date exists
|
||||
if (firstRowData.trim() === '') {
|
||||
subscribersClone.subscribers[0][matchedColumn.index] =
|
||||
@ -882,7 +926,7 @@ define(
|
||||
preventNextStep = true;
|
||||
}
|
||||
else {
|
||||
for (var format in allowedDateFormats) {
|
||||
for (format in allowedDateFormats) {
|
||||
testedFormat = allowedDateFormats[format];
|
||||
if (Moment(firstRowData, testedFormat, true).isValid()) {
|
||||
validationRule = (typeof(testedFormat) === 'function') ?
|
||||
@ -900,8 +944,8 @@ define(
|
||||
jQuery.map(subscribersClone.subscribers, function (dataSubscribers, index) {
|
||||
var data = dataSubscribers;
|
||||
var rowData = data[matchedColumn.index];
|
||||
if (index === fillerPosition || rowData.trim() === '') return;
|
||||
var date = Moment(rowData, testedFormat, true);
|
||||
if (index === fillerPosition || rowData.trim() === '') return;
|
||||
// validate date
|
||||
if (date.isValid()) {
|
||||
data[matchedColumn.index] = new Handlebars.SafeString(
|
||||
@ -960,11 +1004,6 @@ define(
|
||||
});
|
||||
|
||||
nextStepButton.off().on('click', function () {
|
||||
if (jQuery(this).hasClass('button-disabled')) {
|
||||
return;
|
||||
}
|
||||
MailPoet.Modal.loading(true);
|
||||
|
||||
var columns = {};
|
||||
var queue = new jQuery.AsyncQueue();
|
||||
var batchNumber = 0;
|
||||
@ -977,7 +1016,14 @@ define(
|
||||
errors: [],
|
||||
segments: []
|
||||
};
|
||||
var splitSubscribers = function (subscribers, size) {
|
||||
var subscribers;
|
||||
var splitSubscribers;
|
||||
|
||||
if (jQuery(this).hasClass('button-disabled')) {
|
||||
return;
|
||||
}
|
||||
MailPoet.Modal.loading(true);
|
||||
splitSubscribers = function (subscribers, size) {
|
||||
return subscribers.reduce(function (res, item, index) {
|
||||
if (index % size === 0) {
|
||||
res.push([]);
|
||||
@ -986,7 +1032,7 @@ define(
|
||||
return res;
|
||||
}, []);
|
||||
};
|
||||
var subscribers = splitSubscribers(window.importData.step1.subscribers, batchSize);
|
||||
subscribers = splitSubscribers(window.importData.step1.subscribers, batchSize);
|
||||
|
||||
_.each(jQuery('select.mailpoet_subscribers_column_data_match'),
|
||||
function (column, columnIndex) {
|
||||
@ -1057,6 +1103,9 @@ define(
|
||||
});
|
||||
|
||||
router.on('route:step3', function () {
|
||||
var subscribersDataImportResultsTemplate;
|
||||
var exportMenuElement;
|
||||
var importResults;
|
||||
if (typeof (window.importData.step2) === 'undefined') {
|
||||
router.navigate('step2', { trigger: true });
|
||||
return;
|
||||
@ -1075,10 +1124,10 @@ define(
|
||||
});
|
||||
|
||||
// display statistics
|
||||
var subscribersDataImportResultsTemplate =
|
||||
subscribersDataImportResultsTemplate =
|
||||
Handlebars.compile(jQuery('#subscribers_data_import_results_template').html());
|
||||
var exportMenuElement = jQuery('span.mailpoet_export');
|
||||
var importResults = {
|
||||
exportMenuElement = jQuery('span.mailpoet_export');
|
||||
importResults = {
|
||||
created: (window.importData.step2.created)
|
||||
? MailPoet.I18n.t('subscribersCreated')
|
||||
.replace('%1$s', '<strong>' + window.importData.step2.created.toLocaleString() + '</strong>')
|
||||
|
Reference in New Issue
Block a user