(function ($) {
"use strict";
var wp_shortcode = {
// ### Find the next matching shortcode
//
// Given a shortcode `tag`, a block of `text`, and an optional starting
// `index`, returns the next matching shortcode or `undefined`.
//
// Shortcodes are formatted as an object that contains the match
// `content`, the matching `index`, and the parsed `shortcode` object.
next: function (tag, text, index) {
var re = wp_shortcode.regexp(tag),
match, result;
re.lastIndex = index || 0;
match = re.exec(text);
if (!match) {
return;
}
// If we matched an escaped shortcode, try again.
if ('[' === match[1] && ']' === match[7]) {
return wp_shortcode.next(tag, text, re.lastIndex);
}
result = {
index: match.index,
content: match[0],
shortcode: wp_shortcode.fromMatch(match)
};
// If we matched a leading `[`, strip it from the match
// and increment the index accordingly.
if (match[1]) {
result.content = result.content.slice(1);
result.index++;
}
// If we matched a trailing `]`, strip it from the match.
if (match[7]) {
result.content = result.content.slice(0, -1);
}
return result;
},
// ### Replace matching shortcodes in a block of text
//
// Accepts a shortcode `tag`, content `text` to scan, and a `callback`
// to process the shortcode matches and return a replacement string.
// Returns the `text` with all shortcodes replaced.
//
// Shortcode matches are objects that contain the shortcode `tag`,
// a shortcode `attrs` object, the `content` between shortcode tags,
// and a boolean flag to indicate if the match was a `single` tag.
replace: function (tag, text, callback) {
return text.replace(wp_shortcode.regexp(tag), function (match, left, tag, attrs, slash, content, closing, right) {
// If both extra brackets exist, the shortcode has been
// properly escaped.
if (left === '[' && right === ']') {
return match;
}
// Create the match object and pass it through the callback.
var result = callback(wp_shortcode.fromMatch(arguments));
// Make sure to return any of the extra brackets if they
// weren't used to escape the shortcode.
return result ? left + result + right : match;
});
},
// ### Generate a string from shortcode parameters
//
// Creates a `wp_shortcode` instance and returns a string.
//
// Accepts the same `options` as the `wp_shortcode()` constructor,
// containing a `tag` string, a string or object of `attrs`, a boolean
// indicating whether to format the shortcode using a `single` tag, and a
// `content` string.
string: function (options) {
return new wp_shortcode(options).string();
},
// ### Generate a RegExp to identify a shortcode
//
// The base regex is functionally equivalent to the one found in
// `get_shortcode_regex()` in `wp-includes/shortcodes.php`.
//
// Capture groups:
//
// 1. An extra `[` to allow for escaping shortcodes with double `[[]]`
// 2. The shortcode name
// 3. The shortcode argument list
// 4. The self closing `/`
// 5. The content of a shortcode when it wraps some content.
// 6. The closing tag.
// 7. An extra `]` to allow for escaping shortcodes with double `[[]]`
regexp: _.memoize(function (tag) {
return new RegExp('\\[(\\[?)(' + tag + ')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)', 'g');
}),
// ### Parse shortcode attributes
//
// Shortcodes accept many types of attributes. These can chiefly be
// divided into named and numeric attributes:
//
// Named attributes are assigned on a key/value basis, while numeric
// attributes are treated as an array.
//
// Named attributes can be formatted as either `name="value"`,
// `name='value'`, or `name=value`. Numeric attributes can be formatted
// as `"value"` or just `value`.
attrs: _.memoize(function (text) {
var named = {},
numeric = [],
pattern, match;
// This regular expression is reused from `shortcode_parse_atts()`
// in `wp-includes/shortcodes.php`.
//
// Capture groups:
//
// 1. An attribute name, that corresponds to...
// 2. a value in double quotes.
// 3. An attribute name, that corresponds to...
// 4. a value in single quotes.
// 5. An attribute name, that corresponds to...
// 6. an unquoted value.
// 7. A numeric attribute in double quotes.
// 8. An unquoted numeric attribute.
pattern = /([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*'([^']*)'(?:\s|$)|([\w-]+)\s*=\s*([^\s'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/g;
// Map zero-width spaces to actual spaces.
text = text.replace(/[\u00a0\u200b]/g, ' ');
// Match and normalize attributes.
while ((match = pattern.exec(text))) {
if (match[1]) {
named[ match[1].toLowerCase() ] = match[2];
} else if (match[3]) {
named[ match[3].toLowerCase() ] = match[4];
} else if (match[5]) {
named[ match[5].toLowerCase() ] = match[6];
} else if (match[7]) {
numeric.push(match[7]);
} else if (match[8]) {
numeric.push(match[8]);
}
}
return {
named: named,
numeric: numeric
};
}),
// ### Generate a Shortcode Object from a RegExp match
// Accepts a `match` object from calling `regexp.exec()` on a `RegExp`
// generated by `wp_shortcode.regexp()`. `match` can also be set to the
// `arguments` from a callback passed to `regexp.replace()`.
fromMatch: function (match) {
var type;
if (match[4]) {
type = 'self-closing';
} else if (match[6]) {
type = 'closed';
} else {
type = 'single';
}
return new wp_shortcode({
tag: match[2],
attrs: match[3],
type: type,
content: match[5]
});
}
};
// Shortcode Objects
// -----------------
//
// Shortcode objects are generated automatically when using the main
// `wp_shortcode` methods: `next()`, `replace()`, and `string()`.
//
// To access a raw representation of a shortcode, pass an `options` object,
// containing a `tag` string, a string or object of `attrs`, a string
// indicating the `type` of the shortcode ('single', 'self-closing', or
// 'closed'), and a `content` string.
wp_shortcode = _.extend(function (options) {
_.extend(this, _.pick(options || {}, 'tag', 'attrs', 'type', 'content'));
var attrs = this.attrs;
// Ensure we have a correctly formatted `attrs` object.
this.attrs = {
named: {},
numeric: []
};
if (!attrs) {
return;
}
// Parse a string of attributes.
if (_.isString(attrs)) {
this.attrs = wp_shortcode.attrs(attrs);
// Identify a correctly formatted `attrs` object.
} else if (_.isEqual(_.keys(attrs), ['named', 'numeric'])) {
this.attrs = attrs;
// Handle a flat object of attributes.
} else {
_.each(options.attrs, function (value, key) {
this.set(key, value);
}, this);
}
}, wp_shortcode);
_.extend(wp_shortcode.prototype, {
// ### Get a shortcode attribute
//
// Automatically detects whether `attr` is named or numeric and routes
// it accordingly.
get: function (attr) {
return this.attrs[ _.isNumber(attr) ? 'numeric' : 'named' ][ attr ];
},
// ### Set a shortcode attribute
//
// Automatically detects whether `attr` is named or numeric and routes
// it accordingly.
set: function (attr, value) {
this.attrs[ _.isNumber(attr) ? 'numeric' : 'named' ][ attr ] = value;
return this;
},
// ### Transform the shortcode match into a string
string: function () {
var text = '[' + this.tag;
_.each(this.attrs.numeric, function (value) {
if (/\s/.test(value)) {
text += ' "' + value + '"';
} else {
text += ' ' + value;
}
});
_.each(this.attrs.named, function (value, name) {
text += ' ' + name + '="' + value + '"';
});
// If the tag is marked as `single` or `self-closing`, close the
// tag and ignore any additional content.
if ('single' === this.type) {
return text + ']';
} else if ('self-closing' === this.type) {
return text + ' /]';
}
// Complete the opening tag.
text += ']';
if (this.content) {
text += this.content;
}
// Add the closing tag.
return text + '[/' + this.tag + ']';
}
});
function on_ready_first(completed) {
var fired = false;
azh.$.holdReady(true);
if (azh.document.get(0).readyState === "complete") {
setTimeout(function () {
if (!fired) {
fired = true;
completed();
azh.$.holdReady(false);
}
});
} else if (azh.document.get(0).addEventListener) {
azh.document.get(0).addEventListener("DOMContentLoaded", function () {
if (!fired) {
fired = true;
completed();
azh.$.holdReady(false);
}
}, false);
azh.window.get(0).addEventListener("load", function () {
if (!fired) {
fired = true;
completed();
azh.$.holdReady(false);
}
}, false);
} else {
azh.document.get(0).attachEvent("onreadystatechange", function () {
if (!fired) {
fired = true;
completed();
azh.$.holdReady(false);
}
});
azh.window.get(0).attachEvent("onload", function () {
if (!fired) {
fired = true;
completed();
azh.$.holdReady(false);
}
});
}
}
function makeid() {
var text = "id";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
function rgb2hex(rgb) {
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
}
return "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}
function hex2rgb(hex) {
if (hex.lastIndexOf('#') > -1) {
hex = hex.replace(/#/, '0x');
} else {
hex = '0x' + hex;
}
var r = hex >> 16;
var g = (hex & 0x00FF00) >> 8;
var b = hex & 0x0000FF;
return [r, g, b];
}
function hexToRgbA(hex, alpha) {
var c;
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
c = hex.substring(1).split('');
if (c.length === 3) {
c = [c[0], c[0], c[1], c[1], c[2], c[2]];
}
c = '0x' + c.join('');
return 'rgba(' + [(c >> 16) & 255, (c >> 8) & 255, c & 255].join(',') + ',' + alpha + ')';
}
return hex;
}
function htmlEncode(value) {
return $('
').text(value).html();
}
function htmlDecode(value) {
return $('').html(value).text();
}
function html_beautify(html) {
var results = '';
try {
var level = 0;
var indent_size = 1;
var ignore = false;
var ignore_tags = {
// "script": true,
// "style": true
};
AZHParser(html, {
start: function (tag, attrs, unary) {
if (ignore_tags[tag]) {
ignore = true;
return;
}
results += Array(level * indent_size + 1).join("\t") + "<" + tag;
for (var i = 0; i < attrs.length; i++) {
if (attrs[i].value.indexOf('"') >= 0 && attrs[i].value.indexOf("'") < 0) {
results += " " + attrs[i].name + "='" + attrs[i].value + "'";
} else {
results += " " + attrs[i].name + '="' + attrs[i].escaped + '"';
}
}
results += (unary ? "/" : "") + ">\n";
if (!unary) {
level++;
}
},
end: function (tag) {
if (ignore_tags[tag]) {
ignore = false;
return;
}
level--;
results += Array(level * indent_size + 1).join("\t") + "" + tag + ">\n";
},
chars: function (text) {
if ($.trim(text) && !ignore) {
results += Array(level * indent_size + 1).join("\t") + text.replace(/[\t\r\n]+/g, '').replace(/(^|}}|%}|>)(\s{2,})(<|{%|{{)/g, "$1\n$2$3") + "\n";
}
},
comment: function (text) {
results += Array(level * indent_size + 1).join("\t") + '\n";
}
});
} catch (e) {
return false;
}
return results;
}
function html_uglify(html) {
var results = '';
try {
var ignore = false;
var ignore_tags = {
// "script": true,
// "style": true
};
AZHParser(html, {
start: function (tag, attrs, unary) {
if (ignore_tags[tag]) {
ignore = true;
return;
}
results += "<" + tag;
for (var i = 0; i < attrs.length; i++) {
var value = attrs[i].value;
if (/&[^\s]+;/.test(value)) {
value = htmlDecode(value);
}
if (value.indexOf('>') || value.indexOf('<')) {
value = htmlEncode(value);
}
if (value.indexOf('"') >= 0 && value.indexOf("'") < 0) {
results += " " + attrs[i].name + "='" + value + "'";
} else {
value = value.replace(/(^|[^\\])"/g, '$1\\\"');
results += " " + attrs[i].name + '="' + value + '"';
}
}
results += (unary ? "/" : "") + ">";
},
end: function (tag) {
if (ignore_tags[tag]) {
ignore = false;
return;
}
results += "" + tag + ">";
},
chars: function (text) {
if ($.trim(text) && !ignore) {
results += text.replace(/[\t\r\n]+/g, '');
}
},
comment: function (text) {
results += '\n";
}
});
} catch (e) {
return false;
}
return results;
}
function make_html_safe(html) {
html = html.replace(/');
return html;
}
var $window = $(window);
var $document = $(document);
var $body = $('body');
window.azh = $.extend({}, window.azh);
if(!azh.author_url) {
azh.author_url = 'http://azexo.com';
}
azh.$ = $;
azh.window = $window;
azh.window_off = function (event) {
$window.off(event);
azh.window.off(event);
return azh;
};
azh.window_on = function (event, callback) {
$window.on(event, function (e) {
callback(e);
});
if ($window.get(0) != azh.window.get(0)) {
azh.window.on(event, function (e) {
callback(e);
});
}
return azh;
};
azh.document = $document;
azh.document_off = function (event) {
$document.off(event);
azh.document.off(event);
return azh;
};
azh.document_on = function (event, callback) {
$document.on(event, function (e) {
callback(e);
});
if ($window.get(0) != azh.window.get(0)) {
azh.document.on(event, function (e) {
callback(e);
});
}
return azh;
};
azh.body = $body;
azh.body_off = function (event) {
$body.off(event);
azh.body.off(event);
return azh;
};
azh.body_on = function (event, callback) {
$body.on(event, function (e) {
callback(e);
});
if ($window.get(0) != azh.window.get(0)) {
azh.body.on(event, function (e) {
callback(e);
});
}
return azh;
};
azh.section_controlled_device_prefixes = ['lg', 'md'];
azh.element_controlled_device_prefixes = ['lg', 'md', 'sm'];
azh.column_offset_patterns = {
'lg': /[ '"-]col-lg-offset-([0-9]?[0-9])[ '"]/gi,
'md': /[ '"-]col-md-offset-([0-9]?[0-9])[ '"]/gi,
'sm': /[ '"-]col-sm-offset-([0-9]?[0-9])[ '"]/gi,
'xs': /[ '"-]col-xs-offset-([0-9]?[0-9])[ '"]/gi
};
azh.column_width_patterns = {
'lg': /[\w\d-_]+-col-lg-([0-9]?[0-9])/gi,
'md': /[\w\d-_]+-col-md-([0-9]?[0-9])/gi,
'sm': /[\w\d-_]+-col-sm-([0-9]?[0-9])/gi,
'xs': /[\w\d-_]+-col-xs-([0-9]?[0-9])/gi
};
azh.do_replaces = function (content) {
content = content.replace(/\[\[([^\]]+)\]\]/g, '');
content = content.replace(/\[azh_text\]/g, '');
content = content.replace(/\[\/azh_text\]/g, '');
content = content.replace(/href="\/"/g, 'href="' + azh.author_url + '"');
content = content.replace(/href="#"/g, 'href="' + azh.author_url + '"');
var regexp = /{{id-(\d+)}}/g;
var match = null;
var numbers = {};
while ((match = regexp.exec(content)) != null && match.length == 2) {
numbers[match[1]] = true;
}
for (var number in numbers) {
var id = 'id' + makeid();
regexp = new RegExp('{{id-' + number + '}}', 'g');
content = content.replace(regexp, id);
}
return content;
};
azh.clipboard = false;
azh.sections_cache = {};
azh.notify = function (message) {
var $notify = false;
if ($('.azh-notify').length) {
$notify = $('.azh-notify');
} else {
$notify = $('').appendTo($body);
}
$notify.text(message);
$notify.slideDown(function () {
setTimeout(function () {
$notify.slideUp();
}, 3000);
});
};
azh.click_not_hide_contextmenu = '.azh-devices, #wp-link-wrap, .media-modal, .azh-icon-select-dialog, .select2-container';
azh.unlimited_max_wrapper = 'form, .az-form, .az-swiper, .az-template';
azh.possible_controls_selector_start = ['[data-element]', '.azh-row', 'form'];
azh.standard_elements_start_classes = $.extend({}, azh.standard_elements_start_classes);
azh.dynamic_content = '.az-liquid-element';
if ('cssjs' in window) {
azh.cssjs = new cssjs();
}
azh.convert_to_embed = function (url) {
function youtube_parser(url) {
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}
function vimeo_parser(url) {
var match = /vimeo\.\w+\/(\d+)/i.exec(url);
if (match) {
return match[1];
}
return false;
}
var id = youtube_parser(url);
if (id) {
url = '//www.youtube.com/embed/' + id;
url = set_url_argument(url, 'playlist', id);
return url;
}
var id = vimeo_parser(url);
if (id) {
return '//player.vimeo.com/video/' + id;
}
return url;
}
function get_full_width() {
var full_width = azh.body.prop("clientWidth");
if ($window.get(0) != azh.window.get(0)) {
full_width = azh.window.prop("innerWidth");
}
return full_width;
}
var no_image = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAMAAABHPGVmAAAB8lBMVEUAAABcXFxmZmZnZ2eZmZmampqrq6uwsLC9vb2+vr7AwMDBwcHCwsLExMTFxcXGxsbJycnS0tLT09PZ2dna2trb29vd3d3f39/g4ODl5eXp6enq6urr6+vw8PDy8vLz8/P19fX4+Pj6+vr9/f3////V1dX////////S0tLT09PT09PU1NTAwMDCwsLCwsLOzs7h4eHi4uLk5OTl5eXm5ubPz8/a2trb29ve3t6+vr7R0dHQ0NDU1NTS0tLT09PU1NTDw8PAwMDBwcHR0dHR0dHU1NSoqKjS0tLR0dHS0tLT09POzs7Pz8/Q0NDOzs7Pz8/Ozs7Pz8/Q0NDOzs7Pz8+oqKioqKjPz8+mpqanp6fOzs7Pz8/Q0NDOzs7Pz8+jo6Ojo6OioqLR0dHS0tKjo6PNzc3Ozs6hoaHOzs7Ozs6ioqLNzc3Nzc3Nzc3Nzc3Nzc3Nzc3MzMzNzc2goKDNzc2goKDNzc3Ozs7Nzc3Nzc3Nzc2enp6fn5/Nzc3MzMzNzc3Nzc2enp6enp6enp7Nzc3Nzc3Nzc2dnZ2enp6cnJycnJzMzMzMzMzMzMzNzc3MzMzNzc3Nzc2dnZ2bm5ucnJzNzc3Nzc2bm5ucnJycnJzMzMzMzMzMzMzMzMzMzMzMzMyamprMzMyYmJiZmZmamprMzMyJ6HxFAAAAonRSTlMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgIDBgYLCwwMDQ4ODg4ODg8PDw8RFBUVFhYWGhsbGxwcJiYnJyc2NjY3Nzg4OTo6P0BAQkJGRkxOTlVWV1dXWFpaXmJjcHd4eXt9foODhYWGhoaeoKGoqKiqqquwsbKyubq7u8HCxs3Oz9DQ0dTh4u3v8PDy9Pb4+fr7/f5KuRpkAAAEXUlEQVR4Ae2Z6VcUVxDFr1kUYaKORjQuiQpxXDRmiUlQY3DBRaMCmowLjLssw4IiKigusqhxj9EkgCwN9X/mve4+3V1Mk65uOPOpf2c+zXnn3HNfvXqvqhp5JSYmJiYm5nMoKqprLl6sqa6w/5hZPgWOtPW/I4d3/W1H9N8zyZl75MO9M5ghvsDa1gGagoHWtWrBDHCTXN6+6uvt7Xv1llxuYrosxJVhsnjf3XAIDvvru9+TxfAVtWw6bHhKFl1VwGx4mA8c7SSLpxswDdJjpBlpKkUxcigGspbPsTQi8hmyZHID/8M1Msmq5RFYjFukebETqzAly7HjGWluYXEUH3dJ0xIU1YVoJs3dKF5MH0MnIKDK9hIaOx61kLB30IpLSNKkkaoUbDdV0ghDcuOY3ivSZEQqe8yTvDGJEJg5eLxW7gWVZlYiBJetc4WM3It1xi5DymroPH4JRRgvz/U9htUQcoMUvyShyIhVktvc20FAihTXYVEr37GrpEhBRqu+E7GGqdSi9O9xzm8fg2Pucitk6HewCTZu9FNvuMifvgk8AAmLTpMCGu5lsorx+0fggBSnF0HAff1GwUvGo2Lo31RW9Ct2HxJIUbkENhPjf6Wc6Kdun98FlJ99wCxNwGbpYVIgmJJj+j0HXJHxNyk3XwqLgWVzUfbYtmMYrohCv/vHShBIm1rXzUSUipv7J9s76rYggUZT48k33z7xinSrRW0Ipl+ta5jjFWFe1uvo92yehUYdne8SRV8brsiCBrWmH8HoWvQgc+J6cc7Y6L4P8EiJfF/4yU8eERzQFaww7mAizEvGVtlUVKa364cf//BuF2SRr9B1IhcxuQOeLz3Aw9zAQ9eWFQiiWq167SNyKQGe+1vnneNH2OS1WlCNIGrUqr5cEWP3ilPrmJe6leVGrkgfEdUgiAtqVa+PE6CdR78D8HHSS0QXpiHSwaPfLhcRb1f5yjqWL+tOrdgt3y5h4M/N28pzH4lLwsDLj/BDoIfnPu5wEX6Ewyejpqxo02hu7mvEycivlQM+Io/w4b7R3NznIgf1tSK8IOsX5IoYjZi12dyx9Z584SJz2AUZ7qq3aUQCW+o62k/y3A+46mWPloPxuAxzlwHFhWBeJj1av5YIr+HDS5mIw4Oz5cCu87dZ9B2RJZWkgLSQ6IS/iF1I8Oi7TrpYISEviSb8+ecrd8f+jVASYUDSNX3pU/M3mcWdvEwdhgBe86/BiKBMZQX3VQjgNf91ScHNW4dtSQTBav7kz7x1EDVBzxFyx17yJkjWzjVDgBv9Ft7OSRvTyjBejvPGVNxi054CuZch3mLLhwWD2wvEXjTpSGOPwb0IoZKNOMChKgg4MRR+gMNGUc3Bo6iWSKMoNlR7tgPLMSWrsPNF1KEaGw9eC7gdBONBwaBzOAv/QWdp04hg0Cke2XYeBebDw2ygqks8spUPn+v3w+FQAx8+52uMnv8PAvn/tJH/jzT5/9wkJyYmJiYm5j/qBxqDoN+AlgAAAABJRU5ErkJggg==';
var parse_query_string = function (a) {
if (a == "") {
return {};
}
var b = {};
for (var i = 0; i < a.length; ++i) {
var p = a[i].split('=');
if (p.length != 2) {
continue;
}
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
};
var set_url_argument = function (url, argument, value) {
url = url.split('?');
var args = {};
if (url.length === 2) {
args = parse_query_string(url[1].split('&'));
}
var query = {};
for (var arg in args) {
if (arg === argument) {
query[arg] = encodeURIComponent(value);
} else {
query[arg] = encodeURIComponent(args[arg]);
}
}
if (!(argument in args)) {
query[argument] = encodeURIComponent(value);
}
query = Object.entries(query).map(function callback(currentValue, index, array) {
return currentValue[0] + '=' + currentValue[1];
});
if (url.length === 1) {
url.push(query.join('&'));
} else {
url[1] = query.join('&');
}
if (url[1] !== '') {
url = url.join('?');
} else {
url = url[0];
}
return url;
};
var get_url_argument = function (url, argument) {
url = url.split('?');
var args = {};
if (url.length === 2) {
args = parse_query_string(url[1].split('&'));
}
if (argument in args) {
return args[argument];
}
return false;
};
var style_in_page = function (css, verbose) {
if (typeof getComputedStyle == "undefined")
getComputedStyle = function (elem) {
return elem.currentStyle;
}
var who, hoo, values = [], val,
nodes = azh.document.get(0).body.getElementsByTagName('*'),
L = nodes.length;
for (var i = 0; i < L; i++) {
who = nodes[i];
if (who.style) {
hoo = '#' + (who.id || who.nodeName + '(' + i + ')');
val = who.style.fontFamily || getComputedStyle(who, '')[css];
if (val) {
if (verbose)
values.push([hoo, val]);
else if (values.indexOf(val) == -1)
values.push(val);
}
var val_before = getComputedStyle(who, ':before')[css];
if (val_before) {
if (verbose)
values.push([hoo, val_before]);
else if (values.indexOf(val_before) == -1)
values.push(val_before);
}
var val_after = getComputedStyle(who, ':after')[css];
if (val_after) {
if (verbose)
values.push([hoo, val_after]);
else if (values.indexOf(val_after) == -1)
values.push(val_after);
}
}
}
return values;
};
var fixed_inner_html = '.az-fixed-inner-html, .az-gmap';
var empty_inner_html = '.az-empty-inner-html, iframe, [data-shortcode], [data-element*="shortcode"]';
var store_html = function (wrapper) {
var $wrapper = azh.$(wrapper);
var tag = wrapper.outerHTML;
if (wrapper.innerHTML !== '') {
tag = wrapper.outerHTML.replace('>' + wrapper.innerHTML + '<', '><');
}
if (tag.indexOf('>') >= 0) {
tag = tag.split('>');
$wrapper.data('azh-open-tag', tag[0] + '>');
$wrapper.data('azh-close-tag', '' + tag[1]);
} else {
$wrapper.data('azh-open-tag', tag);
}
if ($wrapper.is(fixed_inner_html)) {
$wrapper.data('azh-fixed-inner-html', wrapper.innerHTML);
}
if (!$wrapper.is(empty_inner_html)) {
$wrapper.children().each(function () {
store_html(this);
});
}
};
function hashCode(str) {
var hash = 0, i, chr;
if (str.length === 0)
return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
var extract_html = function ($wrapper, reset_shortcodes) {
var html = '';
if ($wrapper.data('azh-fixed-inner-html') !== undefined) {
html = html + $wrapper.data('azh-fixed-inner-html');
} else {
if (!$wrapper.is(empty_inner_html)) {
$wrapper.contents().each(function () {
var $this = azh.$(this);
if ($this.is('[data-element*="shortcode"]')) {
var instance = $this.attr('data-element');
var shortcode = azh.shortcode_instances[instance];
if (shortcode) {
if (reset_shortcodes) {
set_stored_attribute($this, 'data-element', 'shortcode');
}
html = html + $this.data('azh-open-tag') + shortcode + $this.data('azh-close-tag');
if (reset_shortcodes) {
set_stored_attribute($this, 'data-element', instance);
}
}
} else {
if (this.nodeType === 1 && $this.data('azh-open-tag') && $this.data('azh-close-tag')) {
html = html + $this.data('azh-open-tag') + extract_html(azh.$(this), reset_shortcodes) + $this.data('azh-close-tag');
}
if (this.nodeType === 1 && $this.data('azh-open-tag') && !$this.data('azh-close-tag')) {
html = html + $this.data('azh-open-tag');
}
if (this.nodeType === 1 && !$this.data('azh-open-tag') && !$this.data('azh-close-tag')) {
html = html + extract_html(azh.$(this), reset_shortcodes);
}
if (this.nodeType === 3) {
if($this.parent().is('[contenteditable="true"]:not(script):not(style)')) {
html = html + htmlEncode(this.textContent);
} else {
html = html + this.textContent;
}
}
if (this.nodeType === 8) {
html = html + '';
}
}
});
}
}
return html;
};
var recognition = function ($wrapper) {
function get_subtree_path($element) {
var attributes = [];
azh.$($element.get(0).attributes).each(function () {
attributes.push(this.nodeName);
});
var path = {
tagName: $element.prop('tagName'),
attributes: attributes,
css: $element.css(['display', 'float', 'position']),
children: []
};
$element.children().each(function () {
path.children.push(get_subtree_path(azh.$(this)));
});
return path;
}
function search_cloneable($element, hashes) {
var depth = 1;
var children_hash = false;
var cloneable = true;
if (!$element.is(empty_inner_html)) {
$element.children().each(function () {
var d = search_cloneable(azh.$(this));
if (depth < d) {
depth = d + 1;
}
if (depth < 10) {
var hash = hashCode(JSON.stringify(get_subtree_path(azh.$(this), 0)));
if (!children_hash) {
children_hash = hash;
} else {
if (children_hash != hash) {
cloneable = false;
return false;
}
}
}
});
if ($element.children().length > 1 && children_hash && cloneable) {
if (!$element.attr('data-cloneable') && !$element.attr('data-cloneable-inline')) {
if ($element.find('> [class*="-col-"]').length === 0) {
$element.attr('data-cloneable', '');
set_stored_attribute($element, 'data-cloneable', '');
}
}
}
}
return depth;
}
//cloneable
$wrapper.find('ul, ol').each(function () {
var $element = azh.$(this);
$element.attr('data-cloneable', '');
set_stored_attribute($element, 'data-cloneable', '');
});
search_cloneable($wrapper);
//set styles inline
var doc = azh.document.get(0);
for (var s = 0; s < doc.styleSheets.length; s++) {
try {
var rules = doc.styleSheets[s].rules || doc.styleSheets[s].cssRules;
if (rules) {
for (var r = 0; r < rules.length; r++) {
if ('selectorText' in rules[r] && rules[r].selectorText.indexOf('#') === 0 && rules[r].selectorText.indexOf(' ') < 0 && rules[r].selectorText.indexOf(',') < 0) {
var $element = azh.$(rules[r].selectorText);
if ($element.length === 1 && azh.$.contains($wrapper, $element)) {
var match = /[\S\s]*{([^{}]*)}[\S\s]*/.exec(rules[r].cssText);
if (match.length > 1) {
var style = match[1];
var properties = style.split(';');
for (var p = 0; p < properties.length; p++) {
var property = properties[p].split(':');
$element.css(property[0], property[1]);
set_stored_style($element, property[0], property[1]);
}
}
}
}
//background-image
}
}
} catch (e) {
}
}
//icons
//tabs
};
var shortcodes_refresh = function ($wrapper) {
$wrapper.find('[data-shortcode]').addBack().filter('[data-shortcode]').each(function () {
var $shortcode = azh.$(this);
var instance = $shortcode.attr('data-shortcode');
var shortcode = azh.shortcode_instances[instance];
$.post(azh.ajaxurl, {
'action': 'azh_update_shortcode',
'post_id': azh.post_id,
'instance': instance,
'shortcode': shortcode
}, function (data) {
if (data) {
var $section = $shortcode.closest('[data-section]');
$shortcode.replaceWith(data);
$shortcode.find('a[href]').on('click', function (event) {
event.preventDefault();
});
$shortcode = $section.find('[data-shortcode="' + instance + '"]');
store_html($shortcode.get(0));
azh.section_customization_init($section);
}
});
});
$wrapper.find('[data-element*="shortcode"]').addBack().filter('[data-element*="shortcode"]').each(function () {
var $shortcode = azh.$(this);
var instance = $shortcode.attr('data-element');
var shortcode = azh.shortcode_instances[instance];
if (instance === 'shortcode' || !shortcode) {
instance = 'shortcode: ' + makeid();
shortcode = $shortcode.html();
azh.shortcode_instances[instance] = shortcode;
$shortcode.attr('data-element', instance);
set_stored_attribute($shortcode, 'data-element', instance);
$shortcode.data('element', instance);
}
$.post(azh.ajaxurl, {
'action': 'azh_update_shortcode',
'post_id': azh.post_id,
'instance': instance,
'shortcode': shortcode
}, function (data) {
if (data) {
$shortcode.html(data);
$shortcode.find('a[href]').on('click', function (event) {
event.preventDefault();
});
}
});
});
};
function clone_shortcodes($wrapper) {
// $wrapper.find('[data-shortcode]').addBack().filter('[data-shortcode]').each(function() {
// var $shortcode = azh.$(this);
// var instance = $shortcode.data('shortcode');
// var shortcode = azh.shortcode_instances[instance];
// if (shortcode) {
// instance = makeid();
// azh.shortcode_instances[instance] = shortcode;
// $shortcode.attr('shortcode', instance);
// set_stored_attribute($shortcode, 'shortcode', instance);
// }
// });
$wrapper.find('[data-element*="shortcode"]').addBack().filter('[data-element*="shortcode"]').each(function () {
var $shortcode = azh.$(this);
var instance = $shortcode.attr('data-element');
var shortcode = azh.shortcode_instances[instance];
if (shortcode) {
instance = 'shortcode: ' + makeid();
azh.shortcode_instances[instance] = shortcode;
$shortcode.attr('data-element', instance);
set_stored_attribute($shortcode, 'data-element', instance);
$shortcode.data('element', instance);
}
});
}
var section_refresh = function ($wrapper, html) {
$('.wp-picker-open').trigger('click');
var scrollTop = azh.window.scrollTop();
save_utility_state();
var saved_utility = $wrapper.is('.azh-saved-utility');
if (html === undefined) {
azh.liquid_prepare($wrapper);
html = extract_html($wrapper.wrap('').parent());
}
var $section = azh.$(html);
shortcodes_refresh($section);
$section.find('.azh-over').removeClass('azh-over');
store_html($section.get(0));
remove_visible_controls($wrapper);
$section.data('azh-section-path', $wrapper.data('azh-section-path'));
$section.data('azh-section-path').data('azh-section', $section);
$wrapper.replaceWith($section);
if (saved_utility) {
$wrapper.addClass('azh-saved-utility');
}
azh.section_customization_init($section);
if (azh.frontend_init) {
azh.frontend_init($section);
}
azh.window.trigger('resize');
azh.window.scrollTop(scrollTop);
$('.azh-context-menu').hide();
_.defer(restore_utility_state);
return $section;
};
var set_stored_tag = function ($element, tag) {
$element.data('azh-open-tag', $element.data('azh-open-tag').replace(/<\w+ /, '<' + tag + ' '));
$element.data('azh-close-tag', $element.data('azh-close-tag').replace(/<\/\w+>/, '' + tag + '>'));
};
var get_stored_attribute = function ($element, attribute) {
var value = '';
AZHParser($element.data('azh-open-tag'), {
start: function (tag, attrs, unary) {
for (var i = 0; i < attrs.length; i++) {
if (attrs[i].name === attribute) {
value = attrs[i].value;
break;
}
}
},
end: function (tag) {
//'' + tag + '>';
},
chars: function (text) {
//text;
}
});
return value;
};
var set_stored_attribute = function ($element, attribute, value, pattern, default_value) {
if (typeof value == 'string' && value.indexOf('"') >= 0) {
value = value.replace(/"/g, "'").replace(/\s\s+/g, ' ');
}
var pattern_value = value;
var open_tag = '';
AZHParser($element.data('azh-open-tag'), {
start: function (tag, attrs, unary) {
open_tag += "<" + tag;
var updated = false;
for (var i = 0; i < attrs.length; i++) {
if (attrs[i].name == attribute) {
if (pattern) {
var m = pattern.test(attrs[i].value);
if (m) {
pattern_value = attrs[i].value.replace(pattern, '$1' + value + '$3');
open_tag += " " + attrs[i].name + '="' + pattern_value + '"';
updated = true;
}
} else {
open_tag += " " + attrs[i].name + '="' + value + '"';
updated = true;
}
} else {
if (attrs[i].value.indexOf('"') >= 0 && attrs[i].value.indexOf("'") < 0) {
open_tag += " " + attrs[i].name + "='" + attrs[i].value + "'";
} else {
open_tag += " " + attrs[i].name + '="' + attrs[i].escaped + '"';
}
}
}
if (!updated) {
if (pattern && default_value) {
pattern_value = default_value.replace(pattern, '$1' + value + '$3');
open_tag += " " + attribute + '="' + pattern_value + '"';
} else {
open_tag += " " + attribute + '="' + value + '"';
}
}
open_tag += (unary ? "/" : "") + ">";
},
end: function (tag) {
//'' + tag + '>';
},
chars: function (text) {
//text;
}
});
$element.data('azh-open-tag', open_tag);
azh.change();
return pattern_value;
};
var remove_stored_attribute = function ($element, attribute) {
var open_tag = '';
AZHParser($element.data('azh-open-tag'), {
start: function (tag, attrs, unary) {
open_tag += "<" + tag;
for (var i = 0; i < attrs.length; i++) {
if (attrs[i].name !== attribute) {
if (attrs[i].value.indexOf('"') >= 0 && attrs[i].value.indexOf("'") < 0) {
open_tag += " " + attrs[i].name + "='" + attrs[i].value + "'";
} else {
open_tag += " " + attrs[i].name + '="' + attrs[i].escaped + '"';
}
}
}
open_tag += (unary ? "/" : "") + ">";
},
end: function (tag) {
//'' + tag + '>';
},
chars: function (text) {
//text;
}
});
$element.data('azh-open-tag', open_tag);
azh.change();
};
var add_to_stored_classes = function ($element, class_name) {
var classes = get_stored_attribute($element, 'class');
if (classes) {
classes = classes.split(' ').filter(function (value) {
return value !== ''
});
} else {
classes = [];
}
classes.push($.trim(class_name));
classes = classes.filter(function (value, index, self) {
return self.indexOf(value) === index;
}).join(' ');
set_stored_attribute($element, 'class', classes);
};
var remove_from_stored_classes = function ($element, class_name) {
var classes = get_stored_attribute($element, 'class');
if (classes) {
classes = classes.split(' ').filter(function (value) {
return value !== ''
});
} else {
classes = [];
}
var index = classes.indexOf($.trim(class_name));
if (index > -1) {
classes.splice(index, 1);
}
classes = classes.join(' ');
set_stored_attribute($element, 'class', classes);
};
var get_stored_style = function ($element, property, pattern) {
var style = get_stored_attribute($element, 'style');
var properties = [];
if (style) {
properties = style.split(';');
}
style = '';
var value = '';
$(properties).each(function () {
var match = /\s*([\w-]+):\s*(.*)\s*/.exec(this);
if (match) {
if ($.trim(match[1]) === property) {
value = match[2];
return false;
}
}
});
if (pattern) {
var match = value.match(pattern);
if (match && match.length === 4) {
return match[2];
} else {
return '';
}
} else {
return value;
}
};
var get_media = function (screen_width) {
var media = false;
if (screen_width && ('min' in screen_width || 'max' in screen_width)) {
media = [];
if ('min' in screen_width) {
media.push('(min-width: ' + screen_width.min + 'px)');
}
if ('max' in screen_width) {
media.push('(max-width: ' + screen_width.max + 'px)');
}
media = media.join(' and ');
media = '@media ' + media;
}
return media;
};
var get_stored_rule = function ($element, screen_width, rule_selector, property, pattern) {
var value = '';
var $style = $element.children('style.az-css-rules');
rule_selector = $.trim(rule_selector);
if ($style.length) {
var parsed = azh.cssjs.parseCSS($style.text());
var media = get_media(screen_width);
if (media) {
$(parsed).each(function () {
if (this.type === 'media' && this.selector === media) {
$(this.subStyles).each(function () {
if (this.selector === rule_selector) {
$(this.rules).each(function () {
if (this.directive === property) {
value = this.value;
}
});
}
});
}
});
} else {
$(parsed).each(function () {
if (this.selector === rule_selector) {
$(this.rules).each(function () {
if (this.directive === property) {
value = this.value;
}
});
}
});
}
}
if (pattern) {
var match = value.match(pattern);
if (match && match.length === 4) {
return match[2];
} else {
return '';
}
} else {
return value;
}
};
var set_stored_style = function ($element, property, value, pattern, default_value) {
var pattern_value = value;
var style = get_stored_attribute($element, 'style');
var properties = [];
if (style) {
properties = style.split(';');
}
style = '';
var updated = false;
$(properties).each(function () {
var match = /\s*([\w-]+):\s*(.*)\s*/.exec(this);
if (match) {
if ($.trim(match[1]) === property) {
if (value !== '') {
if (pattern) {
var m = pattern.test(match[2]);
if (m) {
pattern_value = match[2].replace(pattern, '$1' + value + '$3');
style += match[1] + ': ' + pattern_value + '; ';
updated = true;
}
} else {
style += match[1] + ': ' + value + '; ';
updated = true;
}
}
} else {
style += match[1] + ': ' + match[2] + '; ';
}
}
});
if (!updated && value !== '') {
if (pattern && default_value) {
pattern_value = default_value.replace(pattern, '$1' + value + '$3');
style += property + ': ' + pattern_value;
} else {
style += property + ': ' + value;
}
}
set_stored_attribute($element, 'style', style);
return pattern_value;
};
var set_stored_rule = function ($element, screen_width, rule_selector, property, value, pattern, default_value) {
var pattern_value = value;
var updated = false;
var $style = $element.children('style.az-css-rules');
rule_selector = $.trim(rule_selector);
if (!$style.length) {
$style = azh.$('').appendTo($element);
store_html($style.get(0));
}
var parsed = azh.cssjs.parseCSS($style.text());
var media = get_media(screen_width);
var selector_rule = false;
if (media) {
var media_rule = false;
$(parsed).each(function () {
if (this.type === 'media' && this.selector === media) {
media_rule = this;
$(this.subStyles).each(function () {
if (this.selector === rule_selector) {
selector_rule = this;
var new_rules = [];
$(this.rules).each(function () {
if (this.directive === property) {
if (value !== '') {
if (pattern) {
var m = pattern.test(this.value);
if (m) {
pattern_value = this.value.replace(pattern, '$1' + value + '$3');
this.value = pattern_value;
updated = true;
new_rules.push(this);
}
} else {
this.value = value;
updated = true;
new_rules.push(this);
}
}
} else {
new_rules.push(this);
}
});
}
});
}
});
if (!selector_rule) {
selector_rule = {
selector: rule_selector,
rules: []
};
if (media_rule) {
media_rule.subStyles.push(selector_rule);
}
}
if (!media_rule) {
media_rule = {
selector: media,
type: 'media',
subStyles: [selector_rule]
};
parsed.push(media_rule);
}
} else {
$(parsed).each(function () {
if (this.selector === rule_selector) {
selector_rule = this;
var new_rules = [];
$(this.rules).each(function () {
if (this.directive === property) {
if (value !== '') {
if (pattern) {
var m = pattern.test(this.value);
if (m) {
pattern_value = this.value.replace(pattern, '$1' + value + '$3');
this.value = pattern_value;
updated = true;
new_rules.push(this);
}
} else {
this.value = value;
updated = true;
new_rules.push(this);
}
}
} else {
new_rules.push(this);
}
});
this.rules = new_rules;
}
});
if (!selector_rule) {
selector_rule = {
selector: rule_selector,
rules: []
};
parsed.push(selector_rule);
}
}
if (!updated && value !== '') {
if (pattern && default_value) {
pattern_value = default_value.replace(pattern, '$1' + value + '$3');
selector_rule.rules.push({
directive: property,
value: pattern_value
});
} else {
selector_rule.rules.push({
directive: property,
value: value
});
}
}
parsed.sort(function (a, b) {
if (a.type && !b.type) {
return 1;
}
if (!a.type && b.type) {
return -1;
}
return 0;
});
var style = azh.cssjs.getCSSForEditor(parsed);
$style.text(style);
return pattern_value;
};
var get_stored_attr_style = function ($element, attr, property, pattern, default_value) {
var style = get_stored_attribute($element, attr);
var properties = [];
if (style) {
properties = style.split(';');
}
style = '';
var value = '';
$(properties).each(function () {
var match = /\s*([\w-]+):\s*(.*)\s*/.exec(this);
if (match) {
if ($.trim(match[1]) == property) {
value = match[2];
return false;
}
}
});
if (pattern) {
var match = value.match(pattern);
if (match && match.length === 4) {
return match[2];
} else {
match = default_value.match(pattern);
return match[2];
}
} else {
return value;
}
};
var set_stored_attr_style = function ($element, attr, property, value, pattern, default_value) {
var old_style = get_stored_attribute($element, attr);
var properties = [];
if (old_style) {
properties = old_style.split(';');
}
var style = '';
var updated = false;
$(properties).each(function () {
var match = /\s*([\w-]+):\s*(.*)\s*/.exec(this);
if (match) {
if ($.trim(match[1]) == property) {
if (value !== '') {
if (pattern) {
var m = pattern.test(match[2]);
if (m) {
style += match[1] + ': ' + match[2].replace(pattern, '$1' + value + '$3') + '; ';
updated = true;
}
} else {
style += match[1] + ': ' + value + '; ';
updated = true;
}
}
} else {
style += match[1] + ': ' + match[2] + '; ';
}
}
});
if (!updated && value !== '') {
if (pattern && default_value) {
style += property + ': ' + default_value.replace(pattern, '$1' + value + '$3');
} else {
style += property + ': ' + value;
}
}
if (style || old_style) {
set_stored_attribute($element, attr, style);
}
};
var set_stored_responsive_style = function ($element, property, value) {
if (azh.device_prefix === 'lg') {
set_stored_style($element, property, value);
} else {
set_stored_attr_style($element, 'data-responsive-' + azh.device_prefix, property, value);
}
}
var get_stored_responsive_style = function ($element, property) {
var v = get_stored_style($element, property);
if (azh.device_prefix !== 'lg') {
var rv = get_stored_attr_style($element, 'data-responsive-' + azh.device_prefix, property);
if (rv) {
v = rv;
}
}
return v;
}
var remove_visible_controls = function ($wrapper) {
$wrapper.find('.azh-controls').addBack().filter('.azh-controls').each(function () {
azh.$(this).removeClass('azh-controls');
azh.$(this).data('azh-controls').remove();
azh.$(this).data('azh-controls', false);
});
$wrapper.find('.azh-cloneable-controls').addBack().filter('.azh-cloneable-controls').each(function () {
azh.$(this).removeClass('azh-cloneable-controls');
azh.$(this).data('azh-cloneable-controls').remove();
azh.$(this).data('azh-cloneable-controls', false);
});
$wrapper.find('.azh-item-controls').addBack().filter('.azh-item-controls').each(function () {
azh.$(this).removeClass('azh-item-controls');
azh.$(this).data('azh-item-controls').remove();
azh.$(this).data('azh-item-controls', false);
});
if ($wrapper.data('azh-height-top-resizer')) {
$wrapper.data('azh-height-top-resizer').remove();
}
if ($wrapper.data('azh-height-bottom-resizer')) {
$wrapper.data('azh-height-bottom-resizer').remove();
}
if ($wrapper.data('azh-width-right-resizer')) {
$wrapper.data('azh-width-right-resizer').remove();
}
if ($wrapper.data('azh-width-left-resizer')) {
$wrapper.data('azh-width-left-resizer').remove();
}
$wrapper.find('.azh-grid').addBack().filter('.azh-grid').each(function () {
azh.$(this).children().each(function () {
if (azh.$(this).data('azh-resizer')) {
azh.$(this).data('azh-resizer').remove();
azh.$(this).data('azh-resizer', false);
}
});
});
};
var clone_controls = function ($wrapper) {
$('.wp-picker-open').trigger('click');
//this used only for utility which will be refreshed after
$wrapper.find('.azh-linked-controls').addBack().filter('.azh-linked-controls').each(function () {
azh.$(this).data('azh-linked-controls', false);
});
$wrapper.find('.azh-controls').addBack().filter('.azh-controls').each(function () {
var $this = azh.$(this);
var $controls = $this.data('azh-controls');
var $new_controls = $controls.clone(true);
$new_controls.removeClass('azh-hidden-control');
$new_controls.insertAfter($controls);
$this.data('azh-controls', $new_controls);
$new_controls.data('azh-linked-node', $this).find('*').data('azh-linked-node', $this);
if ($this.is('[data-element]')) {
$new_controls.data('azh-linked-element', $this).find('*').data('azh-linked-element', $this);
}
$new_controls.find('.azh-utility-wrapper').remove();
});
$wrapper.find('.azh-cloneable-controls').addBack().filter('.azh-cloneable-controls').each(function () {
var $this = azh.$(this);
var $controls = $this.data('azh-cloneable-controls');
var $new_controls = $controls.clone(true);
$new_controls.removeClass('azh-hidden-control');
$new_controls.insertAfter($controls);
$this.data('azh-cloneable-controls', $new_controls);
$new_controls.data('azh-linked-node', $this).find('*').data('azh-linked-node', $this);
});
$wrapper.find('.azh-cloneable-child-controls').addBack().filter('.azh-cloneable-child-controls').each(function () {
var $this = azh.$(this);
var $controls = $this.data('azh-cloneable-child-controls');
var $new_controls = $controls.clone(true);
$new_controls.removeClass('azh-hidden-control');
$new_controls.insertAfter($controls);
$this.data('azh-cloneable-child-controls', $new_controls);
$new_controls.data('azh-linked-node', $this).find('*').data('azh-linked-node', $this);
});
$wrapper.find('.azh-item-controls').addBack().filter('.azh-item-controls').each(function () {
var $this = azh.$(this);
var $controls = $this.data('azh-item-controls');
var $new_controls = $controls.clone(true);
$new_controls.removeClass('azh-hidden-control');
$new_controls.insertAfter($controls);
$this.data('azh-item-controls', $new_controls);
$new_controls.data('azh-linked-node', $this);
$new_controls.data('azh-linked-node', $this).find('*').data('azh-linked-node', $this);
});
$wrapper.find('.azh-grid').addBack().filter('.azh-grid').each(function () {
azh.$(this).children().each(function () {
var $this = azh.$(this);
var $resizer = $this.data('azh-resizer');
if ($resizer) {
var $new_resizer = $resizer.clone(true);
$new_resizer.removeClass('azh-hidden-control');
$new_resizer.insertAfter($resizer);
$this.data('azh-resizer', $new_resizer);
$resizer.data('azh-column', $this.prev());
$resizer.data('azh-next-column', $this);
}
var $column_controls = $this.data('azh-controls');
if ($column_controls) {
//no need to clone because it already cloned
$column_controls.removeClass('azh-hidden-control');
$column_controls.children().data('azh-column', $this);
$column_controls.children().data('azh-prev-column', $this.prev());
$column_controls.children().data('azh-next-column', $this.next());
}
});
});
};
var trigger_mousemove_controls = function ($wrapper) {
azh.controls_container = false;
if ($('.azh-controls-container').length) {
azh.controls_container = $('.azh-controls-container');
azh.controls_container.detach();
} else {
azh.controls_container = $('');
}
$wrapper.find('.azh-controls').addBack().filter('.azh-controls').trigger('mousemove');
$wrapper.find('.azh-grid').addBack().filter('.azh-grid').trigger('mousemove');
azh.controls_container.appendTo($body);
};
var set_element_controls_position = function ($element, $element_controls) {
var element_rect = $element.get(0).getBoundingClientRect();
if ($element_controls.is('.az-top-right-controls')) {
$element_controls.css('left', 'auto');
$element_controls.css('right', (get_full_width() - element_rect.left - element_rect.width) + 'px');
$element_controls.css('top', (element_rect.top + azh.window.scrollTop() - $element_controls.height()) + 'px');
} else if ($element_controls.is('.az-bottom-right-controls')) {
$element_controls.css('left', 'auto');
$element_controls.css('right', (get_full_width() - element_rect.left - element_rect.width) + 'px');
$element_controls.css('top', (element_rect.top + azh.window.scrollTop() + element_rect.height) + 'px');
} else if ($element_controls.is('.az-bottom-left-controls')) {
$element_controls.css('right', 'auto');
$element_controls.css('left', element_rect.left + 'px');
$element_controls.css('top', (element_rect.top + azh.window.scrollTop() + element_rect.height) + 'px');
} else {
$element_controls.css('right', 'auto');
$element_controls.css('left', element_rect.left + 'px');
$element_controls.css('top', (element_rect.top + azh.window.scrollTop() - $element_controls.height()) + 'px');
}
};
var loading_scripts = {};
var loaded_scripts = {};
function include_required_scripts(data, callback) {
function test_callback() {
var done = true;
for (var u in loading_scripts) {
if (loading_scripts[u] !== false) {
done = false;
break;
}
}
if (done && callback) {
clearInterval(testing);
callback();
}
}
function on_load(url) {
loaded_scripts[url] = loading_scripts[url];
loading_scripts[url] = false;
test_callback();
}
if ('css' in data) {
for (var path in data.css) {
(function (path) {
if (!(data.css[path] in loaded_scripts) && !(data.css[path] in loading_scripts)) {
loading_scripts[data.css[path]] = azh.$("", {
rel: "stylesheet",
type: "text/css",
href: data.css[path]
}).appendTo("head").on('load', function () {
on_load(data.css[path]);
});
}
})(path);
}
}
if ('js' in data) {
for (var path in data.js) {
(function (path) {
if (!(data.js[path] in loaded_scripts) && !(data.js[path] in loading_scripts)) {
var script = azh.document.get(0).createElement('script');
loading_scripts[data.js[path]] = azh.$(script).appendTo("head");
script.onload = function () {
on_load(data.js[path]);
};
script.src = data.js[path];
}
})(path);
}
}
var testing = setInterval(test_callback, 100);
}
function load_required_scripts(content, path, callback) {
function ajax() {
$.post(azh.ajaxurl, {
'action': 'azh_get_scripts_urls',
'content': content
}, function (data) {
include_required_scripts(data, callback);
}, 'json');
}
if (azh.files_scripts[path]) {
include_required_scripts(azh.files_scripts[path], function () {
ajax();
});
} else {
ajax();
}
}
function open_html_editor_modal(options, value, callback) {
var $modal = $('');
$('
' + options['title'] + '
').appendTo($modal);
$('
' + options['desc'] + '
').appendTo($modal);
$('
' + options['label'] + '
').appendTo($modal);
var id = makeid();
$('').appendTo($modal);
var $actions = $('').appendTo($modal);
$('
' + azh.i18n.ok + '
').appendTo($actions).on('click', function () {
if (callback(aceeditor.getSession().getValue())) {
$.simplemodal.close();
}
return false;
});
$('
' + azh.i18n.cancel + '
').appendTo($actions).on('click', function () {
$.simplemodal.close();
return false;
});
$modal.simplemodal({
autoResize: true,
overlayClose: true,
opacity: 0,
overlayCss: {
"background-color": "black"
},
closeClass: "azh-close",
onClose: function () {
$.simplemodal.close();
}
});
var aceeditor = ace.edit(id);
aceeditor.setTheme("ace/theme/chrome");
aceeditor.getSession().setMode("ace/mode/html");
aceeditor.getSession().setValue(value);
aceeditor.resize();
}
function open_modal(options, value, callback) {
var $modal = $('');
$('
' + options['title'] + '
').appendTo($modal);
$('
' + options['desc'] + '
').appendTo($modal);
$('
' + options['label'] + '
').appendTo($modal);
if ('options' in options) {
var $select = $('').appendTo($modal).on('change', function () {
value = $(this).find('option:selected').attr('value');
});
for (var value in options['options']) {
if (value == value) {
$('').appendTo($select);
} else {
$('').appendTo($select);
}
}
} else {
$('').appendTo($modal).on('change', function () {
value = $(this).val();
});
}
var $actions = $('').appendTo($modal);
$('
' + azh.i18n.ok + '
').appendTo($actions).on('click', function () {
$.simplemodal.close();
callback(value);
return false;
});
$('
' + azh.i18n.cancel + '
').appendTo($actions).on('click', function () {
$.simplemodal.close();
return false;
});
$modal.simplemodal({
autoResize: true,
overlayClose: true,
opacity: 0,
overlayCss: {
"background-color": "black"
},
closeClass: "azh-close",
onClose: function () {
$.simplemodal.close();
}
});
}
function insert_at_cursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
var sel = document.selection.createRange();
sel.text = myValue;
}
//MOZILLA and others
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)
+ myValue
+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
}
function get_shortcode_match(code) {
var tags = Object.keys(azh.shortcodes).join('|');
var reg = wp_shortcode.regexp(tags);
var matches = code.match(reg);
return reg.exec(code);//str, open, name, args, self_closing, content, closing, close, offset, s
}
function make_shortcode(settings, shortcode, attrs) {
var shortcode = '[' + settings['base'];
var content = false;
// if ('content_element' in settings && settings['content_element']) {
// content = ' ';
// }
if ('content' in attrs) {
content = attrs['content'];
}
shortcode += Object.keys(attrs).map(function (item) {
if (item == 'content') {
return '';
} else {
return ' ' + item + '="' + attrs[item] + '"';
}
}).join('');
shortcode += ']';
if (content) {
shortcode += content + '[/' + settings['base'] + ']';
}
return shortcode;
}
function getCaretPosition(editableDiv) {
var caretPos = 0, sel, range;
if (azh.window.get(0).getSelection) {
sel = azh.window.get(0).getSelection();
if (sel.rangeCount) {
range = sel.getRangeAt(0);
if (range.commonAncestorContainer.parentNode == editableDiv) {
caretPos = range.endOffset;
}
}
} else if (azh.document.get(0).selection && azh.document.get(0).selection.createRange) {
range = azh.document.get(0).selection.createRange();
if (range.parentElement() == editableDiv) {
var tempEl = azh.document.get(0).createElement("span");
editableDiv.insertBefore(tempEl, editableDiv.firstChild);
var tempRange = range.duplicate();
tempRange.moveToElementText(tempEl);
tempRange.setEndPoint("EndToEnd", range);
caretPos = tempRange.text.length;
}
}
return caretPos;
}
function setCaretPosition(el, pos) {
var range = azh.document.get(0).createRange();
var sel = azh.window.get(0).getSelection();
range.setStart(el.childNodes[0], pos);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
function wrapper_detach($wrapper) {
$wrapper.data('azh-detach-parent', $wrapper.parent());
$wrapper.data('azh-detach-index', $wrapper.parent().children().index($wrapper));
$wrapper.data('azh-focus', false);
if ($wrapper.find('[contenteditable="true"]:focus').length) {
$wrapper.data('azh-focus', $wrapper.find('[contenteditable="true"]:focus'));
$wrapper.data('azh-caret-start', getCaretPosition($wrapper.find('[contenteditable="true"]:focus').get(0)));
}
$wrapper.detach();
}
function wrapper_restore($wrapper) {
if ($wrapper.data('azh-detach-index') === 0) {
$wrapper.prependTo($wrapper.data('azh-detach-parent'));
} else {
$wrapper.insertAfter($wrapper.data('azh-detach-parent').children().eq($wrapper.data('azh-detach-index') - 1));
}
if ($wrapper.data('azh-focus')) {
setCaretPosition($wrapper.data('azh-focus').get(0), $wrapper.data('azh-caret-start'));
$wrapper.data('azh-focus').trigger('focus');
}
}
function closest_descendents($element, selector) {
var $found = azh.$(), $currentSet = $element;
while ($currentSet.length) {
$found = $found.add($currentSet.children().filter(selector));
$currentSet = $currentSet.children().not(selector);
}
return $found;
}
var customization_init = function ($wrapper) {
function open_attribute_modal(options, $element, callback) {
var $modal = $('');
$('
' + options['title'] + '
').appendTo($modal);
if (options['desc']) {
$('
' + options['desc'] + '
').appendTo($modal);
}
if ('attribute' in options) {
if (options['label']) {
$('
' + options['label'] + '
').appendTo($modal);
}
if ('options' in options) {
var $select = $('').appendTo($modal);
for (var value in options['options']) {
if (value === $element.attr(options['attribute'])) {
$('').appendTo($select);
} else {
$('').appendTo($select);
}
}
} else {
if (options['type'] && options['type'] === 'textarea') {
$('').appendTo($modal);
} else {
$('').appendTo($modal);
}
}
}
if ('attributes' in options) {
for (var name in options['attributes']) {
var attribute_options = options['attributes'][name];
if (attribute_options['label']) {
$('
' + attribute_options['label'] + '
').appendTo($modal);
}
if ('options' in attribute_options) {
var $select = $('').appendTo($modal);
for (var value in attribute_options['options']) {
if (value === $element.attr(name)) {
$('').appendTo($select);
} else {
$('').appendTo($select);
}
}
} else {
if (attribute_options['type'] && attribute_options['type'] === 'textarea') {
$('').appendTo($modal);
} else {
$('').appendTo($modal);
}
}
}
}
if (options['callback']) {
options['callback']($element, $modal);
}
var $actions = $('').appendTo($modal);
$('
' + azh.i18n.ok + '
').appendTo($actions).on('click', function () {
if ('attribute' in options) {
$element.attr(options['attribute'], $modal.find('[name="' + options['attribute'] + '"]').val());
set_stored_attribute($element, options['attribute'], $modal.find('[name="' + options['attribute'] + '"]').val());
}
if ('attributes' in options) {
for (var name in options['attributes']) {
$element.attr(name, $modal.find('[name="' + name + '"]').val());
set_stored_attribute($element, name, $modal.find('[name="' + name + '"]').val());
}
}
$.simplemodal.close();
callback();
return false;
});
$('
' + azh.i18n.cancel + '
').appendTo($actions).on('click', function () {
$.simplemodal.close();
return false;
});
$modal.simplemodal({
autoResize: true,
overlayClose: true,
opacity: 0,
overlayCss: {
"background-color": "black"
},
closeClass: "azh-close",
onClose: function () {
$.simplemodal.close();
}
});
}
function open_select_modal($element) {
var $modal = $('');
$('
' + azh.i18n.select_options_edit + '
').appendTo($modal);
$('
' + azh.i18n.options + '
').appendTo($modal);
var $table = $('').appendTo($modal);
var $row = $('
' + azh.i18n.label + '
' + azh.i18n.value + '
' + azh.i18n.clone + '
' + azh.i18n.remove + '
').appendTo($table);
$element.children().each(function () {
var $option = azh.$(this);
var $row = $('').appendTo($table);
$row.data('option', $option);
$('
' + $option.text() + '
').appendTo($row).on('blur keyup paste input', function () {
var $option = $(this).closest('.azh-row').data('option');
$option.text($(this).text());
});
$('
' + $option.val() + '
').appendTo($row).on('blur keyup paste input', function () {
var $option = $(this).closest('.azh-row').data('option');
$option.val($(this).text());
set_stored_attribute($option, 'value', $option.val());
});
$('').appendTo($row).on('click', function () {
var $row = $(this).closest('.azh-row');
var $new_row = $row.clone(true);
$new_row.insertAfter($row);
var $new_option = $row.data('option').clone(true);
$new_option.insertAfter($row.data('option'));
$new_row.data('option', $new_option);
});
$('').appendTo($row).on('click', function () {
var $row = $(this).closest('.azh-row');
$row.data('option').remove();
$row.remove();
});
});
var $actions = $('').appendTo($modal);
$('
' + azh.i18n.ok + '
').appendTo($actions).on('click', function () {
$.simplemodal.close();
return false;
});
$modal.simplemodal({
autoResize: true,
overlayClose: true,
opacity: 0,
overlayCss: {
"background-color": "black"
},
closeClass: "azh-close",
onClose: function () {
$.simplemodal.close();
}
});
}
function grid_editor($grid) {
function get_column_width($column, pattern) {
var column_width = false;
var match = null;
pattern.lastIndex = 0;
if ((match = pattern.exec($column.attr('class'))) != null && match.length == 2 && $.isNumeric(match[1])) {
return column_width = parseInt(match[1], 10);
}
return false;
}
function set_column_width($column, pattern, width) {
var match = null;
pattern.lastIndex = 0;
if ((match = pattern.exec($column.attr('class'))) != null && match.length == 2 && $.isNumeric(match[1])) {
var column_width = match[1];
var c = match[0].replace(column_width, width);
var classes = $column.attr('class').replace(match[0], c);
$column.attr('class', classes);
classes = get_stored_attribute($column, 'class').replace(match[0], c);
set_stored_attribute($column, 'class', classes);
}
}
function get_current_column_width($column) {
if (azh.device_prefix) {
var column_pattern = azh.column_width_patterns[azh.device_prefix];
column_pattern.lastIndex = 0;
var match = null;
while ((match = column_pattern.exec($column.attr('class'))) != null && match.length == 2 && $.isNumeric(match[1])) {
return match;
}
} else {
for (var prefix in azh.column_width_patterns) {
var column_pattern = azh.column_width_patterns[prefix];
column_pattern.lastIndex = 0;
var match = null;
while ((match = column_pattern.exec($column.attr('class'))) != null && match.length == 2 && $.isNumeric(match[1])) {
return match;
}
}
}
return false;
}
var $prev_column = false;
$grid.children().each(function () {
var $column = azh.$(this);
var $column_controls = $column.data('azh-controls');
if (!$column_controls && $column.is('[data-cloneable]')) {
$column_controls = $('').appendTo(azh.controls_container).hide().on('mouseenter', function (event) {
$(this).data('azh-linked-node').addClass('azh-over');
}).on('mouseleave', function (event) {
$(this).data('azh-linked-node').removeClass('azh-over');
});
$('').appendTo($column_controls).on('click', function () {
var $column = $(this).data('azh-column');
var $modal = $('');
$('
' + azh.i18n.column_responsive + '
').appendTo($modal);
var $table = $('
' + azh.i18n.device + '
' + azh.i18n.column_width + '
' + azh.i18n.column_offset + '
').appendTo($modal);
for (var prefix in azh.device_prefixes) {
(function (prefix) {
var $row = $('
').appendTo($table.find('tbody'));
$('
' + azh.device_prefixes[prefix].label + '
').appendTo($row);
var $col = $('
').appendTo($row);
$('').appendTo($col).on('change', function () {
set_column_width($column, azh.column_width_patterns[prefix], $(this).val());
});
var $col = $('
').appendTo($row);
$('').appendTo($col).on('change', function () {
set_column_width($column, azh.column_offset_patterns[prefix], $(this).val());
});
})(prefix);
}
var $actions = $('').appendTo($modal);
$('
' + azh.i18n.ok + '
').appendTo($actions).on('click', function () {
$.simplemodal.close();
return false;
});
$modal.simplemodal({
autoResize: true,
overlayClose: true,
opacity: 0,
overlayCss: {
"background-color": "black"
},
closeClass: "azh-close",
onClose: function () {
$.simplemodal.close();
}
});
return false;
}).data('azh-column', $column);
$('').appendTo($column_controls).on('click', function () {
var $column = $(this).data('azh-column');
var $new_column = $column.clone(true);
var new_column = true;
var controlled_device_prefixes = azh.section_controlled_device_prefixes;
if ($column.closest('[data-element]').length) {
controlled_device_prefixes = azh.element_controlled_device_prefixes;
}
$(controlled_device_prefixes).each(function () {
var prefix = this;
var width = get_column_width($column, azh.column_width_patterns[prefix]);
var width1 = Math.round(width / 2);
var width2 = width - width1;
if (width1 && width2) {
set_column_width($column, azh.column_width_patterns[prefix], width1);
set_column_width($new_column, azh.column_width_patterns[prefix], width2);
} else {
new_column = false;
}
});
if (new_column) {
$new_column.find('> [data-element]').each(function () {
remove_visible_controls(azh.$(this));
make_element_empty(azh.$(this));
});
$new_column.find('> [data-element]:not(:first-child)').remove();
$new_column.insertAfter($column);
section_refresh($new_column.closest('[data-section]'));
}
return false;
}).data('azh-column', $column);
$('').appendTo($column_controls).on('click', function () {
var $this = $(this);
var $column = $this.data('azh-column');
var $new_column = $this.data('azh-prev-column');
if (!$new_column) {
$new_column = $this.data('azh-next-column');
}
if ($new_column) {
var controlled_device_prefixes = azh.section_controlled_device_prefixes;
if ($column.closest('[data-element]').length) {
controlled_device_prefixes = azh.element_controlled_device_prefixes;
}
$(controlled_device_prefixes).each(function () {
var prefix = this;
var width_new = get_column_width($new_column, azh.column_width_patterns[prefix]);
var width_deleted = get_column_width($column, azh.column_width_patterns[prefix]);
set_column_width($new_column, azh.column_width_patterns[prefix], width_new + width_deleted);
});
if ($this.data('azh-prev-column')) {
$column.data('azh-resizer').remove();
if ($this.data('azh-next-column')) {
$this.data('azh-next-column').data('azh-resizer').data('azh-column', $this.data('azh-prev-column'));
$this.data('azh-next-column').data('azh-controls').find('.azh-remove-column').data('azh-prev-column', $this.data('azh-prev-column'));
$this.data('azh-prev-column').data('azh-controls').find('.azh-remove-column').data('azh-next-column', $this.data('azh-next-column'));
} else {
$this.data('azh-prev-column').data('azh-controls').find('.azh-remove-column').data('azh-next-column', false);
}
} else {
if ($this.data('azh-next-column')) {
if ($this.data('azh-next-column').data('azh-resizer')) {
$this.data('azh-next-column').data('azh-resizer').remove();
$this.data('azh-next-column').data('azh-resizer', false);
}
$this.data('azh-next-column').data('azh-controls').find('.azh-remove-column').data('azh-prev-column', false);
}
}
$column.data('azh-controls').remove();
remove_visible_controls($column);
$column.remove();
_.defer(refresh_elements_hierarchy_partial, $new_column);
} else {
alert(azh.i18n.last_column_can_not_be_deleted);
}
return false;
}).data('azh-column', $column).data('azh-prev-column', $prev_column);
$column.data('azh-controls', $column_controls);
$column_controls.data('azh-linked-node', $column);
$column.addClass('azh-controls');
$column.on('mouseenter', function (event) {
var $this = azh.$(this);
var $column_controls = $this.data('azh-controls');
var column_rect = $this.get(0).getBoundingClientRect();
setTimeout(function () {
//azh.device_prefix
$column_controls.css('left', (column_rect.left + column_rect.width / 2 - $column_controls.outerWidth() / 2) + 'px');
$column_controls.css('top', (column_rect.top + azh.window.scrollTop() - 10) + 'px');
$column_controls.css('display', '');
});
}).on('mousemove', function () {
var $this = azh.$(this);
var $column_controls = $this.data('azh-controls');
var column_rect = $this.get(0).getBoundingClientRect();
$column_controls.css('left', (column_rect.left + column_rect.width / 2 - $column_controls.outerWidth() / 2) + 'px');
$column_controls.css('top', (column_rect.top + azh.window.scrollTop() - 10) + 'px');
}).on('mouseleave', function () {
var $column_controls = azh.$(this).data('azh-controls');
setTimeout(function () {
if (!$column_controls.is(':hover')) {
$column_controls.hide();
} else {
$column_controls.one('mouseleave', function () {
$column_controls.hide();
});
}
});
});
}
if ($prev_column) {
if ($prev_column.data('azh-controls')) {
$prev_column.data('azh-controls').find('.azh-remove-column').data('azh-next-column', $column);
}
var $resizer = $('').appendTo(azh.controls_container).hide();
$column.data('azh-resizer', $resizer);
$resizer.data('azh-column', $prev_column);
$resizer.data('azh-next-column', $column);
$resizer.css('left', ($column.offset().left - 20) + 'px');
$resizer.css('top', $column.offset().top + 'px');
$resizer.css('height', $column.height() + 'px');
$resizer.on('mousedown', function (e) {
var $resizer = $(this);
$resizer.addClass('azh-drag');
azh.controls_container.addClass('azh-drag');
$resizer.data('pageX', e.pageX);
$resizer.data('azh-column').closest('.azh-grid').addClass('azh-drag');
e.preventDefault();
e.stopPropagation();
return false;
}).on('mouseleave', function () {
var $resizer = $(this);
$resizer.hide();
});
}
$prev_column = $column;
});
$grid.on('mouseenter', function () {
$grid.children().each(function () {
var $column = azh.$(this);
var $resizer = $column.data('azh-resizer');
if ($resizer) {
var current_width = get_current_column_width($column);
if (current_width && parseInt(current_width[1], 10) < 12) {
$resizer.show();
}
}
});
}).on('mousemove', function (e) {
$grid.children().each(function () {
var $column = azh.$(this);
var $resizer = $column.data('azh-resizer');
if ($resizer) {
$resizer.css('left', ($column.offset().left - 20) + 'px');
$resizer.css('top', $column.offset().top + 'px');
$resizer.css('height', $column.height() + 'px');
}
});
}).on('mouseleave', function () {
$grid.children().each(function () {
var $column = azh.$(this);
var $resizer = $column.data('azh-resizer');
if ($resizer && !$resizer.is(':hover')) {
$resizer.hide();
}
});
});
$grid.on('click', function (e) {
if (azh.$(this).is('.azh-drag')) {
e.preventDefault();
e.stopPropagation();
return false;
}
});
azh.body_off('mouseup.grid').body_on('mouseup.grid', function (e) {
azh.$('.azh-grid.azh-drag').removeClass('azh-drag');
azh.controls_container.find('.azh-width-resizer.azh-drag').removeClass('azh-drag');
azh.controls_container.removeClass('azh-drag');
});
azh.body_off('mousemove.grid').body_on('mousemove.grid', function (e) {
if (e.buttons === 0) {
azh.$('.azh-grid.azh-drag').removeClass('azh-drag');
}
var $resizer = $body.find('.azh-width-resizer.azh-drag');
if (azh.$('.azh-grid.azh-drag').length && $resizer.length) {
var $column = $resizer.data('azh-column');
var current_width = get_current_column_width($column);
var $next_column = $resizer.data('azh-next-column');
var next_current_width = get_current_column_width($next_column);
if ((e.pageX + (azh.device_left ? azh.device_left : 0)) < $resizer.offset().left && e.pageX < $resizer.data('pageX')) {
if (current_width[1] > 1) {
$column.removeClass(current_width[0]);
remove_from_stored_classes($column, current_width[0]);
var less = current_width[0].replace(parseInt(current_width[0].replace(/[^\d]/g, ''), 10), parseInt(current_width[0].replace(/[^\d]/g, ''), 10) - 1);
$column.addClass(less);
add_to_stored_classes($column, less);
$next_column.removeClass(next_current_width[0]);
remove_from_stored_classes($next_column, next_current_width[0]);
var greather = next_current_width[0].replace(parseInt(next_current_width[0].replace(/[^\d]/g, ''), 10), parseInt(next_current_width[0].replace(/[^\d]/g, ''), 10) + 1);
$next_column.addClass(greather);
add_to_stored_classes($next_column, greather);
$resizer.css('left', ($next_column.offset().left - parseInt($next_column.css('padding-left'), 10) - 5) + 'px');
}
} else {
if ((e.pageX + (azh.device_left ? azh.device_left : 0)) > ($resizer.offset().left + $resizer.width()) && e.pageX > $resizer.data('pageX')) {
if (next_current_width[1] > 1) {
$column.removeClass(current_width[0]);
remove_from_stored_classes($column, current_width[0]);
var greather = current_width[0].replace(parseInt(current_width[0].replace(/[^\d]/g, ''), 10), parseInt(current_width[0].replace(/[^\d]/g, ''), 10) + 1);
$column.addClass(greather);
add_to_stored_classes($column, greather);
$next_column.removeClass(next_current_width[0]);
remove_from_stored_classes($next_column, next_current_width[0]);
var less = next_current_width[0].replace(parseInt(next_current_width[0].replace(/[^\d]/g, ''), 10), parseInt(next_current_width[0].replace(/[^\d]/g, ''), 10) - 1);
$next_column.addClass(less);
add_to_stored_classes($next_column, less);
$resizer.css('left', ($next_column.offset().left - parseInt($next_column.css('padding-left'), 10) - 5) + 'px');
}
}
}
}
$resizer.data('pageX', e.pageX);
});
}
function enable_contenteditable($element) {
if ($element.attr('contenteditable') !== 'true' &&
$element.find('[contenteditable="true"]').length === 0 &&
$element.parents('[contenteditable="true"]').length === 0 &&
!$element.is('.az-readonly') &&
$element.find('.az-readonly').length === 0 &&
$element.parents('.az-readonly').length === 0 &&
$element.find('[draggable="true"]').length === 0 &&
$element.closest(azh.dynamic_content).length === 0
) {
if ($element.contents().length === 1) {
$element.contents().each(function () {
if (this.nodeType === 3) {
this.textContent = $.trim(this.textContent.replace(/ /g, ' '));
}
});
}
$element.attr('contenteditable', 'true');
add_to_stored_classes($element, 'az-contenteditable');
$element.on('focus click', function (event) {
function blur($element) {
$element.children().each(function () {
store_html(this);
});
$element.find('.azh-context').each(function () {
remove_from_stored_classes(azh.$(this), 'azh-context');
});
$toolbar.hide();
azh.change();
azh.controls_container.removeClass('azh-edit');
}
var $element = azh.$(this);
var $toolbar = $('.azh-editor-toolbar');
if (azh.editor_toolbar.length) {
if (['block', 'table-cell'].indexOf($element.css('display')) >= 0 || $element.is('[contenteditable="true"]')) {
$toolbar.show();
if (($element.offset().left + $toolbar.outerWidth()) > $toolbar.parent().outerWidth()) {
$toolbar.css({
left: $element.offset().left + $element.outerWidth() - $toolbar.outerWidth(),
top: $element.offset().top - $toolbar.outerHeight()
});
} else {
$toolbar.css({
left: $element.offset().left,
top: $element.offset().top - $toolbar.outerHeight()
});
}
$toolbar.data('azh-linked-node', $element);
$element.off('input').on('input', function (event) {
azh.controls_container.addClass('azh-edit');
});
$element.off('blur').one('blur', function (event) {
blur(azh.$(this));
});
$toolbar.find('input, select').off('mousedown').on('mousedown', function () {
var $this = $(this);
var $element = $toolbar.data('azh-linked-node');
$element.off('blur');
$this.off('change').on('change', function () {
$element.off('blur').one('blur', function (event) {
blur($element);
});
$element.trigger('focus');
});
});
$toolbar.on('mouseenter', function (e) {
var $toolbar = $(this);
var $element = $toolbar.data('azh-linked-node').closest('[data-element]');
if ($element.length) {
$element.data('azh-controls').removeClass('azh-active');
}
}).on('mouseleave', function (e) {
var $toolbar = $(this);
var $element = $toolbar.data('azh-linked-node').closest('[data-element]');
if ($element.length) {
$element.data('azh-controls').addClass('azh-active');
}
});
}
}
}).on('click dragstart dragenter dragover dragleave drop dragend', function (event) {
if (azh.$(this).closest('.azh-drag').length === 0) {
event.preventDefault();
}
}).on('paste', function (event) {
event.preventDefault();
var text = event.originalEvent.clipboardData.getData("text/plain");
azh.document.get(0).execCommand("insertHTML", false, text);
});
}
}
function create_title($section_or_element) {
var title = '';
var $element_wrapper = $section_or_element.closest('[data-element]');
if ($element_wrapper.length) {
title = azh.i18n.element + ': ' + $element_wrapper.attr('data-element');
} else {
var $section_wrapper = $section_or_element.closest('[data-section]');
if ($section_wrapper.length) {
title = azh.i18n.section + ': ' + $section_wrapper.data('section');
}
}
return title;
}
function get_utility($section_or_element) {
if (!$section_or_element.length || $section_or_element.parents('[data-element*="shortcode"]').length) {
return false;
}
var $utility = $section_or_element.data('azh-controls').find('.azh-utility');
if ($utility.length === 0) {
var $utility_wrapper = $('').appendTo($section_or_element.data('azh-controls')).on('click', function (event) {
var $utility_wrapper = $(this);
var $controls = $utility_wrapper.closest('.azh-section-controls, .azh-column-controls, .azh-element-controls');
var $utility = $controls.find('.azh-utility');
if (!$utility.data('azh-filled')) {
refresh_utility($utility);
}
if ($utility.children().not('.azh-utility-title').length) {
if (azh.utility_top !== undefined && azh.utility_left !== undefined) {
azh.controls_container.find('.azh-element-controls.azh-active, .azh-column-controls.azh-active, .azh-section-controls.azh-active').removeClass('azh-active');
$controls.addClass('azh-active');
$utility.css({
'top': azh.utility_top,
'left': azh.utility_left
});
} else {
if (event.which) {
$controls.addClass('azh-active');
if ($controls.is('.azh-section-controls') || $controls.is('.az-top-right-controls') || $controls.is('.az-bottom-right-controls')) {
$utility.css({
'top': $utility_wrapper.offset().top - $window.scrollTop() + $utility_wrapper.outerHeight(),
'left': $utility_wrapper.offset().left - $utility.outerWidth() + $utility_wrapper.outerWidth()
});
} else {
$utility.css({
'top': $utility_wrapper.offset().top - $window.scrollTop() + $utility_wrapper.outerHeight(),
'left': $utility_wrapper.offset().left
});
}
}
}
}
$controls.find('.azh-responsive [data-prefix="' + azh.device_prefix + '"]').trigger('click');
$utility.trigger('azh-show-utility', event);
$utility_wrapper.data('azh-linked-element').trigger('azh-show-utility', event);
if (event.which) {
$utility_wrapper.data('azh-linked-element').trigger('click');
}
return false;
}).data('azh-linked-element', $section_or_element);
var $utility = $('').appendTo($utility_wrapper).on('click', function (event) {
event.stopPropagation();
}).on('mouseenter', function () {
var $controls = $(this).closest('.azh-section-controls, .azh-column-controls, .azh-element-controls');
$controls.addClass('azh-utility-hover');
}).on('mouseleave', function () {
var $controls = $(this).closest('.azh-section-controls, .azh-column-controls, .azh-element-controls');
$controls.removeClass('azh-utility-hover');
}).data('azh-linked-element', $section_or_element);
$('
' + create_title($section_or_element) + '
').prependTo($utility);
$('').appendTo($utility);
// $utility.draggable({
// handle: ".azh-utility-title",
// drag: function (event, ui) {
// azh.utility_top = ui.position.top;
// azh.utility_left = ui.position.left;
// }
// });
}
return $utility;
}
function control_create($element, options, $target_utility) {
function add_units() {
if (options['units']) {
var $units = $('').appendTo($control);
$control.addClass('azh-with-units');
if (typeof options['units'] === 'string') {
$control.data('azh-units', options['units']);
$('' + options['units'] + '').appendTo($units);
} else {
$control.data('azh-units', Object.keys(options['units'])[0]);
for (var units in options['units']) {
$('' + units + '').appendTo($units).on('click', function () {
var $control = $(this).closest('.azh-control');
var options = $control.data('azh-options');
var units = $(this).data('units');
$control.data('azh-units', units);
$control.find('[data-units]').removeClass('azh-active');
$control.find('[data-units="' + units + '"]').addClass('azh-active');
if ('min' in options['units'][units]) {
if ($control.find('.azh-slider').length && $control.find('.azh-slider').children().length) {
$control.find('.azh-slider').slider("option", "min", parseFloat(options['units'][units]['min']));
}
$control.find('input').attr('min', options['units'][units]['min']);
}
if ('max' in options['units'][units]) {
if ($control.find('.azh-slider').length && $control.find('.azh-slider').children().length) {
$control.find('.azh-slider').slider("option", "max", parseFloat(options['units'][units]['max']));
}
$control.find('input').attr('max', options['units'][units]['max']);
}
if ('step' in options['units'][units]) {
if ($control.find('.azh-slider').length && $control.find('.azh-slider').children().length) {
$control.find('.azh-slider').slider("option", "step", parseFloat(options['units'][units]['step']));
}
$control.find('input').attr('step', options['units'][units]['step']);
}
});
}
$control.find('[data-units="' + Object.keys(options['units'])[0] + '"]').addClass('azh-active');
$control.data('azh-units', Object.keys(options['units'])[0]);
}
}
}
function add_responsive(change) {
function control_change() {
var $element = $(this).data('azh-linked-node');
azh.window.get(0).azh.refresh_responsive_css_rules($element);
}
if (azh.responsive && options['responsive']) {
var $responsive = $('').appendTo($control);
for (var prefix in azh.device_prefixes) {
$('').appendTo($responsive).on('click', function () {
var $control = $(this).closest('.azh-control');
var prefix = $(this).data('prefix');
var attr = 'data-responsive-' + prefix;
if (options['attribute']) {
var attr = options['attribute'] + '-' + prefix;
}
if ($control.data('azh-responsive') !== attr) {
$control.find('[data-prefix]').removeClass('azh-active');
$control.find('[data-prefix="' + prefix + '"]').addClass('azh-active');
if (prefix === 'lg') {
$control.data('azh-responsive', false);
} else {
$control.data('azh-responsive', attr);
}
$control.trigger('azh-init');
$control.off('azh-change.responsive', control_change);
change($control);
$control.on('azh-change.responsive', control_change);
}
});
}
$responsive.find('[data-prefix="' + azh.device_prefix + '"]').addClass('azh-active');
$control.on('azh-change.responsive', control_change);
}
}
function get_style_property($control, options, $element, property) {
if ('rule_selector' in options && options['rule_selector']) {
var value = '';
var rule_selector = options['rule_selector'];
var id = $element.attr('id');
if (id) {
rule_selector = options['rule_selector'].split(',').map(function (sel) {
return '#' + id + sel;
}).join(',');
}
if ($control.data('azh-responsive')) {
var prefix = $control.data('azh-responsive').replace('data-responsive-', '');
value = get_stored_rule($element, azh.device_prefixes[prefix], rule_selector, property);
} else {
value = get_stored_rule($element, false, rule_selector, property);
}
} else {
var $multiplying_element = $element;
if (options['multiplying_selector']) {
$multiplying_element = $multiplying_element.find(options['multiplying_selector']).first();
}
var value = get_stored_style($multiplying_element, property);
if ($control.data('azh-responsive')) {
value = get_stored_attr_style($multiplying_element, $control.data('azh-responsive'), property);
} else {
if (options['attribute']) {
value = get_stored_attr_style($multiplying_element, options['attribute'], property);
}
}
}
return value;
}
function set_style_property($control, options, $element, property, value) {
function set_style_property_prefixes_multiplying($control, options, $element, property, value) {
if (options['prefixes'] && options['prefixes'].length) {
for (var p = 0; p < options['prefixes'].length; p++) {
var prefixed_property = options['prefixes'][p] + property;
set_style_property_multiplying($control, options, $element, prefixed_property, value);
}
}
}
function set_style_property_multiplying($control, options, $element, property, value) {
if ('rule_selector' in options && options['rule_selector']) {
var id = $element.attr('id');
if (!id) {
id = makeid();
$element.attr('id', id);
set_stored_attribute($element, 'id', id);
}
var rule_selector = options['rule_selector'].split(',').map(function (sel) {
return '#' + id + sel;
}).join(',');
if ($control.data('azh-responsive')) {
var prefix = $control.data('azh-responsive').replace('data-responsive-', '');
set_stored_rule($element, azh.device_prefixes[prefix], rule_selector, property, value, options['pattern'], options['default']);
} else {
set_stored_rule($element, false, rule_selector, property, value, options['pattern'], options['default']);
}
} else {
if ($control.data('azh-responsive')) {
set_stored_attr_style($element, $control.data('azh-responsive'), property, value, options['pattern'], options['default']);
var style = get_stored_attribute($element, $control.data('azh-responsive'));
if (style !== false) {
$element.attr($control.data('azh-responsive'), style);
}
} else {
if (options['attribute']) {
set_stored_attr_style($element, options['attribute'], property, value, options['pattern'], options['default']);
$element.attr(options['attribute'], get_stored_attribute($element, options['attribute']));
} else {
var pattern_value = set_stored_style($element, property, value, options['pattern'], options['default']);
$element.css(property, pattern_value);
}
}
}
}
if (options['multiplying_selector']) {
$element.find(options['multiplying_selector']).each(function () {
set_style_property_multiplying($control, options, azh.$(this), property, value);
set_style_property_prefixes_multiplying($control, options, azh.$(this), property, value);
});
} else {
set_style_property_multiplying($control, options, $element, property, value);
set_style_property_prefixes_multiplying($control, options, $element, property, value);
}
}
function get_attribute_value($control, options, $element, attribute) {
var value = '';
var $multiplying_element = $element;
if (options['multiplying_selector']) {
$multiplying_element = $element.find(options['multiplying_selector']).first();
}
if ($control.data('azh-responsive')) {
value = get_stored_attribute($multiplying_element, $control.data('azh-responsive'));
} else {
value = get_stored_attribute($multiplying_element, attribute);
}
value = get_value_by_pattern(options, value);
$control.attr('data-value', value);
return value;
}
function set_attribute_value($control, options, $element, attribute, value) {
var pattern_value = '';
if (options['multiplying_selector']) {
if ($control.data('azh-responsive')) {
$element.find(options['multiplying_selector']).each(function () {
pattern_value = set_stored_attribute(azh.$(this), $control.data('azh-responsive'), value, options['pattern'], options['default']);
azh.$(this).attr($control.data('azh-responsive'), pattern_value);
});
} else {
$element.find(options['multiplying_selector']).each(function () {
pattern_value = set_stored_attribute(azh.$(this), attribute, value, options['pattern'], options['default']);
azh.$(this).attr(attribute, pattern_value);
});
}
} else {
if ($control.data('azh-responsive')) {
pattern_value = set_stored_attribute($element, $control.data('azh-responsive'), value, options['pattern'], options['default']);
$element.attr($control.data('azh-responsive'), pattern_value);
} else {
pattern_value = set_stored_attribute($element, attribute, value, options['pattern'], options['default']);
$element.attr(attribute, pattern_value);
}
}
$control.attr('data-value', pattern_value);
}
function get_value_by_pattern(options, value) {
if (options['pattern']) {
var match = false;
if (value && (match = value.match(options['pattern']))) {
if (match.length === 4) {
value = match[2];
}
} else {
value = options['default'];
var match = value.match(options['pattern']);
value = match[2];
}
}
return value;
}
function integer_mouse_drag($input) {
$input.on('mousedown', function (event) {
var $input = $(this);
$input.data('azh-drag', true);
$input.data('azh-drag-y', event.pageY);
$input.data('azh-start-drag-y', event.pageY);
azh.document_off('mouseup.mouse-drag').document_on('mouseup.mouse-drag', function (e) {
if ($input.data('azh-start-drag-y') === e.pageY) {
$input.trigger('change');
}
$input.data('azh-drag', false);
if (!$input.is(':hover')) {
azh.document_off('mouseup.mouse-drag');
}
});
azh.document_off('mousemove.mouse-drag').document_on('mousemove.mouse-drag', function (e) {
if ($input.data('azh-drag')) {
var d = $input.data('azh-drag-y') - e.pageY;
if ($input.attr('step')) {
d = d * $input.attr('step');
}
var v = parseFloat($input.val()) + d;
v = isNaN(v) ? 0 : v;
$input.val(v).trigger('change');
$input.data('azh-drag-y', e.pageY);
}
});
}).on('mouseleave', function (event) {
var $input = $(this);
if (!$input.data('azh-drag') && !$input.is(':hover')) {
}
}).on('contextmenu', function (event) {
var $input = $(this);
$input.val('').trigger('change');
return false;
}).addClass('azh-integer-mouse-drag');
}
var $section_or_element = $element.closest('[data-section].azh-controls, [class*="azh-col-"].azh-controls, [data-element].azh-controls');
if (options['target_utility']) {
$section_or_element = $element.closest(options['target_utility']).closest('[data-section].azh-controls, [class*="azh-col-"].azh-controls, [data-element].azh-controls');
}
var $utility = get_utility($section_or_element);
if ($target_utility) {
if ('menu' in options && options['menu'] == 'utility') {
if (!$target_utility.is($utility)) {
return;
}
} else {
return;
}
}
if ($element.parents('[data-element*="shortcode"]').length) {
return;
}
if (options['not_selector']) {
if ($element.is(options['not_selector'])) {
return;
}
}
var controls = $element.data('azh-linked-controls');
if (!controls) {
controls = [];
}
var exists = false;
$(controls).each(function () {
var existing_options = this.data('azh-options');
if (existing_options) {
if (!('selector' in options)) {
if ('attribute' in options && 'attribute' in existing_options && options['attribute'] === existing_options['attribute']
&& !('property' in options)
&& !('properties' in options)
&& (options['type'] != 'background-image')
&& (options['type'] != 'font-family')
&& 'menu' in options && 'menu' in existing_options && options['menu'] === existing_options['menu']) {
exists = true;
return false;
}
if ('property' in options && 'property' in existing_options && options['property'] === existing_options['property']
&& 'control_type' in options && 'control_type' in existing_options && options['control_type'] === existing_options['control_type']
&& 'menu' in options && 'menu' in existing_options && options['menu'] === existing_options['menu']
&& options['attribute'] === existing_options['attribute']
) {
exists = true;
return false;
}
} else {
var o = $.extend({}, options);
var eo = $.extend({}, existing_options);
delete o['order'];
delete eo['order'];
delete o['selector'];
delete eo['selector'];
delete o['pattern'];
delete eo['pattern'];
delete o['default'];
delete eo['default'];
delete o['group'];
delete eo['group'];
if (JSON.stringify(o) === JSON.stringify(eo)) {
exists = true;
return false;
}
}
if (JSON.stringify(options) === JSON.stringify(existing_options)) {
exists = true;
return false;
}
}
});
if (!exists) {
var $control = $('').on('click', function (event) {
event.stopPropagation();
}).on('mouseenter', function () {
var $this = $(this);
if ($this.data('azh-linked-node')) {
$this.data('azh-linked-node').addClass('azh-over');
}
}).on('mouseleave', function () {
var $this = $(this);
if ($this.data('azh-linked-node')) {
$this.data('azh-linked-node').removeClass('azh-over');
}
});
if (options['control_text_attribute']) {
$('').appendTo($control);
} else {
$('').appendTo($control);
}
if (options['init']) {
if (typeof options['init'] === 'function') {
options['init']($control, $element);
}
}
switch (options['type']) {
case 'input-innertext':
$('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
$element.text($this.val());
$control.attr('data-value', $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var value = $.trim($element.text());
var $input = $this.find('input').val(value);
$this.attr('data-value', value);
});
break;
case 'radio-attribute':
var $radio = $('').appendTo($control);
var name = makeid();
$control.data('azh-radio-name', name);
if (typeof options['options'] !== 'string') {
for (var value in options['options']) {
var $radio_button = $('').appendTo($radio);
var id = makeid();
$radio_button.append('');
}
}
$radio.find('input[type="radio"]').on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
set_attribute_value($control, options, $element, options['attribute'], $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var name = $control.data('azh-radio-name');
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var $radio = $this.find('> div');
var $section = $element.closest('[data-section]');
var value = get_attribute_value($this, options, $element, options['attribute']);
if (typeof options['options'] === 'string') {
$section.find('[' + options['options'] + ']').each(function () {
var $radio_button = $('').appendTo($radio);
var id = makeid();
$radio_button.append('');
});
}
$radio.find('[value]').prop('checked', false);
$radio.find('[value="' + value + '"]').prop('checked', true);
$this.attr('data-value', value);
});
break;
case 'dropdown-attribute':
var $dropdown = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
set_attribute_value($control, options, $element, options['attribute'], $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
if (typeof options['options'] === 'object') {
for (var value in options['options']) {
$dropdown.append('');
}
}
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var $dropdown = $this.find('select');
var $section = $element.closest('[data-section]');
var value = get_attribute_value($this, options, $element, options['attribute']);
if (typeof options['options'] === 'string') {
$dropdown.empty();
var values = $element.attr(options['options']);
if (values) {
values = values.split('|');
$(values).each(function () {
$dropdown.append('');
});
} else {
$section.find('[' + options['options'] + ']').each(function () {
$dropdown.append('');
});
}
}
if (typeof options['options'] === 'function') {
$dropdown.empty();
var dynamic_options = options['options']($this, $element, function (dynamic_options) {
$dropdown.empty();
for (var v in dynamic_options) {
$dropdown.append('');
}
$dropdown.val(value);
});
for (var v in dynamic_options) {
$dropdown.append('');
}
}
$dropdown.val(value);
if (options['select2']) {
$dropdown.select2({
width: '100%'
}).on("select2:open", function (e) {
$(this).data('select2').$dropdown.off('click.azh, mousedown.azh, mouseup.azh').on('click.azh, mousedown.azh, mouseup.azh', function (event) {
event.stopPropagation();
event.preventDefault();
return false;
});
});
}
$this.attr('data-value', value);
});
break;
case 'ajax-dropdown-attribute':
var $dropdown = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var pattern_value = set_stored_attribute($element, options['attribute'], $this.val(), options['pattern'], options['default']);
$element.attr(options['attribute'], pattern_value);
$control.attr('data-value', pattern_value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var $dropdown = $this.find('select');
var $section = $element.closest('[data-section]');
var value = $element.attr(options['attribute']);
value = get_value_by_pattern(options, value);
$dropdown.select2({
ajax: {
url: options['ajaxurl'],
dataType: 'json'
},
width: '100%'
}).on("select2:open", function (e) {
$(this).data('select2').$dropdown.off('click.azh, mousedown.azh, mouseup.azh').on('click.azh, mousedown.azh, mouseup.azh', function (event) {
event.stopPropagation();
event.preventDefault();
return false;
});
});
if (value) {
$.post(options['ajaxurl'], {
'values': [value]
}, function (data) {
for (var v in data) {
$('').appendTo($dropdown);
}
}, 'json');
}
$this.attr('data-value', value);
});
break;
case 'radio-style':
var $radio = $('').appendTo($control);
var name = makeid();
$control.data('azh-radio-name', name);
for (var value in options['options']) {
var $radio_button = $('').appendTo($radio);
var id = makeid();
$radio_button.append('');
}
$radio.find('input[type="radio"]').on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
set_style_property($control, options, $element, options['property'], $this.val());
$control.attr('data-value', $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
add_responsive(function ($control) {
// $control.find('input:checked').trigger('change');
});
$control.on('azh-init', function () {
var $this = $(this);
var name = $control.data('azh-radio-name');
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var value = get_style_property($this, options, $element, options['property']);
value = get_value_by_pattern(options, value);
var $radio = $this.find('> div');
$radio.find('[value]').prop('checked', false);
$radio.find('[value="' + value + '"]').prop('checked', true);
$this.attr('data-value', value);
});
break;
case 'dropdown-style':
var $dropdown = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
set_style_property($control, options, $element, options['property'], $this.val());
$control.attr('data-value', $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
for (var value in options['options']) {
$dropdown.append('');
}
add_responsive(function ($control) {
// $control.find('select').trigger('change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var value = get_style_property($this, options, $element, options['property']);
value = get_value_by_pattern(options, value);
$this.find('select').val(value);
$this.attr('data-value', value);
});
break;
case 'integer-attribute':
$('').appendTo($control);
var $input = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var units = $control.data('azh-units') ? $control.data('azh-units') : '';
var value = $this.val() ? $this.val() + units : '';
set_attribute_value($control, options, $element, options['attribute'], value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
}).on('mousewheel', function (e) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var step = options['step'] ? options['step'] : 1;
if ($this.data('focus')) {
var d = step * e.originalEvent.wheelDelta / 120;
$this.val($this.val() + d);
$this.trigger('change');
return false;
}
});
integer_mouse_drag($input);
add_units();
add_responsive(function ($control) {
// $control.find('input').trigger('change');
$control.find('.azh-slider.ui-slider').slider('value', $control.find('input').val());
});
$control.on('azh-init', function () {
var $this = $(this);
var options = $this.data('azh-options');
var $element = $this.data('azh-linked-node');
var value = get_attribute_value($this, options, $element, options['attribute']);
if ('min' in options && 'max' in options && 'step' in options || options['slider']) {
if ($this.find('.azh-slider.ui-slider').length) {
if (value) {
var v = $this.find('.azh-slider.ui-slider').slider('value');
if (v !== parseFloat(value)) {
$this.find('.azh-slider.ui-slider').slider('value', value);
}
}
} else {
var min = 0;
var max = 100;
var step = 1;
var units = $this.data('azh-units');
if (options['min']) {
min = parseFloat(options['min']);
} else if (units && options['units'] && options['units'][units] && options['units'][units]['min']) {
min = parseFloat(options['units'][units]['min']);
}
if (options['max']) {
max = parseFloat(options['max']);
} else if (units && options['units'] && options['units'][units] && options['units'][units]['max']) {
max = parseFloat(options['units'][units]['max']);
}
if (options['step']) {
step = parseFloat(options['step']);
} else if (units && options['units'] && options['units'][units] && options['units'][units]['step']) {
step = parseFloat(options['units'][units]['step']);
}
$this.find('.azh-slider').slider({
min: min,
max: max,
step: step,
value: value ? parseFloat(value) : 0,
slide: function (event, ui) {
if (event.which) {
if (!options['refresh']) {
$this.find('input').val(parseFloat(ui.value)).trigger('change');
}
}
},
change: function (event, ui) {
if (event.which) {
if (options['refresh']) {
$this.find('input').val(parseFloat(ui.value)).trigger('change');
}
$this.trigger('azh-change');
}
}
}).find('.ui-slider-handle').on('contextmenu', function () {
$(this).closest('.azh-slider').slider("option", "value", 0);
$this.find('input').val('').trigger('change');
return false;
});
}
}
$this.find('input').val(parseFloat(value));
if (value) {
var units = '';
if (value.split(/[\-\.0-9]+/g).length === 2) {
units = value.split(/[\-\.0-9]+/g).pop();
}
$this.data('azh-units', units);
$this.find('[data-units="' + units + '"]').trigger('click');
}
});
break;
case 'input-attribute':
$('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var value = $this.val();
if (options['filter'] && options['filter'] in azh) {
value = azh[options['filter']](value);
$this.val(value);
}
set_attribute_value($control, options, $element, options['attribute'], value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var value = get_attribute_value($this, options, $element, options['attribute']);
var $input = $this.find('input').val(value);
$this.attr('data-value', value);
if (options['unique']) {
if (options['unique_exception']) {
if ($element.is(options['unique_exception'])) {
return;
}
}
var $unique_wrapper = $element.closest('[data-section]');
if (options['unique_wrapper']) {
$unique_wrapper = $element.closest(options['unique_wrapper']);
}
var unique = options['unique'];
var pattern = new RegExp('{' + options['attribute'] + '}', "g");
unique = unique.replace(pattern, $input.val());
if ($unique_wrapper.find(unique).length > 1) {
$this.find('label').get(0).style.setProperty("color", "red", "important");
$this.find('input').get(0).style.setProperty("color", "red", "important");
$this.attr('title', azh.i18n.value_must_be_unique_in_this_scope);
} else {
$this.find('label').get(0).style.setProperty("color", "", "");
$this.find('input').get(0).style.setProperty("color", "", "");
$this.attr('title', '');
}
}
});
break;
case 'textarea-attribute':
$('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var pattern_value = set_stored_attribute($element, options['attribute'], $this.val(), options['pattern'], options['default']);
$element.attr(options['attribute'], pattern_value);
$control.attr('data-value', pattern_value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var value = $element.attr($this.data('azh-options')['attribute']);
value = get_value_by_pattern(options, value);
var $textarea = $this.find('textarea').val(value);
$this.attr('data-value', value);
});
break;
case 'url-attribute':
$('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var pattern_value = set_stored_attribute($element, options['attribute'], $this.val(), options['pattern'], options['default']);
$element.attr(options['attribute'], pattern_value);
$control.attr('data-value', pattern_value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var options = $control.data('azh-options');
var $element = $this.data('azh-linked-node');
var value = $element.attr(options['attribute']);
value = get_value_by_pattern(options, value);
var $input = $this.find('input').val(value);
$this.attr('data-value', value);
});
$('
' + azh.i18n.edit_link + '
').appendTo($control).on('click', function (event) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
azh.open_link_select_dialog(event, function (url, target, title) {
$element.attr(options['attribute'], url);
set_stored_attribute($element, options['attribute'], url);
if (options['attribute'] === 'href') {
$element.attr('target', target ? target : '_self');
$element.attr('title', title);
set_stored_attribute($element, 'target', target ? target : '_self');
set_stored_attribute($element, 'title', title);
}
$control.trigger('azh-init');
}, $element.attr(options['attribute']), $element.attr('target'), $element.attr('title'));
});
break;
case 'video-url-attribute':
$('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var pattern_value = set_stored_attribute($element, options['attribute'], $this.val(), options['pattern'], options['default']);
$element.attr(options['attribute'], pattern_value);
$control.attr('data-value', pattern_value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var options = $control.data('azh-options');
var $element = $this.data('azh-linked-node');
var value = $element.attr(options['attribute']);
value = get_value_by_pattern(options, value);
var $input = $this.find('input').val(value);
$this.attr('data-value', value);
});
$('
' + azh.i18n.edit_link + '
').appendTo($control).on('click', function (event) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
azh.open_image_select_dialog(event, function (url, id) {
$element.attr(options['attribute'], url);
set_stored_attribute($element, options['attribute'], url);
$control.attr('data-value', 'true');
$control.find('input').val(url);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
}, false, 'video');
});
break;
case 'integer-list-style':
var $properties = $('').appendTo($control);
for (var property in options['properties']) {
var $property = $('').appendTo($properties);
var $input = $('').appendTo($property).on('change', function (event) {
var $this = $(this);
var property = $this.data('property');
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var units = $control.data('azh-units') ? $control.data('azh-units') : '';
var value = $this.val() ? $this.val() + units : '';
set_style_property($control, options, $element, property, value);
if (!event.isTrigger) {
if ($control.find('.azh-linked.azh-active').length) {
$control.find('.azh-linked').removeClass('azh-active');
for (var p in options['properties']) {
if (p !== property) {
$control.find('input[data-property="' + p + '"]').val(parseFloat(value)).trigger('change');
}
}
$control.find('.azh-linked').addClass('azh-active');
}
}
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
}).on('mousewheel', function (e) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var step = options['step'] ? options['step'] : 1;
if ($this.data('focus')) {
var d = step * e.originalEvent.wheelDelta / 120;
$this.val($this.val() + d);
$this.trigger('change');
return false;
}
});
integer_mouse_drag($input);
$('
' + options['properties'][property] + '
').appendTo($property);
}
add_units();
add_responsive(function ($control) {
// $control.find('input').trigger('change');
});
$('').appendTo($control).on('click', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
if ($this.is('.azh-active')) {
$this.removeClass('azh-active');
} else {
var first_property = Object.keys(options['properties'])[0];
for (var property in options['properties']) {
if (first_property !== property) {
$control.find('input[data-property="' + property + '"]').val(parseFloat($control.find('input[data-property="' + first_property + '"]').val())).trigger('change');
}
}
$this.addClass('azh-active');
}
});
$control.on('azh-init', function () {
var $this = $(this);
var options = $this.data('azh-options');
var $element = $this.data('azh-linked-node');
var values = [];
for (var property in options['properties']) {
var value = get_style_property($this, options, $element, property);
value = get_value_by_pattern(options, value);
$this.find('input[data-property="' + property + '"]').val(parseFloat(value));
values.push(value);
}
if (values.length && values[0]) {
var units = '';
if (values[0].split(/[\-\.0-9]+/g).length === 2) {
units = values[0].split(/[\-\.0-9]+/g).pop();
}
$this.data('azh-units', units);
$this.find('[data-units="' + units + '"]').trigger('click');
}
});
break;
case 'integer-style':
$('').appendTo($control);
var $input = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var units = $control.data('azh-units') ? $control.data('azh-units') : '';
var value = $this.val() ? $this.val() + units : '';
set_style_property($control, options, $element, options['property'], value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
}).on('mousewheel', function (e) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var step = options['step'] ? options['step'] : 1;
if ($this.data('focus')) {
var d = step * e.originalEvent.wheelDelta / 120;
$this.val($this.val() + d);
$this.trigger('change');
return false;
}
});
integer_mouse_drag($input);
add_units();
add_responsive(function ($control) {
// $control.find('input').trigger('change');
$control.find('.azh-slider.ui-slider').slider('value', $control.find('input').val());
});
$control.on('azh-init', function () {
var $this = $(this);
var options = $this.data('azh-options');
var $element = $this.data('azh-linked-node');
var value = get_style_property($this, options, $element, options['property']);
value = get_value_by_pattern(options, value);
if ('min' in options && 'max' in options && 'step' in options || options['slider']) {
if ($this.find('.azh-slider.ui-slider').length) {
if (value) {
var v = $this.find('.azh-slider.ui-slider').slider('value');
if (v !== parseFloat(value)) {
$this.find('.azh-slider.ui-slider').slider('value', value);
}
}
} else {
var min = 0;
var max = 100;
var step = 1;
var units = $this.data('azh-units');
if (options['min']) {
min = parseFloat(options['min']);
} else if (units && options['units'] && options['units'][units] && options['units'][units]['min']) {
min = parseFloat(options['units'][units]['min']);
}
if (options['max']) {
max = parseFloat(options['max']);
} else if (units && options['units'] && options['units'][units] && options['units'][units]['max']) {
max = parseFloat(options['units'][units]['max']);
}
if (options['step']) {
step = parseFloat(options['step']);
} else if (units && options['units'] && options['units'][units] && options['units'][units]['step']) {
step = parseFloat(options['units'][units]['step']);
}
$this.find('.azh-slider').slider({
min: min,
max: max,
step: step,
value: value ? parseFloat(value) : 0,
slide: function (event, ui) {
if (event.which) {
if (!options['refresh']) {
$this.find('input').val(parseFloat(ui.value)).trigger('change');
}
}
},
change: function (event, ui) {
if (event.which) {
if (options['refresh']) {
$this.find('input').val(parseFloat(ui.value)).trigger('change');
}
$this.trigger('azh-change');
}
}
}).find('.ui-slider-handle').on('contextmenu', function () {
$(this).closest('.azh-slider').slider("option", "value", 0);
$this.find('input').val('').trigger('change');
return false;
});
}
}
$this.find('input').val(parseFloat(value));
if (value) {
var units = '';
if (value.split(/[\-\.0-9]+/g).length === 2) {
units = value.split(/[\-\.0-9]+/g).pop();
}
$this.data('azh-units', units);
$this.find('[data-units="' + units + '"]').trigger('click');
}
});
break;
case 'color-style':
$control.addClass('azh-color');
$('').appendTo($control).on('change', function () {
var $control = $(this).closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var color = hexToRgbA($control.find('input[type="text"]').val(), 1);
set_style_property($control, options, $element, options['property'], color);
if (options['refresh']) {
$(this).wpColorPicker('close');
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
add_responsive(function ($control) {
// $control.find('input').trigger('change');
$control.find('input').wpColorPicker('color', $control.find('input').val());
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var color = get_style_property($this, options, $element, options['property']);
color = get_value_by_pattern(options, color);
var $input = $this.find('input[type="text"]').val(color);
if ($input.closest('.wp-picker-container').length) {
$input.wpColorPicker('color', color);
} else {
$input.wpColorPicker({
change: function (event, ui) {
if (options['refresh']) {
$input.closest('.wp-picker-container').off('mouseleave').on('mouseleave', function () {
var color = ui.color.toString();
// if (hexToRgbA($input.val(), 1) !== hexToRgbA(color, 1)) {
$input.val(color);
$input.trigger('change');
// }
$input.closest('.wp-picker-container').off('mouseleave');
});
} else {
var color = ui.color.toString();
if (hexToRgbA($input.val(), 1) !== hexToRgbA(color, 1)) {
$input.val(color);
$input.trigger('change');
}
}
$this.trigger('azh-change');
},
clear: function (event) {
if (event.type === 'click') {
if (options['refresh']) {
$input.closest('.wp-picker-container').off('mouseleave').on('mouseleave', function () {
$input.val('');
$input.trigger('change');
$input.closest('.wp-picker-container').off('mouseleave');
});
} else {
$input.trigger('change');
}
$this.trigger('azh-change');
}
}
});
}
});
break;
case 'color-attribute':
$control.addClass('azh-color');
$('').appendTo($control).on('change', function () {
var $control = $(this).closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var color = $control.find('input[type="text"]').val();
if (options['alpha']) {
color = hexToRgbA(color, 1);
}
var pattern_value = set_stored_attribute($element, options['attribute'], color, options['pattern'], options['default']);
$element.attr(options['attribute'], pattern_value);
if (options['refresh']) {
$(this).wpColorPicker('close');
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var color = $element.attr(options['attribute']);
color = get_value_by_pattern(options, color);
var $input = $this.find('input[type="text"]').val(color);
if ($input.closest('.wp-picker-container').length) {
$input.wpColorPicker('color', color);
} else {
$input.wpColorPicker({
change: function (event, ui) {
if (options['refresh']) {
$input.closest('.wp-picker-container').off('mouseleave').on('mouseleave', function () {
var color = ui.color.toString();
// if (hexToRgbA($input.val(), 1) !== hexToRgbA(color, 1)) {
$input.val(color);
$input.trigger('change');
// }
$input.closest('.wp-picker-container').off('mouseleave');
});
} else {
var color = ui.color.toString();
if (hexToRgbA($input.val(), 1) !== hexToRgbA(color, 1)) {
$input.val(color);
$input.trigger('change');
}
}
$this.trigger('azh-change');
},
clear: function (event) {
if (event.type === 'click') {
if (options['refresh']) {
$input.closest('.wp-picker-container').off('mouseleave').on('mouseleave', function () {
$input.trigger('change');
$input.closest('.wp-picker-container').off('mouseleave');
});
} else {
$input.trigger('change');
}
$this.trigger('azh-change');
}
}
});
}
});
break;
case 'toggle-attribute':
var $checkbox = $('').prependTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
if ($this.prop('checked')) {
$element.attr(options['attribute'], ('true_value' in options ? options['true_value'] : 'true'));
set_stored_attribute($element, options['attribute'], ('true_value' in options ? options['true_value'] : 'true'));
$control.attr('data-value', 'true');
} else {
$element.attr(options['attribute'], ('false_value' in options ? options['false_value'] : 'false'));
set_stored_attribute($element, options['attribute'], ('false_value' in options ? options['false_value'] : 'false'));
$control.attr('data-value', 'false');
}
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var checked = false;
if ('false_value' in options) {
checked = $element.attr($this.data('azh-options')['attribute']) !== options['false_value'];
} else if ('true_value' in options) {
checked = $element.attr($this.data('azh-options')['attribute']) === options['true_value'];
} else {
checked = $element.attr($this.data('azh-options')['attribute']) === 'true' ? true : false;
}
$this.find('input').prop('checked', checked);
var id = makeid();
$(this).find('input').attr('id', id);
$(this).find('label').attr('for', id);
$this.attr('data-value', checked);
});
break;
case 'toggle-url-argument':
var $checkbox = $('').prependTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var url = $element.attr(options['attribute']);
if (url) {
if ($this.prop('checked')) {
url = set_url_argument(url, options['argument'], options['true_value']);
} else {
url = set_url_argument(url, options['argument'], options['false_value']);
}
$element.attr(options['attribute'], url);
set_stored_attribute($element, options['attribute'], url);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
}
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var url = $element.attr(options['attribute']);
if (url) {
var value = get_url_argument(url, options['argument']);
var checked = (options['default'] ? options['default'] : false);
if (value) {
if (value === options['true_value']) {
checked = true;
}
if (value === options['false_value']) {
checked = false;
}
}
$this.find('input').prop('checked', checked);
$this.attr('data-value', checked);
}
var id = makeid();
$(this).find('input').attr('id', id);
$(this).find('label').attr('for', id);
});
break;
case 'input-url-argument':
$('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var url = $element.attr(options['attribute']);
url = set_url_argument(url, options['argument'], $this.val());
set_attribute_value($control, options, $element, options['attribute'], url);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var url = $element.attr(options['attribute']);
if (url) {
var value = get_url_argument(url, options['argument']);
$this.find('input').val(value);
$this.attr('data-value', value);
}
});
break;
case 'font-family':
var $dropdown = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var font_family = $.trim($this.val().replace(/['"]/g, ''));
set_style_property($control, options, $element, options['property'], "'" + font_family + "'");
var font_url = 'https://fonts.googleapis.com/css?family=' + font_family + ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
if (!loaded_scripts[font_url]) {
if (azh.google_fonts_locale_subsets[ azh.locale ]) {
font_url += '&subset=' + azh.google_fonts_locale_subsets[ azh.locale ];
}
loaded_scripts[font_url] = azh.$("", {
rel: "stylesheet",
type: "text/css",
href: font_url
}).appendTo("head");
}
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var options = $this.data('azh-options');
var $dropdown = $this.find('select');
var $element = $this.data('azh-linked-node');
var font_family = get_style_property($this, options, $element, options['property']);
font_family = font_family ? font_family : '';
font_family = font_family.replace(/['"]/g, '');
var fonts = azh.google_fonts;
var font_families = style_in_page('fontFamily');
$(font_families).each(function() {
fonts = fonts.concat(this.split(',').map(function(f){return $.trim(f.replace(/['"]/g,''));}));
});
fonts = $.unique(fonts);
$dropdown.append('');
$(fonts).each(function () {
$dropdown.append('');
});
$dropdown.val(font_family);
$dropdown.select2({
width: '100%'
}).on("select2:open", function (e) {
$(this).data('select2').$dropdown.off('click.azh, mousedown.azh, mouseup.azh').on('click.azh, mousedown.azh, mouseup.azh', function (event) {
event.stopPropagation();
event.preventDefault();
return false;
});
});
});
break;
case 'background-image':
$control.addClass('azh-image');
$('').appendTo($control).on('contextmenu', function (event) {
event.preventDefault();
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var bi = get_style_property($control, options, $element, 'background-image');
if (bi.indexOf($this.attr('src')) >= 0) {
bi = bi.replace($this.attr('src'), '/');
} else {
bi = "url('/')";
}
set_style_property($control, options, $element, 'background-image', bi);
$this.attr('src', no_image);
$this.attr('alt', azh.i18n.select_image);
$control.attr('data-value', 'false');
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
}).on('click', function (event) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
azh.open_image_select_dialog(event, function (url, id) {
var bi = get_style_property($control, options, $element, 'background-image');
if (bi.indexOf($this.attr('src')) >= 0) {
bi = bi.replace($this.attr('src'), url);
} else {
bi = "url('" + url + "')";
}
set_style_property($control, options, $element, 'background-image', bi);
$this.attr('src', url);
$control.attr('data-value', 'true');
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
}).on('error', function () {
$(this).attr('src', no_image);
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var $image = $this.find('img').first();
$image.detach();
$this.find('img').remove();
var bi = get_style_property($this, options, $element, 'background-image');
var pattern = /url\(['"]?([^'"\)]+)['"]?\)/gi;
var url = null;
while ((url = pattern.exec(bi)) != null) {
if (url[1] === '' || url[1] === '/') {
$image.clone(true).appendTo($this).attr('src', no_image);
$this.attr('data-value', 'false');
} else {
$image.clone(true).appendTo($this).attr('src', url[1]);
$this.attr('data-value', 'true');
}
}
if ($this.find('img').length === 0) {
$image.clone(true).appendTo($this).attr('src', no_image);
}
});
break;
case 'image-attribute':
$control.addClass('azh-image');
var url = $element.attr(options['attribute']);
$('').appendTo($control).on('contextmenu', function (event) {
event.preventDefault();
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
$this.attr('src', no_image);
$this.attr('alt', azh.i18n.select_image);
$control.attr('data-value', 'false');
$element.attr(options['attribute'], '');
set_stored_attribute($element, options['attribute'], '');
}).on('click', function (event) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
azh.open_image_select_dialog(event, function (url, id) {
$this.attr('src', url);
$control.attr('data-value', 'true');
$element.attr(options['attribute'], url);
set_stored_attribute($element, options['attribute'], url);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
}).on('error', function () {
$(this).attr('src', no_image);
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var url = $element.attr($this.data('azh-options')['attribute']);
if (url) {
$this.find('img').attr('src', url);
$this.attr('data-value', 'true');
} else {
$this.find('img').attr('src', no_image);
$this.attr('data-value', 'false');
}
});
break;
case 'icon-class':
$('
' + azh.i18n.edit_icon + '
').appendTo($control).on('click', function (event) {
var $this = $(this);
var $control = $this.closest('.azh-control');
var options = $control.data('azh-options');
var $element = $control.data('azh-linked-node');
var classes = '';
if (options['multiplying_selector']) {
classes = $element.find(options['multiplying_selector']).first().attr('class');
} else {
classes = $element.attr('class');
}
var az_classes = classes.split(' ').filter(function (v) {
return (v.indexOf('az-') >= 0 ? v : false);
});
var azh_classes = classes.split(' ').filter(function (v) {
return (v.indexOf('azh-') >= 0 ? v : false);
});
azh.open_icon_select_dialog(event, classes, function (icon_class) {
load_required_scripts(icon_class);
var value = az_classes.concat(icon_class.split(' ')).join(' ');
set_attribute_value($control, options, $element, 'class', value);
if (options['multiplying_selector']) {
$element.find(options['multiplying_selector']).each(function () {
azh.$(this).addClass(azh_classes.join(' '));
});
} else {
$element.addClass(azh_classes.join(' '));
}
});
});
break;
case 'exists-class':
var $checkbox = $('').prependTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
if ($this.prop('checked')) {
$element.addClass(options['class']);
add_to_stored_classes($element, options['class']);
$control.attr('data-value', 'true');
} else {
$element.removeClass(options['class']);
remove_from_stored_classes($element, options['class']);
$control.attr('data-value', 'false');
}
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
$this.find('input').prop('checked', $element.hasClass(options['class']));
$this.attr('data-value', $element.hasClass(options['class']) ? 'true' : 'false');
var id = makeid();
$(this).find('input').attr('id', id);
$(this).find('label').attr('for', id);
});
break;
case 'dropdown-classes':
var $dropdown = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
for (var c in options['classes']) {
$element.removeClass(c);
remove_from_stored_classes($element, c);
}
$element.addClass($this.val());
add_to_stored_classes($element, $this.val());
$control.attr('data-value', $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
if (typeof options['classes'] !== 'string') {
for (var value in options['classes']) {
$dropdown.append('');
}
}
$control.on('azh-init', function () {
var $this = $(this);
var name = $control.data('azh-radio-name');
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var value = '';
for (var c in options['classes']) {
if ($element.is('.' + c)) {
value = c;
break;
}
}
var $dropdown = $this.find('select');
$dropdown.val(value);
$this.attr('data-value', value);
});
break;
case 'radio-classes':
var $radio = $('').appendTo($control);
var name = makeid();
$control.data('azh-radio-name', name);
for (var value in options['classes']) {
var $radio_button = $('').appendTo($radio);
var id = makeid();
$radio_button.append('');
}
$radio.find('input[type="radio"]').on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
for (var c in options['classes']) {
$element.removeClass(c);
remove_from_stored_classes($element, c);
}
$element.addClass($this.val());
add_to_stored_classes($element, $this.val());
$control.attr('data-value', $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var name = $control.data('azh-radio-name');
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var value = '';
for (var c in options['classes']) {
if ($element.is('.' + c)) {
value = c;
break;
}
}
var $radio = $this.find('> div');
$radio.find('[value]').prop('checked', false);
$radio.find('[value="' + value + '"]').prop('checked', true);
$this.attr('data-value', value);
});
break;
case 'exists-attribute':
var $checkbox = $('').prependTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
if ($this.prop('checked')) {
var value = options['value'] ? options['value'] : '';
$element.attr(options['attribute'], value);
set_stored_attribute($element, options['attribute'], value);
$control.attr('data-value', 'true');
} else {
$element.removeAttr(options['attribute']);
remove_stored_attribute($element, options['attribute']);
$control.attr('data-value', 'false');
}
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
$this.find('input').prop('checked', typeof $element.attr($this.data('azh-options')['attribute']) !== typeof undefined && $element.attr($this.data('azh-options')['attribute']) !== false);
$this.attr('data-value', $this.find('input').prop('checked') ? 'true' : 'false');
var id = makeid();
$(this).find('input').attr('id', id);
$(this).find('label').attr('for', id);
});
break;
case 'exists-style':
var $checkbox = $('').prependTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var value = options['value'] ? options['value'] : '';
if ($this.prop('checked')) {
$control.attr('data-value', 'true');
} else {
value = '';
$control.attr('data-value', 'false');
}
set_style_property($control, options, $element, options['property'], value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
add_responsive(function ($control) {
// $control.find('input').trigger('change');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var value = get_style_property($this, options, $element, options['property']);
value = value ? true : false;
$this.find('input').prop('checked', value);
$this.attr('data-value', value ? 'true' : 'false');
var id = makeid();
$(this).find('input').attr('id', id);
$(this).find('label').attr('for', id);
});
break;
case 'svg_content':
$control.addClass('azh-svg');
$('').appendTo($control).css({
position: 'fixed',
left: '-1000px',
top: '-1000px'
}).on('click', function (event) {
event.stopPropagation();
}).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var file = $control.find('.azh-file-input').get(0).files[0];
var reader = new FileReader();
reader.onload = function (e) {
var contents = e.target.result;
try {
var xml = $.parseXML(contents);
for (var i = xml.documentElement.attributes.length - 1; i >= 0; i--) {
if (['height', 'width', 'viewbox'].indexOf(xml.documentElement.attributes[i].name.toLowerCase()) < 0) {
xml.documentElement.removeAttribute(xml.documentElement.attributes[i].name);
}
}
xml = xml.documentElement.outerHTML;
xml = xml.replace(/<(\/?)([^:>\s]*:)?([^>]+)>/g, "<$1$3>");
$element.html(xml);
store_html($element.children().get(0));
$control.find('.azh-svg-wrapper').html(xml);
clear_element_utility($element);
$control.attr('data-value', 'true');
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
} catch (e) {
}
};
reader.readAsText(file);
});
$('').appendTo($control).on('contextmenu', function (event) {
event.preventDefault();
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
$control.find('.azh-svg-wrapper').empty().append('');
$control.attr('data-value', 'false');
$element.empty();
}).on('click', function (event) {
var $this = $(this);
var $control = $this.closest('.azh-control');
$control.find('.azh-file-input').trigger('click');
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var $svg = $element.find('svg');
var $wrapper = $this.find('.azh-svg-wrapper');
$wrapper.empty();
if ($svg.length) {
$wrapper.append($svg.clone());
$this.attr('data-value', 'true');
} else {
$wrapper.append('');
$this.attr('data-value', 'false');
}
});
break;
case 'html-switcher':
var $dropdown = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
var value = $this.val();
save_utility_state();
if (value) {
var html = azh.html[value];
$element.html(html);
$($element.children()).each(function () {
store_html(this);
});
} else {
$element.empty();
}
set_stored_attribute($element, 'data-html-switcher', value);
$element.attr('data-html-switcher', value);
if (value) {
$($element.children()).each(function () {
customization_init($(this));
});
clear_element_utility($element);
}
restore_utility_state();
$control.attr('data-value', value);
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
if (typeof options['options'] !== 'string') {
for (var value in options['options']) {
$dropdown.append('');
}
}
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var $dropdown = $this.find('select');
var value = $element.attr('data-html-switcher');
$dropdown.val(value);
$this.attr('data-value', value);
});
break;
case 'html-tag':
var $dropdown = $('').appendTo($control).on('change', function () {
var $this = $(this);
var $control = $this.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
save_utility_state();
var html = extract_html($element);
$element.empty();
set_stored_tag($element, $this.val());
var $new_element = azh.$($element.data('azh-open-tag') + html + $element.data('azh-close-tag'));
$element.replaceWith($new_element);
store_html($new_element.get(0));
customization_init($new_element);
clear_element_utility($new_element);
restore_utility_state();
$control.data('azh-linked-node', $new_element);
$control.attr('data-value', $this.val());
if (options['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
$element.trigger('azh-change');
$control.trigger('azh-change');
});
if (typeof options['options'] !== 'string') {
for (var value in options['options']) {
$dropdown.append('');
}
}
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var options = $this.data('azh-options');
var $dropdown = $this.find('select');
var value = $element.prop("tagName").toLowerCase();
$dropdown.val(value);
$this.attr('data-value', value);
});
break;
case 'post-autocomplete':
var $input = $('').appendTo($control).on("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).autocomplete("instance").menu.active) {
event.preventDefault();
}
}).on("keydown keyup blur", function (event) {
var $input = $(this);
if ($input.val() == '') {
var $control = $input.closest('.azh-control');
var $element = $control.data('azh-linked-node');
var options = $control.data('azh-options');
$element.attr(options['attribute'], '');
set_stored_attribute($element, options['attribute'], '');
}
});
$control.on('azh-init', function () {
var $this = $(this);
var $element = $this.data('azh-linked-node');
var value = $element.attr($this.data('azh-options')['attribute']);
var $input = $this.find('input');
$input.autocomplete({
minLength: 0,
source: function (request, response) {
var $input = this.element;
if (request.term != '') {
var $control = $input.closest('.azh-control');
var $element = $control.data('azh-linked-node');
$.post(azh.ajaxurl, {
'action': 'azh_posts_autocomplete',
'exclude': $element.attr($control.data('azh-options')['attribute']),
'search': request.term
}, function (data) {
response(data);
}, 'json');
} else {
response();
}
},
focus: function (event, ui) {
return false;
},
select: function (event, ui) {
var $input = $(this);
if (ui.item) {
var $control = $input.closest('.azh-control');
var $element = $control.data('azh-linked-node');
$element.attr($this.data('azh-options')['attribute'], ui.item.value);
set_stored_attribute($element, $this.data('azh-options')['attribute'], ui.item.value);
if ($this.data('azh-options')['refresh']) {
if (typeof options['refresh'] === 'function') {
options['refresh']($control, $element);
} else {
section_refresh($element.closest('[data-section]'));
}
}
}
return false;
}
});
if ($.trim(value) != '') {
$.post(azh.ajaxurl, {
'action': 'azh_posts_autocomplete_labels',
'values': value
}, function (data) {
$input.val(Object.keys(data).map(function (item) {
return data[item]
}).join(' '));
}, 'json');
}
});
break;
}
if (options['description']) {
$('
' + options['description'] + '
').appendTo($control);
}
$control.data('azh-options', options);
$control.data('azh-linked-node', $element);
if (options['init']) {
options['init']($control, $element, options);
}
if ('menu' in options && options['menu'] == 'utility') {
setTimeout(function() {
$control.trigger('azh-init');
});
$utility.children('.azh-controls-list').append($control);
$utility.data('azh-filled', true);
} else {
var context = $element.data('azh-context');
if (!context) {
context = [];
}
context.push($control);
$element.data('azh-context', context);
$element.addClass('azh-context');
}
controls.push($control);
$element.addClass('azh-linked-controls');
$element.data('azh-linked-controls', controls);
}
}
function make_element_empty($element) {
if ($element.is('[data-element*="shortcode"]')) {
$element.off('mouseenter.azh-shortcode').off('mouseleave.azh-shortcode');
}
var name = $.trim($element.attr('data-element'));
if (azh.files_settings[name] && azh.files_settings[name].wrapper_classes) {
$(azh.files_settings[name].wrapper_classes).each(function () {
$element.removeClass(this);
remove_from_stored_classes($element, this);
});
}
$element.attr('data-element', '');
set_stored_attribute($element, 'data-element', '');
$element.data('element', '');
$element.data('azh-linked-controls', false);
if ($element.attr('contenteditable')) {
$element.attr('contenteditable', 'false');
$element.removeClass('az-contenteditable');
remove_from_stored_classes($element, 'az-contenteditable');
}
$element.empty();
}
function clear_element_classes($element) {
var az_classes = $element.attr('class').split(' ').filter(function (v) {
return (v.indexOf('az-') >= 0 ? v : false);
});
var azh_classes = $element.attr('class').split(' ').filter(function (v) {
return (v.indexOf('azh-') >= 0 ? v : false);
});
$element.attr('class', azh_classes.concat(az_classes).join(' '));
set_stored_attribute($element, 'class', az_classes.join(' '));
}
function init_element_wrapper_class($element, $element_controls) {
var name = $.trim($element.attr('data-element'));
if (azh.files_settings[name] && azh.files_settings[name].wrapper_classes) {
$(azh.files_settings[name].wrapper_classes).each(function () {
$element.addClass(this);
add_to_stored_classes($element, this);
});
}
$element_controls.addClass($element.attr('class'));
}
function create_element_controls($element) {
var $element_controls = $('').appendTo(azh.controls_container).on('mouseenter', function () {
$(this).data('azh-linked-element').addClass('azh-over');
}).on('mouseleave', function () {
$(this).data('azh-linked-element').removeClass('azh-over');
});
init_element_wrapper_class($element, $element_controls);
$element_controls.hide();
$element.data('azh-controls', $element_controls);
$element_controls.data('azh-linked-element', $element);
$element.addClass('azh-controls');
$element_controls.append('
' + $.trim($element.data('element')) + '
');
setTimeout(function () {
set_element_controls_position($element, $element_controls);
});
if ($element.closest('.az-no-utility').length) {
$element_controls.addClass('azh-no-utility');
}
return $element_controls;
}
function get_copy_element_button($element_controls) {
return $('').appendTo($element_controls).on('click', function () {
var $button = $(this);
var $element = $button.data('azh-linked-element');
$element_controls = $element.data('azh-controls');
$element.find('.az-free-positioning').each(function () {
set_stored_attribute(azh.$(this), 'data-height', azh.$(this).height());
set_stored_attribute(azh.$(this), 'data-width', azh.$(this).width());
});
var $html = $element.clone(true);
azh.liquid_prepare($html);
var code = html_uglify(extract_html($html, true));
azh.clipboard = {
'code': code,
'path': $element.attr('data-element')
};
azh.notify(azh.i18n.copied);
$.post(azh.ajaxurl, {
'action': 'azh_copy',
'code': code,
'path': $element.attr('data-element')
}, function (data) {
});
return false;
});
}
function get_remove_element_button($element_controls) {
return $('').appendTo($element_controls).on('click', function () {
var $button = $(this);
var $element = $button.data('azh-linked-element');
var $section = $element.closest('[data-section]');
var $cloneable = $element.closest('[data-cloneable], [data-cloneable-inline]');
$element_controls = $element.data('azh-controls');
var $linked_element = get_linked_element($element);
if ($linked_element) {
remove_visible_controls($linked_element);
$linked_element.remove();
}
if ($element.parent('[data-cloneable], [data-cloneable-inline]').length === 1) {
if ($element.parent().children().length === 1) {
remove_visible_controls($element);
make_element_empty($element);
create_element_controls($element);
} else {
remove_visible_controls($element);
$element.remove();
}
} else {
var $element_wrapper = $element.closest('.az-element-wrapper');
if ($cloneable.children('.az-element-wrapper').length === 1) {
remove_visible_controls($element);
make_element_empty($element);
create_element_controls($element);
} else {
remove_visible_controls($element_wrapper);
$element_wrapper.remove();
}
}
if ($cloneable.length) {
if (is_section_refresh($cloneable)) {
section_refresh($section);
} else {
$cloneable.trigger('azh-refresh');
}
}
_.defer(refresh_elements_hierarchy_partial, $cloneable);
azh.change();
return false;
});
}
function get_add_element_button($element_controls, after) {
return $('').appendTo($element_controls).on('click', function () {
var $button = $(this);
var $element = $button.data('azh-linked-element');
var $element_wrapper = $element.closest('.az-element-wrapper');
var $new_element_wrapper = false;
if ($element_wrapper.is('[data-element]')) {
if ($element_wrapper.contents().length === 0) {
$new_element_wrapper = $element_wrapper;
} else {
$new_element_wrapper = $element_wrapper.clone(true);
if (after) {
$new_element_wrapper.insertAfter($element_wrapper);
} else {
$new_element_wrapper.insertBefore($element_wrapper);
}
}
} else {
if ($element_wrapper.find('[data-element]').children().length === 0) {
$new_element_wrapper = $element_wrapper;
} else {
$new_element_wrapper = $element_wrapper.clone(true);
if (after) {
$new_element_wrapper.insertAfter($element_wrapper);
} else {
$new_element_wrapper.insertBefore($element_wrapper);
}
}
}
var $new_element = $new_element_wrapper.is('[data-element]') ? $new_element_wrapper : $new_element_wrapper.find('[data-element]');
make_element_empty($new_element);
clear_element_classes($new_element);
create_element_controls($new_element);
setTimeout(function () {
$new_element.trigger('click');
var $element_controls = $new_element.data('azh-controls');
if ($element_controls) {
$element_controls.find('.azh-utility-wrapper').trigger('click');
}
});
azh.change();
return false;
});
}
function width_height_resizer($element) {
function width_right_resizer($element) {
if (!$element.data('azh-width-right-resizer')) {
var element_rect = $element.get(0).getBoundingClientRect();
var $resizer = $('').appendTo(azh.controls_container).on('mousedown', function (event) {
var $resizer = $(this);
$resizer.data('azh-drag', true);
$resizer.data('azh-drag-x', event.pageX);
$resizer.data('azh-start-drag-x', event.pageX);
$resizer.css('left', element_rect.left + element_rect.width - 5);
if ($element.get(0).style['width'] == '') {
set_width($element, $element.width());
}
azh.document_off('mouseup.resizer').document_on('mouseup.resizer', function (e) {
if ($resizer.data('azh-start-drag-x') === e.pageX) {
$element.css('width', '');
set_stored_style($element, 'width', '');
}
$resizer.data('azh-drag', false);
if (!$resizer.is(':hover')) {
azh.document_off('mouseup.resizer');
}
});
azh.document_off('mousemove.resizer').document_on('mousemove.resizer', function (e) {
if ($resizer.data('azh-drag')) {
var d = e.pageX - $resizer.data('azh-drag-x');
var $zoom = $element.closest('.azh-zoom');
if ($zoom.length) {
d = d / $zoom.data('azh-scale');
}
var v = (parseFloat(get_stored_style($element, 'width')) + d);
v = isNaN(v) ? 0 : v;
if (v < 0) {
v = 0;
}
set_width($element, v);
$resizer.data('azh-drag-x', e.pageX);
element_rect = $element.get(0).getBoundingClientRect();
$resizer.css('left', element_rect.left + element_rect.width - 5);
}
});
event.stopPropagation();
event.preventDefault();
return false;
}).on('mouseleave', function (event) {
var $resizer = $(this);
if (!$resizer.data('azh-drag') && !($element.is(':hover') || $element.is('.azh-hover'))) {
$element.data('azh-width-right-resizer', false);
$resizer.remove();
$element.trigger('mouseleave');
}
}).css({
"top": element_rect.top + azh.window.scrollTop(),
"left": element_rect.left + element_rect.width - 5,
"height": element_rect.height,
"width": 10
});
$element.data('azh-width-right-resizer', $resizer);
}
}
function width_left_resizer($element) {
if (!$element.data('azh-width-left-resizer')) {
var element_rect = $element.get(0).getBoundingClientRect();
var $resizer = $('').appendTo(azh.controls_container).on('mousedown', function (event) {
var $resizer = $(this);
$resizer.data('azh-drag', true);
$resizer.data('azh-drag-x', event.pageX);
$resizer.data('azh-start-drag-x', event.pageX);
$resizer.css('left', element_rect.left - 5);
if ($element.get(0).style['width'] == '') {
set_width($element, $element.width());
}
azh.document_off('mouseup.resizer').document_on('mouseup.resizer', function (e) {
if ($resizer.data('azh-start-drag-x') === e.pageX) {
$element.css('width', '');
set_stored_style($element, 'width', '');
}
$resizer.data('azh-drag', false);
if (!$resizer.is(':hover')) {
azh.document_off('mouseup.resizer');
}
});
azh.document_off('mousemove.resizer').document_on('mousemove.resizer', function (e) {
if ($resizer.data('azh-drag')) {
var d = $resizer.data('azh-drag-x') - e.pageX;
var $zoom = $element.closest('.azh-zoom');
if ($zoom.length) {
d = d / $zoom.data('azh-scale');
}
var v = (parseFloat(get_stored_style($element, 'width')) + d);
v = isNaN(v) ? 0 : v;
if (v < 0) {
v = 0;
}
set_width($element, v);
$resizer.data('azh-drag-x', e.pageX);
element_rect = $element.get(0).getBoundingClientRect();
$resizer.css('left', element_rect.left - 5);
}
});
event.stopPropagation();
event.preventDefault();
return false;
}).on('mouseleave', function (event) {
var $resizer = $(this);
if (!$resizer.data('azh-drag') && !($element.is(':hover') || $element.is('.azh-hover'))) {
$element.data('azh-width-left-resizer', false);
$resizer.remove();
$element.trigger('mouseleave');
}
}).css({
"top": element_rect.top + azh.window.scrollTop(),
"left": element_rect.left - 5,
"height": element_rect.height,
"width": 10
});
$element.data('azh-width-left-resizer', $resizer);
}
}
function height_top_resizer($element) {
if (!$element.data('azh-height-top-resizer')) {
var element_rect = $element.get(0).getBoundingClientRect();
var $resizer = $('').appendTo(azh.controls_container).on('mousedown', function (event) {
var $resizer = $(this);
$resizer.data('azh-drag', true);
$resizer.data('azh-drag-y', event.pageY);
$resizer.data('azh-start-drag-y', event.pageY);
$resizer.css('top', element_rect.top + azh.window.scrollTop() - 5);
if ($element.get(0).style['height'] == '') {
set_height($element, $element.height());
}
azh.document_off('mouseup.resizer').document_on('mouseup.resizer', function (e) {
if ($resizer.data('azh-start-drag-y') === e.pageY) {
$element.css('height', '');
set_stored_style($element, 'height', '');
}
$resizer.data('azh-drag', false);
if (!$resizer.is(':hover')) {
azh.document_off('mouseup.resizer');
}
});
azh.document_off('mousemove.resizer').document_on('mousemove.resizer', function (e) {
if ($resizer.data('azh-drag')) {
var d = $resizer.data('azh-drag-y') - e.pageY;
var $zoom = $element.closest('.azh-zoom');
if ($zoom.length) {
d = d / $zoom.data('azh-scale');
}
var v = (parseFloat(get_stored_style($element, 'height')) + d);
v = isNaN(v) ? 0 : v;
if (v < 0) {
v = 0;
}
set_height($element, v);
$resizer.data('azh-drag-y', e.pageY);
element_rect = $element.get(0).getBoundingClientRect();
$resizer.css('top', element_rect.top + azh.window.scrollTop() - 5);
}
});
event.stopPropagation();
event.preventDefault();
return false;
}).on('mouseleave', function (event) {
var $resizer = $(this);
if (!$resizer.data('azh-drag') && !($element.is(':hover') || $element.is('.azh-hover'))) {
$element.data('azh-height-top-resizer', false);
$resizer.remove();
$element.trigger('mouseleave');
}
}).css({
"top": element_rect.top + azh.window.scrollTop() - 5,
"left": element_rect.left,
"height": 10,
"width": element_rect.width
});
$element.data('azh-height-top-resizer', $resizer);
}
}
function height_bottom_resizer($element) {
if (!$element.data('azh-height-bottom-resizer')) {
var element_rect = $element.get(0).getBoundingClientRect();
var $resizer = $('').appendTo(azh.controls_container).on('mousedown', function (event) {
var $resizer = $(this);
$resizer.data('azh-drag', true);
$resizer.data('azh-drag-y', event.pageY);
$resizer.data('azh-start-drag-y', event.pageY);
$resizer.css('top', element_rect.top + azh.window.scrollTop() + element_rect.height - 5);
if ($element.get(0).style['height'] == '') {
set_height($element, $element.height());
}
azh.document_off('mouseup.resizer').document_on('mouseup.resizer', function (e) {
if ($resizer.data('azh-start-drag-y') === e.pageY) {
$element.css('height', '');
set_stored_style($element, 'height', '');
}
$resizer.data('azh-drag', false);
if (!$resizer.is(':hover')) {
azh.document_off('mouseup.resizer');
}
});
azh.document_off('mousemove.resizer').document_on('mousemove.resizer', function (e) {
if ($resizer.data('azh-drag')) {
var d = e.pageY - $resizer.data('azh-drag-y');
var $zoom = $element.closest('.azh-zoom');
if ($zoom.length) {
d = d / $zoom.data('azh-scale');
}
var v = (parseFloat(get_stored_style($element, 'height')) + d);
v = isNaN(v) ? 0 : v;
if (v < 0) {
v = 0;
}
set_height($element, v);
$resizer.data('azh-drag-y', e.pageY);
element_rect = $element.get(0).getBoundingClientRect();
$resizer.css('top', element_rect.top + azh.window.scrollTop() + element_rect.height - 5);
}
});
event.stopPropagation();
event.preventDefault();
return false;
}).on('mouseleave', function (event) {
var $resizer = $(this);
if (!$resizer.data('azh-drag') && !($element.is(':hover') || $element.is('.azh-hover'))) {
$element.data('azh-height-bottom-resizer', false);
$resizer.remove();
$element.trigger('mouseleave');
}
}).css({
"top": element_rect.top + azh.window.scrollTop() + element_rect.height - 5,
"left": element_rect.left,
"height": 10,
"width": element_rect.width
});
$element.data('azh-height-bottom-resizer', $resizer);
}
}
$element.on('mouseenter', function (event) {
if (event.which === 0) {
var $element = azh.$(this);
azh.$(event.target).parents().each(function () {
if (azh.$(this).css('position') === 'fixed') {
event.stopPropagation();
return false;
}
});
setTimeout(function () {
// if ($element.css('position') === 'absolute') {
// if ($element.is('.az-left, .az-center')) {
// width_right_resizer($element);
// }
// if ($element.is('.az-right')) {
// width_left_resizer($element);
// }
// if ($element.is('.az-top, .az-middle')) {
// height_bottom_resizer($element);
// }
// if ($element.is('.az-bottom')) {
// height_top_resizer($element);
// }
// }
if (!$element.parents('.az-free-positioning').length) {
$element.find('> .az-free-positioning').each(function () {
// width_right_resizer(azh.$(this));
// height_top_resizer(azh.$(this));
height_bottom_resizer(azh.$(this));
});
}
});
}
}).on('mouseleave', function () {
var $element = azh.$(this);
setTimeout(function () {
$(['azh-width-right-resizer', 'azh-width-left-resizer', 'azh-height-top-resizer', 'azh-height-bottom-resizer']).each(function () {
var mode = this;
if ($element.data(mode)) {
if (!($element.data(mode).is(':hover') || $element.is('.azh-hover')) && !$element.data(mode).data('azh-drag')) {
$element.data(mode).remove();
$element.data(mode, false);
}
}
$element.find('> .az-free-positioning').each(function () {
var $this = azh.$(this);
if ($this.data(mode)) {
if (!($this.data(mode).is(':hover') || $element.is('.azh-hover')) && !$this.data(mode).data('azh-drag')) {
$this.data(mode).remove();
$this.data(mode, false);
}
}
});
});
});
});
}
function start_free_transform($element) {
var $element_wrapper = $element.closest('.az-element-wrapper');
$element_wrapper.data('width', isNaN(parseInt($element_wrapper.css('width'), 10)) ? 0 : parseInt($element_wrapper.css('width'), 10));
$element_wrapper.data('height', isNaN(parseInt($element_wrapper.css('height'), 10)) ? 0 : parseInt($element_wrapper.css('height'), 10));
if ($element_wrapper.parent('.az-free-positioning').length || $element_wrapper.parent('.az-elements-list').parent('.az-free-positioning').length || $element_wrapper.css('position') === 'absolute') {
if ($element_wrapper.is('.az-left')) {
$element_wrapper.data('left', isNaN(parseInt($element_wrapper.css('left'), 10)) ? 0 : parseInt($element_wrapper.css('left'), 10));
}
if ($element_wrapper.is('.az-center')) {
var left = /calc\(50% ([-+]) ([\d\.]+)px\)/gi.exec($element_wrapper.get(0).style.left ? $element_wrapper.get(0).style.left : '');
if (left) {
$element_wrapper.data('center', parseInt(left[1] + left[2], 10));
} else {
$element_wrapper.data('center', 0);
}
}
if ($element_wrapper.is('.az-right')) {
$element_wrapper.data('right', isNaN(parseInt($element_wrapper.css('right'), 10)) ? 0 : parseInt($element_wrapper.css('right'), 10));
}
if ($element_wrapper.is('.az-top')) {
$element_wrapper.data('top', isNaN(parseInt($element_wrapper.css('top'), 10)) ? 0 : parseInt($element_wrapper.css('top'), 10));
}
if ($element_wrapper.is('.az-middle')) {
var top = /calc\(50% ([-+]) ([\d\.]+)px\)/gi.exec($element_wrapper.get(0).style.top ? $element_wrapper.get(0).style.top : '');
if (top) {
$element_wrapper.data('middle', parseInt(top[1] + top[2], 10));
} else {
$element_wrapper.data('middle', 0);
}
}
if ($element_wrapper.is('.az-bottom')) {
$element_wrapper.data('bottom', isNaN(parseInt($element_wrapper.css('bottom'), 10)) ? 0 : parseInt($element_wrapper.css('bottom'), 10));
}
} else {
$element_wrapper.data('top', isNaN(parseInt($element_wrapper.css('top'), 10)) ? 0 : parseInt($element_wrapper.css('top'), 10));
$element_wrapper.data('left', isNaN(parseInt($element_wrapper.css('left'), 10)) ? 0 : parseInt($element_wrapper.css('left'), 10));
}
}
function show_guides($free_position, rect, $exclude) {
function get_possible_shift(rect, element_rect) {
var possible_shift = {
x: false,
y: false,
};
if (Math.abs(rect.left - element_rect.left) <= epsilon) {
possible_shift.side = 'left';
possible_shift.x = Math.round(element_rect.left - rect.left);
}
if (Math.abs(rect.left - element_rect.right) <= epsilon) {
possible_shift.side = 'right';
possible_shift.x = Math.round(element_rect.right - rect.left);
}
if (Math.abs(rect.right - element_rect.left) <= epsilon) {
possible_shift.side = 'left';
possible_shift.x = Math.round(element_rect.left - rect.right);
}
if (Math.abs(rect.right - element_rect.right) <= epsilon) {
possible_shift.side = 'right';
possible_shift.x = Math.round(element_rect.right - rect.right);
}
if (Math.abs(rect.top - element_rect.top) <= epsilon) {
possible_shift.side = 'top';
possible_shift.y = Math.round(element_rect.top - rect.top);
}
if (Math.abs(rect.top - element_rect.bottom) <= epsilon) {
possible_shift.side = 'bottom';
possible_shift.y = Math.round(element_rect.bottom - rect.top);
}
if (Math.abs(rect.bottom - element_rect.top) <= epsilon) {
possible_shift.side = 'top';
possible_shift.y = Math.round(element_rect.top - rect.bottom);
}
if (Math.abs(rect.bottom - element_rect.bottom) <= epsilon) {
possible_shift.side = 'bottom';
possible_shift.y = Math.round(element_rect.bottom - rect.bottom);
}
var center_x = Math.round(rect.left + rect.width / 2);
var center_y = Math.round(rect.top + rect.height / 2);
var element_center_x = Math.round(element_rect.left + element_rect.width / 2);
var element_center_y = Math.round(element_rect.top + element_rect.height / 2);
if (Math.abs(center_x - element_center_x) <= epsilon) {
possible_shift.side = 'center_x';
possible_shift.x = element_center_x - center_x;
}
if (Math.abs(center_y - element_center_y) <= epsilon) {
possible_shift.side = 'center_y';
possible_shift.y = element_center_y - center_y;
}
return possible_shift;
}
azh.document_off('mouseup.guides').document_on('mouseup.guides', function (e) {
hide_guides();
});
var epsilon = 5;
var $elements = closest_descendents($free_position, '[data-element]');
$elements = $elements.add($free_position);
if ($exclude) {
$elements = $elements.not($exclude);
}
var shift = {
x: {},
y: {},
};
$elements.each(function () {
var $element = azh.$(this);
var element_rect = $element.get(0).getBoundingClientRect();
var possible_shift = get_possible_shift(rect, element_rect);
if (possible_shift.x !== false) {
if (!(possible_shift.x in shift.x)) {
shift.x[possible_shift.x] = 0;
}
shift.x[possible_shift.x]++;
}
if (possible_shift.y !== false) {
if (!(possible_shift.y in shift.y)) {
shift.y[possible_shift.y] = 0;
}
shift.y[possible_shift.y]++;
}
});
var max = 0;
var max_s = false;
for (var s in shift.x) {
if (shift.x[s] > max) {
max = shift.x[s];
max_s = s;
}
}
shift.x = max_s ? parseInt(max_s, 10) : 0;
var max = 0;
var max_s = false;
for (var s in shift.y) {
if (shift.y[s] > max) {
max = shift.y[s];
max_s = s;
}
}
shift.y = max_s ? parseInt(max_s, 10) : 0;
$elements.each(function () {
var $element = azh.$(this);
var element_rect = $element.get(0).getBoundingClientRect();
var possible_shift = get_possible_shift(rect, element_rect);
if (possible_shift.x !== false && shift.x === possible_shift.x || possible_shift.y !== false && shift.y === possible_shift.y) {
var element_rect = $element.get(0).getBoundingClientRect();
var $guide = $('').appendTo(azh.controls_container);
$guide.css({
left: element_rect.left,
top: element_rect.top + azh.window.scrollTop(),
width: element_rect.width,
height: element_rect.height
});
var $guide_line = $('').appendTo(azh.controls_container);
switch (possible_shift.side) {
case 'left':
$guide_line.css({
top: (element_rect.top > rect.top) ? rect.top + azh.window.scrollTop() : element_rect.top + azh.window.scrollTop(),
left: element_rect.left,
height: (element_rect.top > rect.top) ? element_rect.top - rect.top + element_rect.height : rect.top - element_rect.top + rect.height,
width: '1px'
});
break;
case 'right':
$guide_line.css({
top: (element_rect.top > rect.top) ? rect.top + azh.window.scrollTop() : element_rect.top + azh.window.scrollTop(),
left: element_rect.right,
height: (element_rect.top > rect.top) ? element_rect.top - rect.top + element_rect.height : rect.top - element_rect.top + rect.height,
width: '1px'
});
break;
case 'top':
$guide_line.css({
left: (element_rect.left > rect.left) ? rect.left : element_rect.left,
top: element_rect.top + azh.window.scrollTop(),
width: (element_rect.left > rect.left) ? element_rect.left - rect.left + element_rect.width : rect.left - element_rect.left + rect.width,
height: '1px'
});
break;
case 'bottom':
$guide_line.css({
left: (element_rect.left > rect.left) ? rect.left : element_rect.left,
top: element_rect.bottom + azh.window.scrollTop(),
width: (element_rect.left > rect.left) ? element_rect.left - rect.left + element_rect.width : rect.left - element_rect.left + rect.width,
height: '1px'
});
break;
case 'center_x':
var center_x = Math.round(rect.left + rect.width / 2);
var center_y = Math.round(rect.top + rect.height / 2) + azh.window.scrollTop();
var element_center_x = Math.round(element_rect.left + element_rect.width / 2);
var element_center_y = Math.round(element_rect.top + element_rect.height / 2) + azh.window.scrollTop();
if (center_y > element_center_y) {
$guide_line.css({
top: element_center_y + 'px',
height: (center_y - element_center_y) + 'px',
left: element_center_x + 'px',
width: '1px',
});
} else {
$guide_line.css({
top: center_y + 'px',
height: (element_center_y - center_y) + 'px',
left: element_center_x + 'px',
width: '1px',
});
}
break;
case 'center_y':
var center_x = Math.round(rect.left + rect.width / 2);
var center_y = Math.round(rect.top + rect.height / 2) + azh.window.scrollTop();
var element_center_x = Math.round(element_rect.left + element_rect.width / 2);
var element_center_y = Math.round(element_rect.top + element_rect.height / 2) + azh.window.scrollTop();
if (center_x > element_center_x) {
$guide_line.css({
top: element_center_y + 'px',
height: '1px',
left: element_center_x + 'px',
width: (center_x - element_center_x) + 'px',
});
} else {
$guide_line.css({
top: element_center_y + 'px',
height: '1px',
left: center_x + 'px',
width: (element_center_x - center_x) + 'px',
});
}
break;
}
}
});
if ($free_position.is('.azh-zoom')) {
var scale = $free_position.data('azh-scale');
if (shift.x !== false) {
shift.x = shift.x / scale;
}
if (shift.y !== false) {
shift.y = shift.y / scale;
}
}
return shift;
}
function hide_guides() {
azh.controls_container.find('.azh-guide, .azh-guide-line').remove();
}
function transform_translate($element_wrapper, delta_x, delta_y) {
if ($element_wrapper.is('.az-left')) {
set_left($element_wrapper, $element_wrapper.data('left') + delta_x);
}
if ($element_wrapper.is('.az-center')) {
set_center($element_wrapper, $element_wrapper.data('center') + delta_x);
}
if ($element_wrapper.is('.az-right')) {
set_right($element_wrapper, $element_wrapper.data('right') - delta_x);
}
if ($element_wrapper.is('.az-top')) {
set_top($element_wrapper, $element_wrapper.data('top') + delta_y);
}
if ($element_wrapper.is('.az-middle')) {
set_middle($element_wrapper, $element_wrapper.data('middle') + delta_y);
}
if ($element_wrapper.is('.az-bottom')) {
set_bottom($element_wrapper, $element_wrapper.data('bottom') - delta_y);
}
}
function refresh_transformer($element) {
if ($element.data('azh-transformer')) {
var $transformer = $element.data('azh-transformer');
var element_rect = $element.get(0).getBoundingClientRect();
$transformer.css({
left: element_rect.left,
top: element_rect.top + azh.window.scrollTop(),
width: element_rect.width,
height: element_rect.height
});
}
}
function enable_free_transform($element) {
$element.on('drag dragstart dragenter dragover dragleave drop dragend', function (e) {
e.stopPropagation();
return false;
});
azh.document_off('mousemove.transform').document_on('mousemove.transform', function (e) {
if (e.which === 1) {
var transform = false;
azh.$('.azh-transform').each(function () {
transform = true;
var $element = azh.$(this);
var delta_y = e.pageY - $element.data('pageY');
var delta_x = e.pageX - $element.data('pageX');
var $element_wrapper = $element.closest('.az-element-wrapper');
var $controls = $element.data('azh-controls');
set_element_controls_position($element_wrapper, $controls);
var $zoom = $element_wrapper.closest('.azh-zoom');
if ($zoom.length) {
delta_y = delta_y / $zoom.data('azh-scale');
delta_x = delta_x / $zoom.data('azh-scale');
}
if ($element_wrapper.parent('.az-free-positioning').length || $element_wrapper.parent('.az-elements-list').parent('.az-free-positioning').length || $element_wrapper.css('position') === 'absolute') {
var $free_position = $element_wrapper.closest('.az-free-positioning');
hide_guides();
if ($element.is('.azh-left-resize')) {
if ($element_wrapper.is('.az-left')) {
set_left($element_wrapper, $element_wrapper.data('left') + delta_x);
}
if ($element_wrapper.is('.az-center')) {
set_center($element_wrapper, $element_wrapper.data('center') + delta_x / 2);
}
if ($element_wrapper.is('.az-right')) {
}
set_width($element_wrapper, $element_wrapper.data('width') - delta_x);
var shift = show_guides($free_position, $element_wrapper.get(0).getBoundingClientRect(), $element_wrapper);
delta_x = delta_x + shift.x;
delta_y = delta_y + shift.y;
if ($element_wrapper.is('.az-left')) {
set_left($element_wrapper, $element_wrapper.data('left') + delta_x);
}
if ($element_wrapper.is('.az-center')) {
set_center($element_wrapper, $element_wrapper.data('center') + delta_x / 2);
}
if ($element_wrapper.is('.az-right')) {
}
set_width($element_wrapper, $element_wrapper.data('width') - delta_x);
}
if ($element.is('.azh-right-resize')) {
if ($element_wrapper.is('.az-left')) {
}
if ($element_wrapper.is('.az-center')) {
set_center($element_wrapper, $element_wrapper.data('center') + delta_x / 2);
}
if ($element_wrapper.is('.az-right')) {
set_right($element_wrapper, $element_wrapper.data('right') - delta_x);
}
set_width($element_wrapper, $element_wrapper.data('width') + delta_x);
var shift = show_guides($free_position, $element_wrapper.get(0).getBoundingClientRect(), $element_wrapper);
delta_x = delta_x + shift.x;
delta_y = delta_y + shift.y;
if ($element_wrapper.is('.az-left')) {
}
if ($element_wrapper.is('.az-center')) {
set_center($element_wrapper, $element_wrapper.data('center') + delta_x / 2);
}
if ($element_wrapper.is('.az-right')) {
set_right($element_wrapper, $element_wrapper.data('right') - delta_x);
}
set_width($element_wrapper, $element_wrapper.data('width') + delta_x);
}
if ($element.is('.azh-top-resize')) {
if ($element_wrapper.is('.az-top')) {
set_top($element_wrapper, $element_wrapper.data('top') + delta_y);
}
if ($element_wrapper.is('.az-middle')) {
set_middle($element_wrapper, $element_wrapper.data('middle') + delta_y / 2);
}
if ($element_wrapper.is('.az-bottom')) {
}
set_height($element_wrapper, $element_wrapper.data('height') - delta_y);
var shift = show_guides($free_position, $element_wrapper.get(0).getBoundingClientRect(), $element_wrapper);
delta_x = delta_x + shift.x;
delta_y = delta_y + shift.y;
if ($element_wrapper.is('.az-top')) {
set_top($element_wrapper, $element_wrapper.data('top') + delta_y);
}
if ($element_wrapper.is('.az-middle')) {
set_middle($element_wrapper, $element_wrapper.data('middle') + delta_y / 2);
}
if ($element_wrapper.is('.az-bottom')) {
}
set_height($element_wrapper, $element_wrapper.data('height') - delta_y);
}
if ($element.is('.azh-bottom-resize')) {
if ($element_wrapper.is('.az-top')) {
}
if ($element_wrapper.is('.az-middle')) {
set_middle($element_wrapper, $element_wrapper.data('middle') + delta_y / 2);
}
if ($element_wrapper.is('.az-bottom')) {
set_bottom($element_wrapper, $element_wrapper.data('bottom') - delta_y);
}
set_height($element_wrapper, $element_wrapper.data('height') + delta_y);
var shift = show_guides($free_position, $element_wrapper.get(0).getBoundingClientRect(), $element_wrapper);
delta_x = delta_x + shift.x;
delta_y = delta_y + shift.y;
if ($element_wrapper.is('.az-top')) {
}
if ($element_wrapper.is('.az-middle')) {
set_middle($element_wrapper, $element_wrapper.data('middle') + delta_y / 2);
}
if ($element_wrapper.is('.az-bottom')) {
set_bottom($element_wrapper, $element_wrapper.data('bottom') - delta_y);
}
set_height($element_wrapper, $element_wrapper.data('height') + delta_y);
}
if (!$element.is('.azh-left-resize') && !$element.is('.azh-right-resize') && !$element.is('.azh-top-resize') && !$element.is('.azh-bottom-resize')) {
transform_translate($element_wrapper, delta_x, delta_y);
var shift = show_guides($free_position, $element_wrapper.get(0).getBoundingClientRect(), $element_wrapper);
transform_translate($element_wrapper, delta_x + shift.x, delta_y + shift.y);
}
} else {
if ($element_wrapper.css('position') === 'static') {
$element_wrapper.css('position', 'relative');
set_stored_style($element_wrapper, 'position', 'relative');
$element_wrapper.css('z-index', '1');
set_stored_style($element_wrapper, 'z-index', '1');
set_width($element_wrapper, $element_wrapper.width());
}
set_top($element_wrapper, $element_wrapper.data('top') + delta_y);
set_left($element_wrapper, $element_wrapper.data('left') + delta_x);
}
refresh_transformer($element);
});
if (transform) {
return false;
}
}
});
azh.document_off('mouseup.transform').document_on('mouseup.transform', function (e) {
var transform = false;
azh.$('.azh-transform').each(function () {
transform = true;
var $element = azh.$(this);
var $element_wrapper = $element.closest('.az-element-wrapper');
$element.removeClass('azh-transform azh-left-resize azh-right-resize azh-top-resize azh-bottom-resize');
if ($element.data('pageX') == e.pageX && $element.data('pageY') == e.pageY) {
if ($element.data('azh-transformer')) {
if ($element.children().length) {
$element.data('azh-transformer').addClass('azh-editing');
$element.addClass('azh-editing');
}
setTimeout(function () {
$element.trigger('click');
var $element_controls = $element.data('azh-controls');
if ($element_controls) {
$element_controls.find('.azh-utility-wrapper').trigger('click');
}
azh.document.trigger('click.azh-polygon');
});
}
}
});
if (transform) {
return false;
}
});
$element.on('click', function (e) {
var $element = azh.$(this);
}).on('mouseenter', function (e) {
var $element = azh.$(this);
if (!$element.parents('.az-group').length && !$element.data('azh-transformer') && !$element.closest('.az-free-positioning.azh-lasso').length) {
var $polygon = $element.find('.az-polygone');
if ($polygon && $polygon.data('azh-polygone-handlers')) {
return;
}
$element.removeClass('azh-editing');
var $transformer = $('').appendTo(azh.controls_container).on('mousemove', function (event) {
var $this = $(this);
if (!$this.is('.azh-editing')) {
var $element = $this.data('azh-linked-element');
if (!$element.is('.azh-transform')) {
$this.css('pointer-events', 'none');
var $target = azh.$(azh.document.get(0).elementFromPoint(event.clientX - azh.device_left, event.clientY));
if ($element.has($target).length || $element.is($target)) {
$this.css('pointer-events', '');
} else {
$element.data('azh-transformer', false);
$this.remove();
$element.triggerHandler(azh.$.Event('mouseleave', {which: 0}));
}
}
}
}).on('contextmenu', function (event) {
event.preventDefault();
var $element = $(this).data('azh-linked-element');
open_context_menu(event, azh.$(azh.document.get(0).elementFromPoint(event.clientX - azh.device_left, event.clientY)));
setTimeout(function () {
var $element_controls = $element.data('azh-controls');
if ($element_controls) {
$element_controls.find('.azh-utility-wrapper').trigger('click');
}
});
}).on('mouseleave', function (event) {
var $this = $(this);
if (!$this.is('.azh-editing')) {
var $element = $this.data('azh-linked-element');
$element.data('azh-transformer', false);
$this.remove();
$element.removeClass('azh-editing');
$element.triggerHandler(azh.$.Event('mouseleave', {which: 0}));
}
}).on('mousedown', function (e) {
var $transformer = $(this);
var $element = $transformer.data('azh-linked-element');
if ($element.is('.az-group') || !$element.parents('.az-group').length) {
$element.addClass('azh-transform');
$element.data('pageX', e.pageX);
$element.data('pageY', e.pageY);
start_free_transform($element);
}
});
$('').appendTo($transformer).on('mousedown', function (e) {
var $transformer = $(this).parent();
var $element = $transformer.data('azh-linked-element');
$element.addClass('azh-transform azh-left-resize');
$element.data('pageX', e.pageX);
$element.data('pageY', e.pageY);
start_free_transform($element);
});
$('').appendTo($transformer).on('mousedown', function (e) {
var $transformer = $(this).parent();
var $element = $transformer.data('azh-linked-element');
$element.addClass('azh-transform azh-right-resize');
$element.data('pageX', e.pageX);
$element.data('pageY', e.pageY);
start_free_transform($element);
});
$('').appendTo($transformer).on('mousedown', function (e) {
var $transformer = $(this).parent();
var $element = $transformer.data('azh-linked-element');
$element.addClass('azh-transform azh-top-resize');
$element.data('pageX', e.pageX);
$element.data('pageY', e.pageY);
start_free_transform($element);
});
$('').appendTo($transformer).on('mousedown', function (e) {
var $transformer = $(this).parent();
var $element = $transformer.data('azh-linked-element');
$element.addClass('azh-transform azh-bottom-resize');
$element.data('pageX', e.pageX);
$element.data('pageY', e.pageY);
start_free_transform($element);
});
$transformer.data('azh-linked-element', $element);
$element.data('azh-transformer', $transformer);
}
refresh_transformer($element);
}).on('mouseleave', function (e) {
var $element = azh.$(this);
if ($element.data('azh-transformer') && !$element.data('azh-transformer').is(':hover')) {
$element.data('azh-transformer').remove();
$element.data('azh-transformer', false);
$element.removeClass('azh-editing');
}
})
}
function add_unmerge_button($group) {
var $element_controls = $group.data('azh-controls');
if (!$element_controls.find('.azh-unmerge').length) {
$('').on('click', function (event) {
var $this = $(this);
var $element_controls = $this.closest('.azh-element-controls');
var $group = $element_controls.data('azh-linked-element');
var $elements = closest_descendents($group, '[data-element]');
$elements.detach();
$elements.insertAfter($group);
var $free_position = $group.closest('.az-free-positioning').closest('[data-element]');
change_h_alignment($group, 'left');
change_v_alignment($group, 'top');
var delta_x = get_left($group);
var delta_y = get_top($group);
var delta_width = $group.width() - $free_position.width();
var delta_height = $group.height() - $free_position.height();
$elements.each(function () {
var $element = azh.$(this);
if ($element.is('.az-left')) {
set_left($element, get_left($element) + delta_x);
}
if ($element.is('.az-center')) {
set_center($element, get_center($element) + delta_x + delta_width / 2);
}
if ($element.is('.az-right')) {
set_right($element, get_right($element) - delta_x - delta_width);
}
if ($element.is('.az-top')) {
set_top($element, get_top($element) + delta_y);
}
if ($element.is('.az-middle')) {
set_middle($element, get_middle($element) + delta_y + delta_height / 2);
}
if ($element.is('.az-bottom')) {
set_bottom($element, get_bottom($element) - delta_y - delta_height);
}
});
if ($element_controls.find('.azh-remove-element').length) {
$element_controls.find('.azh-remove-element').click();
}
_.defer(refresh_elements_hierarchy_partial, $free_position);
}).appendTo($element_controls);
}
}
function remove_buttons($elements, selector) {
$elements.each(function () {
var $element = azh.$(this);
var $element_controls = $element.data('azh-controls');
$element_controls.find(selector).remove();
});
}
function refresh_lasso($free_position, $elements) {
if ($free_position.data('azh-lasso')) {
var lasso_rect = {
left: Number.POSITIVE_INFINITY,
top: Number.POSITIVE_INFINITY,
width: 0,
height: 0,
};
$elements.each(function () {
var $element = azh.$(this);
var rect = this.getBoundingClientRect();
var element_rect = {
left: rect.left,
top: rect.top,
width: rect.width,
height: rect.height
};
if (lasso_rect.left > element_rect.left) {
if (lasso_rect.left !== Number.POSITIVE_INFINITY) {
lasso_rect.width = lasso_rect.width + (lasso_rect.left - element_rect.left);
}
lasso_rect.left = element_rect.left;
}
if (lasso_rect.top > element_rect.top) {
if (lasso_rect.top !== Number.POSITIVE_INFINITY) {
lasso_rect.height = lasso_rect.height + (lasso_rect.top - element_rect.top);
}
lasso_rect.top = element_rect.top;
}
if ((lasso_rect.left + lasso_rect.width) < (element_rect.left + element_rect.width)) {
lasso_rect.width = element_rect.left + element_rect.width - lasso_rect.left;
}
if ((lasso_rect.top + lasso_rect.height) < (element_rect.top + element_rect.height)) {
lasso_rect.height = element_rect.top + element_rect.height - lasso_rect.top;
}
});
var $lasso = $free_position.data('azh-lasso');
lasso_rect.top = lasso_rect.top + azh.window.scrollTop();
$lasso.css(lasso_rect).off('mousedown').on('mousedown', function (e) {
$free_position.addClass('azh-lasso-transform');
$free_position.data('pageX', e.pageX);
$free_position.data('pageY', e.pageY);
$elements.each(function () {
var $element = azh.$(this);
// $element.addClass('azh-transform');
// $element.data('pageX', e.pageX);
// $element.data('pageY', e.pageY);
start_free_transform($element);
});
});
}
}
function refresh_selections($elements) {
$elements.each(function () {
refresh_selection(azh.$(this));
});
}
function refresh_selection($element) {
if ($element.data('azh-selection')) {
var $selection = $element.data('azh-selection');
var element_rect = $element.get(0).getBoundingClientRect();
$selection.css({
left: element_rect.left,
top: element_rect.top + azh.window.scrollTop(),
width: element_rect.width,
height: element_rect.height
});
}
}
function enable_lasso($free_position) {
azh.document_off('mousemove.lasso').document_on('mousemove.lasso', function (e) {
if (e.which === 1) {
var lasso = false;
azh.$('.azh-lasso').each(function () {
lasso = true;
var $free_position = azh.$(this);
var delta_y = e.pageY - $free_position.data('pageY') + azh.window.scrollTop();
var delta_x = e.pageX - $free_position.data('pageX') - azh.device_left;
// if ($free_position.is('.azh-zoom')) {
// delta_y = delta_y / $free_position.data('azh-scale');
// delta_x = delta_x / $free_position.data('azh-scale');
// }
var $lasso = $free_position.data('azh-lasso');
$lasso.css({
left: $free_position.data('pageX'),
top: $free_position.data('pageY'),
width: delta_x,
height: delta_y,
});
var $elements = closest_descendents($free_position, '[data-element]');
var lasso_rect = $lasso.get(0).getBoundingClientRect();
$elements.each(function () {
function intersected(r1, r2) {
return !(r2.left > r1.right || r2.right < r1.left || r2.top > r1.bottom || r2.bottom < r1.top);
}
var $element = azh.$(this);
var rect = this.getBoundingClientRect();
var element_rect = {
left: rect.left + azh.device_left,
right: rect.right + azh.device_left,
top: rect.top,
bottom: rect.bottom
};
if (intersected(lasso_rect, element_rect)) {
if (!$element.data('azh-selection')) {
var $selection = $('').appendTo(azh.controls_container);
$selection.data('azh-linked-element', $element);
$element.data('azh-selection', $selection);
$element.addClass('azh-selected');
}
} else {
if ($element.data('azh-selection')) {
$element.data('azh-selection').remove();
$element.data('azh-selection', false);
$element.removeClass('azh-selected');
}
}
refresh_selection($element);
});
});
azh.$('.azh-lasso-transform').each(function () {
hide_guides();
lasso = true;
var $free_position = azh.$(this);
var $elements = closest_descendents($free_position, '[data-element]').filter('.azh-selected');
if ($elements.length) {
var delta_y = e.pageY - $free_position.data('pageY');
var delta_x = e.pageX - $free_position.data('pageX');
$elements.each(function () {
transform_translate(azh.$(this), delta_x, delta_y);
});
refresh_lasso($free_position, $elements);
var rect = $free_position.data('azh-lasso').get(0).getBoundingClientRect();
var scale = 1;
if ($free_position.is('.azh-zoom')) {
scale = $free_position.data('azh-scale');
}
var lasso_rect = {
left: rect.left - azh.device_left * scale,
right: rect.right - azh.device_left * scale,
top: rect.top,
bottom: rect.bottom,
width: rect.width,
height: rect.height
};
var shift = show_guides($free_position, lasso_rect, $elements);
$elements.each(function () {
transform_translate(azh.$(this), delta_x + shift.x, delta_y + shift.y);
});
refresh_lasso($free_position, $elements);
$elements.each(function () {
refresh_selection(azh.$(this));
});
}
});
if (lasso) {
return false;
}
}
});
azh.document_off('mouseup.lasso').document_on('mouseup.lasso', function (e) {
var lasso = false;
azh.$('.azh-lasso').each(function (e) {
lasso = true;
var $free_position = azh.$(this);
$free_position.removeClass('azh-lasso');
var $elements = closest_descendents($free_position, '[data-element]').filter('.azh-selected');
if ($elements.length) {
refresh_lasso($free_position, $elements);
var $lasso = $free_position.data('azh-lasso');
var $operations = $('').appendTo($lasso);
var $alignment = $('').appendTo($operations);
var $e_alignment = $('').appendTo($alignment);
$('').appendTo($e_alignment).on('click', function (event) {
if ($elements.length > 1) {
v_align($elements, 'top');
} else {
reset_free_positioning($elements);
set_top_alignment($elements);
}
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($elements.length > 1) {
v_align($elements, 'middle');
} else {
reset_free_positioning($elements);
set_middle_alignment($elements);
}
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($elements.length > 1) {
v_align($elements, 'bottom');
} else {
reset_free_positioning($elements);
set_bottom_alignment($elements);
}
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($elements.length > 1) {
h_align($elements, 'left');
} else {
reset_free_positioning($elements);
set_left_alignment($elements);
}
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($elements.length > 1) {
h_align($elements, 'center');
} else {
reset_free_positioning($elements);
set_center_alignment($elements);
}
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($elements.length > 1) {
h_align($elements, 'right');
} else {
reset_free_positioning($elements);
set_right_alignment($elements);
}
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
if ($elements.length > 2) {
var $g_alignment = $('').appendTo($alignment);
$('').appendTo($g_alignment).on('click', function (event) {
between_v_align($elements, 'top');
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($g_alignment).on('click', function (event) {
between_v_align($elements, 'middle');
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($g_alignment).on('click', function (event) {
between_v_align($elements, 'bottom');
refresh_selections($elements);
});
$('').appendTo($g_alignment).on('click', function (event) {
between_h_align($elements, 'left');
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($g_alignment).on('click', function (event) {
between_h_align($elements, 'center');
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
$('').appendTo($g_alignment).on('click', function (event) {
between_h_align($elements, 'right');
refresh_lasso($free_position, $elements);
refresh_selections($elements);
});
}
if ($elements.length > 1) {
$('').appendTo($operations).on('click', function (event) {
var free_position_rect = $free_position.get(0).getBoundingClientRect();
var lasso_rect = $lasso.get(0).getBoundingClientRect();
$free_position.trigger('mousedown');
$free_position.trigger('mouseup');
var $cloneable = $elements.parent();
var $element_wrapper = $elements.first();
var $group = clone_for_new_element($element_wrapper);
$group.removeClass('azh-selected');
$group.addClass('az-group');
add_to_stored_classes($group, 'az-group');
$group.data('azh-controls').addClass('az-fixed-controls');
var $button = get_remove_element_button($group.data('azh-controls'));
$button.data('azh-linked-element', $group);
$button = get_add_element_button($group.data('azh-controls'), true);
$button.data('azh-linked-element', $group);
$button = get_add_element_button($group.data('azh-controls'), false);
$button.data('azh-linked-element', $group);
$button = get_copy_element_button($group.data('azh-controls'));
$button.data('azh-linked-element', $group);
$group.attr('data-element', 'free-positioning/free-positioning.htm');
set_stored_attribute($group, 'data-element', 'free-positioning/free-positioning.htm');
$group.html('
');
store_html($group.get(0));
$elements.detach();
customization_init($group);
$group.find('.az-elements-list').append($elements);
var s = 1;
if ($free_position.is('.azh-zoom')) {
s = $free_position.data('azh-scale');
}
var delta_x = (lasso_rect.left - free_position_rect.left) / s - azh.device_left / s;
var delta_y = (lasso_rect.top - free_position_rect.top) / s;
var delta_width = (lasso_rect.width - free_position_rect.width) / s;
var delta_height = (lasso_rect.height - free_position_rect.height) / s;
reset_free_positioning($group);
set_top_alignment($group);
set_left_alignment($group);
set_top($group, delta_y);
set_left($group, delta_x);
set_width($group, lasso_rect.width / s);
set_height($group, lasso_rect.height / s);
$elements.each(function () {
var $element = azh.$(this);
if ($element.is('.az-left')) {
set_left($element, get_left($element) - delta_x);
}
if ($element.is('.az-center')) {
set_center($element, get_center($element) - delta_x - delta_width / 2);
}
if ($element.is('.az-right')) {
set_right($element, get_right($element) + delta_x + delta_width);
}
if ($element.is('.az-top')) {
set_top($element, get_top($element) - delta_y);
}
if ($element.is('.az-middle')) {
set_middle($element, get_middle($element) - delta_y - delta_height / 2);
}
if ($element.is('.az-bottom')) {
set_bottom($element, get_bottom($element) + delta_y + delta_height);
}
});
setTimeout(function () {
$group.trigger('click');
var $element_controls = $group.data('azh-controls');
if ($element_controls) {
$element_controls.find('.azh-utility-wrapper').trigger('click');
}
});
_.defer(refresh_elements_hierarchy_partial, $free_position);
remove_buttons($elements, '.azh-add-element');
add_unmerge_button($group);
});
}
} else {
$free_position.data('azh-lasso').remove();
$free_position.data('azh-lasso', false);
}
});
azh.$('.azh-lasso-transform').each(function () {
lasso = true;
var $free_position = azh.$(this);
$free_position.removeClass('azh-lasso-transform');
});
if (lasso) {
return false;
}
});
$free_position.on('click', function (e) {
var $free_position = azh.$(this);
}).on('mouseenter', function (e) {
var $free_position = azh.$(this);
}).on('mouseleave', function (e) {
var $free_position = azh.$(this);
}).on('mousedown', function (e) {
var $free_position = azh.$(this);
if (!$free_position.parents('.az-free-positioning').length && !$free_position.find('.azh-transform').length && !$free_position.find('.azh-editing').length && ($free_position.is(e.target) || !azh.$(e.target).parentsUntil($free_position).filter('[draggable]').length)) {
closest_descendents($free_position, '[data-element]').filter('.azh-selected').each(function () {
var $element = azh.$(this);
$element.data('azh-selection').remove();
$element.data('azh-selection', false);
$element.removeClass('azh-selected');
});
$free_position.addClass('azh-lasso');
$free_position.data('pageX', e.pageX);
$free_position.data('pageY', e.pageY);
var $lasso = $free_position.data('azh-lasso');
if (!$lasso) {
$lasso = $('').appendTo(azh.controls_container);
$lasso.data('azh-linked-element', $free_position);
$free_position.data('azh-lasso', $lasso);
}
$lasso.empty();
$lasso.css({
left: $free_position.data('pageX'),
top: $free_position.data('pageY'),
width: 0,
height: 0,
});
return false;
}
});
}
function has_stored_css_property($element, property) {
var property_pattern = new RegExp('(^' + property + '|[ \\"\\\'\\;]' + property + ')\\s*:', 'i');
if ($element.attr('style').match(property_pattern)) {
var style = get_stored_attribute($element, 'style');
if (style !== false) {
if (style.match(property_pattern)) {
return true;
}
}
}
return false;
}
function has_stored_hover_property($element, property) {
var property_pattern = new RegExp('(^' + property + '|[ \\"\\\'\\;]' + property + ')\\s*:', 'i');
if ($element.attr('data-hover').match(property_pattern)) {
var style = get_stored_attribute($element, 'data-hover');
if (style !== false) {
if (style.match(property_pattern)) {
return true;
}
}
}
return false;
}
function open_shortcode_settings($shortcode, callback) {
function edit_shortcode(code) {
open_modal({
"title": azh.i18n.shortcode_edit,
"desc": "",
"label": azh.i18n.shortcode
}, $.trim(azh.shortcode_instances[code]), function (shortcode) {
azh.shortcode_instances[code] = shortcode;
$.post(azh.ajaxurl, {
'action': 'azh_update_shortcode',
'post_id': azh.post_id,
'instance': code,
'shortcode': shortcode
}, function (data) {
if (data) {
$shortcode.html(data);
$shortcode.find('a[href]').on('click', function (event) {
event.preventDefault();
});
if (callback) {
callback();
}
}
});
azh.change();
});
}
if ('shortcode_instances' in azh && azh.shortcode_instances && $shortcode.attr('data-shortcode') in azh.shortcode_instances) {
var match = get_shortcode_match(azh.shortcode_instances[$shortcode.attr('data-shortcode')]);
if (match) {
var name = match[2];
var values = wp_shortcode.attrs(match[3]).named;
var content = match[5];
var settings = azh.shortcodes[name];
values['content'] = content;
azh.open_element_settings_dialog(settings, values, function (shortcode, attrs) {
var shortcode = make_shortcode(settings, shortcode, attrs);
azh.shortcode_instances[$shortcode.attr('data-shortcode')] = shortcode;
var instance = $shortcode.attr('data-shortcode');
$.post(azh.ajaxurl, {
'action': 'azh_update_shortcode',
'post_id': azh.post_id,
'instance': instance,
'shortcode': shortcode
}, function (data) {
if (data) {
var $section = $shortcode.closest('[data-section]');
$shortcode.replaceWith(data);
$shortcode.find('a[href]').on('click', function (event) {
event.preventDefault();
});
$shortcode = $section.find('[data-shortcode="' + instance + '"]');
store_html($shortcode.get(0));
azh.section_customization_init($section);
if (callback) {
callback();
}
}
});
azh.change();
});
}
} else {
if ($shortcode.is('[data-element*="shortcode"]')) {
var code = $shortcode.attr('data-element');
var shortcode = '';
if (!$shortcode.children().length) {
shortcode = $shortcode.text();
}
if ('shortcode_instances' in azh && azh.shortcode_instances && code in azh.shortcode_instances) {
shortcode = azh.shortcode_instances[code]
} else {
code = 'shortcode: ' + makeid();
$shortcode.attr('data-element', code);
set_stored_attribute($shortcode, 'data-element', code);
azh.shortcode_instances[code] = shortcode;
}
var match = get_shortcode_match(shortcode);
if (match) {
var name = match[2];
var values = wp_shortcode.attrs(match[3]).named;
var content = match[5];
values['content'] = content;
if (azh.shortcodes[name]) {
var settings = azh.shortcodes[name];
azh.open_element_settings_dialog(settings, values, function (shortcode, attrs) {
var shortcode = make_shortcode(settings, shortcode, attrs);
azh.shortcode_instances[code] = shortcode;
$.post(azh.ajaxurl, {
'action': 'azh_update_shortcode',
'post_id': azh.post_id,
'instance': code,
'shortcode': shortcode
}, function (data) {
if (data) {
$shortcode.html(data);
$shortcode.find('a[href]').on('click', function (event) {
event.preventDefault();
});
if (callback) {
callback();
}
}
});
azh.change();
});
} else {
edit_shortcode(code);
}
} else {
edit_shortcode(code);
}
}
}
}
function generate_modal_buttons($wrapper, $target_utility) {
if ('modal_options' in azh) {
var order = 1;
$(azh.modal_options).each(function () {
var options = this;
if ($target_utility) {
if (options['menu'] && options['menu'] !== 'utility') {
return;
}
} else {
if (options['menu'] && options['menu'] !== 'context') {
return;
}
}
if (!options.order) {
options.order = order;
}
order++;
if ('selector' in options) {
$wrapper.find(options['selector']).addBack().filter(options['selector']).each(function () {
var $element = azh.$(this);
var $section_or_element = $element.closest('[data-section].azh-controls, [class*="azh-col-"].azh-controls, [data-element].azh-controls');
var $utility = get_utility($section_or_element);
if ($element.parents('[data-element*="shortcode"]').length) {
return;
}
if ($target_utility) {
if ('menu' in options && options['menu'] == 'utility') {
if (!$target_utility.is($utility)) {
return;
}
} else {
return;
}
}
var $button = $('
' + options['button_text'] + '
').on('click', function (event) {
var $this = $(this);
open_attribute_modal(options, $this.data('azh-linked-node'), function () {
if ($this.data('azh-options')['refresh']) {
section_refresh($this.data('azh-linked-node').closest('[data-section]'));
}
});
}).on('mouseenter', function () {
var $this = $(this);
if ($this.data('azh-linked-node')) {
$this.data('azh-linked-node').addClass('azh-over');
}
}).on('mouseleave', function () {
var $this = $(this);
if ($this.data('azh-linked-node')) {
$this.data('azh-linked-node').removeClass('azh-over');
}
});
$button.data('azh-options', options);
$button.data('azh-linked-node', $element);
if ('menu' in options) {
if (options['menu'] === 'context') {
var context = $element.data('azh-context');
if (!context) {
context = [];
}
context.push($button);
$element.data('azh-context', context);
$element.addClass('azh-context');
}
if (options['menu'] === 'utility') {
$utility.children('.azh-controls-list').append($button);
$utility.data('azh-filled', true);
}
} else {
if ('section_control' in options && options['section_control']) {
var $section = $element.closest('[data-section]');
$section.data('azh-controls').append($button);
} else {
var $section_or_element = $element.closest('[data-section].azh-controls, [data-element].azh-controls');
$section_or_element.data('azh-controls').append($button);
}
}
});
}
});
}
}
function generate_controls($wrapper, $target_utility) {
function init_controls_options_tree() {
function selector_insert(current_node, i, selector) {
var node = current_node.children[selector[i]];
if (!node) {
node = {
options_list: [],
children: {}
};
current_node.children[selector[i]] = node;
}
if ((i + 1) < selector.length) {
return selector_insert(node, i + 1, selector);
}
return node;
}
if (!azh.selectors_cache_tree) {
azh.selectors_cache_tree = {
children: {}
};
azh.context_selectors_cache_tree = {
children: {}
};
azh.utility_selectors_cache_tree = {
children: {}
};
var order = 1;
$(azh.controls_options).each(function () {
var options = this;
if (options['selector']) {
var selectors = options['selector'].split(',');
if (!options.order) {
options.order = order;
}
$(selectors).each(function () {
var selector = $.trim(this).replace(/([\~\+\>])/g, ' $1 ').replace(/\s\s+/g, ' ').replace(/ ([\~\+\>]) /g, ' $1');
selector = selector.replace(/" "/g, '"***"');
selector = selector.split(' ');
selector = selector.map(function (s) {
return s.replace(/"\*\*\*"/g, '" "');
});
selector_insert(azh.selectors_cache_tree, 0, selector).options_list.push(options);
if (options['menu'] === 'context') {
selector_insert(azh.context_selectors_cache_tree, 0, selector).options_list.push(options);
}
if (options['menu'] === 'utility') {
selector_insert(azh.utility_selectors_cache_tree, 0, selector).options_list.push(options);
}
});
}
order++;
});
}
}
function traverse($node, current_node, standard_element) {
for (var selector_part in current_node.children) {
if (standard_element && selector_part !== standard_element && selector_part in azh.standard_elements_start_classes) {
//continue;
}
//console.log(selector_part);
var $next_element = $node.find(selector_part);
if (current_node === azh.selectors_cache_tree || current_node === azh.context_selectors_cache_tree || current_node === azh.utility_selectors_cache_tree) {
$next_element = $next_element.addBack().filter(selector_part);
}
if ($next_element.length) {
var options_list = current_node.children[selector_part]['options_list'];
if (options_list) {
//console.log('options_list: ' + options_list.length);
$(options_list).each(function () {
var options = this;
$next_element.each(function () {
control_create(azh.$(this), options, $target_utility);
});
});
}
$next_element.each(function () {
if (!azh.$(this).is($node)) {
traverse(azh.$(this), current_node.children[selector_part]);
}
});
}
}
}
if ('controls_options' in azh) {
init_controls_options_tree();
if ($target_utility) {
var standard_element = false;
if ($target_utility.data('azh-linked-element')) {
var $element_start = $target_utility.data('azh-linked-element').children().first();
if($element_start.is(Object.keys(azh.standard_elements_start_classes).join(', '))) {
var classes = $element_start.attr('class').split(' ').map(function(item){return $.trim(item);});
$.each(classes, function() {
var c = '.' + this;
if(c in azh.standard_elements_start_classes) {
standard_element = c;
return;
}
});
}
}
//console.log($wrapper);
var start_time = Date.now();
traverse($wrapper, azh.utility_selectors_cache_tree, standard_element);
console.log('traverse: ' + (Date.now() - start_time));
} else {
traverse($wrapper, azh.context_selectors_cache_tree);
}
//traverse($wrapper, azh.selectors_cache_tree);
$(azh.controls_options).each(function () {
var options = this;
if ($target_utility) {
if (options['menu'] && options['menu'] !== 'utility') {
return;
}
} else {
if (options['menu'] && options['menu'] !== 'context') {
return;
}
}
if (!('selector' in options)) {
switch (options['type']) {
case 'input-attribute':
case 'textarea-attribute':
case 'dropdown-attribute':
case 'radio-attribute':
case 'integer-attribute':
case 'toggle-attribute':
case 'url-attribute':
case 'toggle-url-argument':
case 'image-attribute':
case 'color-attribute':
$wrapper.find('[' + options['attribute'] + ']').each(function () {
var value = get_stored_attribute(azh.$(this), options['attribute']);
if (value !== false) {
if ('restriction' in options && options['restriction']) {
var restriction = options['restriction'];
var pattern = new RegExp('{' + options['attribute'] + '}', "g");
restriction = restriction.replace(pattern, value);
if ($wrapper.find(restriction).length) {
return;
}
}
control_create(azh.$(this), options, $target_utility);
}
});
break;
case 'dropdown-style':
case 'radio-style':
case 'integer-style':
case 'integer-list-style':
case 'background-image':
case 'color-style':
case 'font-family':
if (options['attribute']) {
$wrapper.find('[' + options['attribute'] + ']').each(function () {
var $this = azh.$(this);
if (has_stored_hover_property($this, options['property'])) {
control_create($this, options, $target_utility);
}
});
} else {
$wrapper.find('[style]').each(function () {
var $this = azh.$(this);
if (options['is_selector']) {
if (!$this.is(options['is_selector'])) {
return;
}
}
if (has_stored_css_property($this, options['property'])) {
control_create($this, options, $target_utility);
}
});
}
break;
}
}
});
}
}
function fill_utility($wrapper, $utility, no_detach) {
if (!no_detach) {
azh.controls_container.detach();
wrapper_detach($wrapper);
}
generate_modal_buttons($wrapper, $utility);
generate_controls($wrapper, $utility);
elements_sort($utility.children('.azh-controls-list'), ':not(.azh-group):not(.azh-subgroup):not(.azh-cloneable-group):not(.azh-utility-title):not(.azh-context-menu-title)');
group_controls($utility.children('.azh-controls-list'));
if (!no_detach) {
azh.controls_container.appendTo($body);
wrapper_restore($wrapper);
}
if ($utility) {
$utility.find('.azh-responsive [data-prefix="' + azh.device_prefix + '"]').trigger('click');
$utility.data('azh-filled', true);
}
}
function init_possible_controls_selector_start_tree() {
function selector_insert(current_node, i, selector) {
var node = current_node.children[selector[i]];
if (!node) {
node = {
options_list: [],
children: {}
};
current_node.children[selector[i]] = node;
}
if ((i + 1) < selector.length) {
return selector_insert(node, i + 1, selector);
}
return node;
}
if (!azh.possible_controls_selector_start_tree) {
azh.possible_controls_selector_start_tree = {
children: {}
};
var order = 1;
$(azh.controls_options).each(function () {
var options = this;
if (options['selector']) {
var possible = false;
$(azh.possible_controls_selector_start).each(function () {
if (options['selector'].indexOf(this) >= 0) {
possible = true;
return false;
}
});
if (possible) {
var selectors = options['selector'].split(',');
if (!options.order) {
options.order = order;
}
$(selectors).each(function () {
var selector = $.trim(this).replace(/([\~\+\>])/g, ' $1 ').replace(/\s\s+/g, ' ').replace(/ ([\~\+\>]) /g, ' $1');
selector = selector.replace(/" "/g, '"***"');
selector = selector.split(' ');
selector = selector.map(function (s) {
return s.replace(/"\*\*\*"/g, '" "');
});
if (options['menu'] === 'utility') {
selector_insert(azh.possible_controls_selector_start_tree, 0, selector).options_list.push(options);
}
});
}
}
order++;
});
}
}
function refresh_utility($utility) {
var $wrapper = false;
var $linked_element = $utility.data('azh-linked-element');
$wrapper = $linked_element;
var $max_wrapper = $linked_element.closest('[data-section], [data-element]');
if ($max_wrapper.closest(azh.unlimited_max_wrapper).closest('[data-section], [data-element]').length) {
$max_wrapper = $max_wrapper.closest(azh.unlimited_max_wrapper).closest('[data-section], [data-element]');
} else {
if ($max_wrapper.parent().closest('[data-section], [data-element]').length) { // только непосредственный элемент-родитель может иметь настройки элементов-детей
$max_wrapper = $max_wrapper.parent().closest('[data-section], [data-element]');
}
if ($max_wrapper.parent().closest('[data-section], [data-element]').length) { // только непосредственный элемент-родитель может иметь настройки элементов-детей
$max_wrapper = $max_wrapper.parent().closest('[data-section], [data-element]');
}
}
if ($linked_element.parents($max_wrapper).length) {
init_possible_controls_selector_start_tree();
var possible_start_selector = Object.keys(azh.possible_controls_selector_start_tree.children).join(', ');
$linked_element.parentsUntil($max_wrapper).each(function () {
var $node = azh.$(this);
if ($node.is(possible_start_selector)) {
$wrapper = $node.parent();
}
});
}
if ($wrapper.length) {
$utility.children('.azh-controls-list').empty();
fill_utility($wrapper, $utility);
}
}
function clear_element_utility($element) {
var $section_or_element = $element.closest('[data-section].azh-controls, [class*="azh-col-"].azh-controls, [data-element].azh-controls');
var $utility = get_utility($section_or_element);
var $linked_element = $utility.data('azh-linked-element');
$wrapper = $linked_element.closest('[data-section]');
if ($wrapper.length) {
$utility.children('.azh-controls-list').empty();
$utility.data('azh-filled', false);
}
}
function get_utility_state($element) {
var $section_or_element = $element.closest('[data-section].azh-controls, [class*="azh-col-"].azh-controls, [data-element].azh-controls');
var $utility = get_utility($section_or_element);
var group = $utility.find('[data-group].azh-group.azh-active').data('group');
var subgroup = $utility.find('[data-subgroup].azh-subgroup.azh-active').data('subgroup');
return {
scrollTop: $utility.children('.azh-controls-list').scrollTop(),
group: group,
subgroup: subgroup
};
}
function set_utility_state($element, state) {
var $section_or_element = $element.closest('[data-section].azh-controls, [class*="azh-col-"].azh-controls, [data-element].azh-controls');
var $utility = get_utility($section_or_element);
$utility.closest('.azh-utility-wrapper').trigger('click');
$utility.find('[data-group="' + state.group + '"].azh-title').trigger('click');
$utility.find('[data-subgroup="' + state.subgroup + '"].azh-title').trigger('click');
$utility.children('.azh-controls-list').scrollTop(state.scrollTop);
return $utility;
}
function elements_sort($parent, selector) {
var $elements = $parent.children();
if (selector) {
$elements = $parent.children(selector);
}
$elements.detach();
$elements.sort(function (e1, e2) {
var o1 = parseInt($(e1).attr('data-order'), 10);
var o2 = parseInt($(e2).attr('data-order'), 10);
return (o1 < o2) ? -1 : (o1 > o2) ? 1 : 0;
});
$parent.append($elements);
}
function is_section_refresh($cloneable) {
return $cloneable.data('azh-cloneable') && $cloneable.data('azh-cloneable')['refresh'] === true || $cloneable.is(azh.cloneable_refresh.join(',')) || $cloneable.find(azh.cloneable_refresh_children.join(',')).length;
}
function cloneable_group_controls($menu) {
if ($menu.closest('.azh-context-menu').length) {
return;
}
var $cloneables = azh.$();
var $linked_elements = azh.$();
var $menu_controls = $menu.children(':not(.azh-group):not(.azh-subgroup)');
if ($menu_controls.length) {
$menu_controls.each(function () {
var $this = $(this);
var $linked_element = $this.data('azh-linked-node');
if ($linked_element) {
$linked_elements = $linked_elements.add($linked_element);
var $element = $linked_element.closest('[data-element]');
var $cloneable = $linked_element.closest('[data-cloneable], [data-cloneable-inline]');
if ($cloneable.length) {
if (!$element.length || !$cloneable.has($element).length) {
$cloneables = $cloneables.add($cloneable);
}
}
}
});
$cloneables.each(function () {
var $cloneable = azh.$(this);
var children = [];
$cloneable.children().each(function () {
var $child = azh.$(this);
var $child_linked_elements = $linked_elements.filter(function () {
return (azh.$(this).parents().addBack().filter($child).length);
});
if ($child_linked_elements.length) {
children.push($child_linked_elements);
}
});
if (children.length) {
var $cloneable_groups = $('').appendTo($menu);
var $operations = $('').appendTo($cloneable_groups);
var $list = $('').appendTo($cloneable_groups);
var filled = false;
$(children).each(function () {
var $controls = $();
azh.$(this).each(function () {
$(azh.$(this).data('azh-linked-controls')).each(function () {
$controls = $controls.add(this);
});
});
$controls = $controls.filter($menu_controls);
if ($controls.length) {
filled = true;
$controls.detach();
var id = makeid();
var $cloneable_group = $('').appendTo($list).append($controls);
subgroup_controls($cloneable_group);
elements_sort($cloneable_group);
var $linked_element = $controls.first().data('azh-linked-node');
if ($linked_element) {
// var $child = $linked_element.parentsUntil('[data-cloneable], [data-cloneable-inline]').last();
// if (!$child.length) {
// $child = $linked_element;
// }
var $child = $linked_element.closest('.azh-cloneable-child-controls');
var $triggers = $('').appendTo($operations).data('azh-linked-child', $child).on('mouseenter', function () {
$(this).data('azh-linked-child').addClass('azh-over');
}).on('mouseleave', function () {
$(this).data('azh-linked-child').removeClass('azh-over');
}).data('azh-linked-child', $child);
$('').appendTo($triggers).on('click', function (event) {
var $child = $(this).data('azh-linked-child');
var $trigger = $(this).closest('[data-trigger]');
var duration = event.which ? 400 : 0;
$trigger.addClass('azh-active').siblings().removeClass('azh-active');
$list.children().filter('[data-id="' + $trigger.data('trigger') + '"]').fadeIn(duration).siblings().css("display", "none");
$child.trigger('azh-active');
return false;
}).data('azh-linked-child', $child);
$('').appendTo($triggers).on('click', function () {
var $child = $(this).data('azh-linked-child');
var $cloneable = $child.closest('[data-cloneable], [data-cloneable-inline]');
if (is_section_refresh($cloneable)) {
$child.data('azh-cloneable-child-controls').find('.azh-clone').trigger('click');
} else {
var state = get_utility_state($child);
$child.data('azh-cloneable-child-controls').find('.azh-clone').trigger('click');
set_utility_state($child, state);
}
return false;
}).data('azh-linked-child', $child);
$('').appendTo($triggers).on('click', function () {
if ($list.children().length > 1) {
var $child = $(this).data('azh-linked-child');
var $cloneable = $child.closest('[data-cloneable], [data-cloneable-inline]');
if (is_section_refresh($cloneable)) {
$child.data('azh-cloneable-child-controls').find('.azh-remove').trigger('click');
} else {
var state = get_utility_state($child);
$child.data('azh-cloneable-child-controls').find('.azh-remove').trigger('click');
set_utility_state($cloneable, state);
}
}
return false;
}).data('azh-linked-child', $child);
}
}
});
if (filled) {
$operations.sortable({
handle: '.azh-move',
placeholder: 'azh-placeholder',
forcePlaceholderSize: true,
update: function (event, ui) {
var $child = $(ui.item).data('azh-linked-child');
var $cloneable = $child.closest('[data-cloneable], [data-cloneable-inline]');
$child.detach();
if ($(ui.item).next().length) {
var $next_child = $(ui.item).next().data('azh-linked-child');
$next_child.before($child);
} else {
if ($(ui.item).prev().length) {
var $prev_child = $(ui.item).prev().data('azh-linked-child');
$prev_child.after($child);
}
}
if (is_section_refresh($cloneable)) {
section_refresh($cloneable.closest('[data-section]'));
}
},
over: function (event, ui) {
ui.placeholder.attr('class', ui.helper.attr('class'));
ui.placeholder.removeClass('ui-sortable-helper');
ui.placeholder.attr('style', ui.helper.attr('style'));
ui.placeholder.css('position', 'relative');
ui.placeholder.css('z-index', 'auto');
ui.placeholder.css('left', 'auto');
ui.placeholder.css('top', 'auto');
ui.placeholder.addClass('azh-placeholder');
}
});
$operations.children().first().find('.azh-move').trigger('click');
} else {
$cloneable_groups.remove();
}
}
});
}
}
function subgroup_controls($group) {
var subgroups = {};
$group.find('> [data-subgroup]').each(function () {
subgroups[$(this).data('subgroup')] = true;
});
subgroups = Object.keys(subgroups);
if (subgroups.length) {
var $subgroups = $('').appendTo($group);
var $titles = $('').appendTo($subgroups);
var $list = $('').appendTo($subgroups);
$(subgroups).each(function () {
var $controls = $group.find('> [data-subgroup="' + this + '"]').detach();
var $subgroup = $('').appendTo($list).append($controls);
//cloneable_group_controls($subgroup);
//elements_sort($subgroup);
$('' + this + '').appendTo($titles).on('click', function (event) {
var $this = $(this);
$this.addClass('azh-active').siblings().removeClass('azh-active');
var duration = event.which ? 400 : 0;
$group.find('.azh-subgroup[data-subgroup="' + $this.data('subgroup') + '"]').fadeIn(duration).siblings().css("display", "none");
return false;
});
});
$titles.children().first().trigger('click');
}
}
function group_controls($menu) {
var groups = {};
$menu.find('> [data-group]').each(function () {
groups[$(this).data('group')] = true;
});
groups = Object.keys(groups);
$(groups).each(function () {
var $group = $menu.find('> [data-group="' + this + '"]:not(.azh-group)').wrapAll('').parent();
var $children = $group.children();
var $list = $('').insertBefore($children.first());
$children.detach().appendTo($list);
//$group.children().wrapAll('');
//elements_sort($group.children().first());
var $title = $('
' + this + '
').prependTo($group).on('click', function (event) {
var $this = $(this);
var duration = event.which ? 400 : 0;
if ($this.closest('.azh-group').is('.azh-active')) {
$this.closest('.azh-group').removeClass("azh-active").find('.azh-group-list').slideUp(duration);
} else {
$menu.find('.azh-group').removeClass("azh-active").find('.azh-group-list').slideUp(duration);
$this.closest('.azh-group').addClass("azh-active").find('.azh-group-list').slideDown(duration, function () {
$(this).css('overflow', 'visible');
});
}
return false;
});
});
$menu.find('.azh-group > .azh-group-list').each(function () {
cloneable_group_controls($(this));
subgroup_controls($(this));
}).hide();
cloneable_group_controls($menu);
}
function get_linked_element($element) {
var $linked_element = false;
var linked_ids = $element.closest('[data-section]').data('azh-linked-ids');
$element.find('.azh-id-attr, .azh-hash-attr').addBack().filter('.azh-id-attr, .azh-hash-attr').each(function () {
var $this = azh.$(this);
var id = false;
if ($this.data('azh-id-attr')) {
id = $this.attr($this.data('azh-id-attr'));
}
if ($this.data('azh-hash-attr')) {
id = $this.attr($this.data('azh-hash-attr')).replace('#', '');
}
if (id && id in linked_ids) {
$(linked_ids[id]).each(function () {
if (!azh.$(this).is($this)) {
$linked_element = azh.$(this).parentsUntil('[data-cloneable], [data-cloneable-inline]').last();
if ($linked_element.length === 0) {
$linked_element = azh.$(this);
}
return false;
}
});
if ($linked_element) {
return false;
}
}
});
return $linked_element;
}
function is_controls_visible($node) {
var v = $node.closest('.azh-controls-visible').length;
var h = $node.closest('.azh-controls-hidden').length;
if (v > 0 && h === 0 || $node.is('.azh-controls-visible')) {
return true;
}
if (h > 0 && v === 0 || $node.is('.azh-controls-hidden')) {
return false;
}
v = $node.parents().index($node.closest('.azh-controls-visible'));
h = $node.parents().index($node.closest('.azh-controls-hidden'));
if (v < h) {
return true;
}
return false;
}
function clone_for_new_element($element_wrapper) {
var $new_element_wrapper = false;
if ($element_wrapper.is('[data-element]')) {
if ($element_wrapper.contents().length === 0) {
$new_element_wrapper = $element_wrapper;
} else {
$new_element_wrapper = $element_wrapper.clone(true);
$new_element_wrapper.insertAfter($element_wrapper);
}
} else {
if ($element_wrapper.find('[data-element]').children().length === 0) {
$new_element_wrapper = $element_wrapper;
} else {
$new_element_wrapper = $element_wrapper.clone(true);
$new_element_wrapper.insertAfter($element_wrapper);
}
}
var $new_element = $new_element_wrapper.is('[data-element]') ? $new_element_wrapper : $new_element_wrapper.find('[data-element]');
make_element_empty($new_element);
clear_element_classes($new_element);
create_element_controls($new_element);
return $new_element;
}
azh.window.trigger("azh-customization-before-init", {
wrapper: $wrapper
});
wrapper_detach($wrapper);
azh.controls_container = false;
if ($('.azh-controls-container').length) {
azh.controls_container = $('.azh-controls-container');
azh.controls_container.detach();
} else {
azh.controls_container = $('');
}
$wrapper.on('contextmenu', function (event) {
function fill_context_menu($context_menu, $target) {
function fill($context_menu, $target) {
if ($target.is('.azh-context')) {
$($target.data('azh-context')).each(function () {
var $button = $(this);
if ($context_menu.children().filter('[data-type="' + $button.data('type') + '"]').length === 0) {
$context_menu.append($button.clone(true).data('azh-linked-node', $target));
}
});
}
$target.parents('.azh-context').each(function () {
var $element = azh.$(this);
$($element.data('azh-context')).each(function () {
var $button = $(this);
if ($context_menu.children().filter('[data-type="' + $button.data('type') + '"]').length === 0) {
$context_menu.append($button.clone(true).data('azh-linked-node', $element));
}
});
});
}
fill($context_menu, $target);
var linked_ids = $target.closest('[data-section]').data('azh-linked-ids');
$target.parents('.azh-id-attr').addBack().filter('.azh-id-attr').each(function () {
var $this = azh.$(this);
if ($this.is('.azh-id-attr')) {
var id = $this.attr($this.data('azh-id-attr'));
if (id in linked_ids) {
$(linked_ids[id]).each(function () {
if (!azh.$(this).is($this)) {
fill($context_menu, azh.$(this));
}
});
}
}
});
$target.parents('.azh-hash-attr').addBack().filter('.azh-hash-attr').each(function () {
var $this = azh.$(this);
if ($this.is('.azh-hash-attr')) {
var id = $this.attr($this.data('azh-hash-attr')).replace('#', '');
if (id in linked_ids) {
$(linked_ids[id]).each(function () {
if (!azh.$(this).is($this)) {
fill($context_menu, azh.$(this));
}
});
}
}
});
var $modal_buttons = $context_menu.find('.azh-modal-button');
$modal_buttons.detach();
$context_menu.prepend($modal_buttons);
}
event.preventDefault();
var $target = azh.$(event.target);
if ($target.closest('.azh-deferred-context-controls').length) {
var $deferred = $target.closest('.azh-deferred-context-controls');
generate_controls($deferred);
$deferred.removeClass('azh-deferred-context-controls');
}
var $context_menu = $('.azh-context-menu').length ? $('.azh-context-menu') : $('').appendTo($body);
$context_menu.hide();
$context_menu.empty();
$('
' + create_title($target) + '
').prependTo($context_menu);
var $controls_list = $('').appendTo($context_menu);
$context_menu.css('left', (event.clientX + (azh.device_left ? azh.device_left : 0)) + 'px');
$context_menu.css('top', event.clientY + 'px');
$document.off('click.azh-context-menu').on('click.azh-context-menu', function (event) {
if (!$(event.target).closest(azh.click_not_hide_contextmenu).length) {
if (!$(event.target).closest('.azh-context-menu').length) {
$context_menu.hide();
$document.off('click.azh-context-menu');
}
}
});
if ($target.is('.azh-auxiliary')) {
$target.css('pointer-events', 'none');
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
$target.css('pointer-events', '');
}
fill_context_menu($controls_list, $target);
if ($target.css('position') === 'absolute') {
$target.css('pointer-events', 'none');
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
$target.css('pointer-events', '');
}
if ($target.is('.az-spacer')) {
$target.closest('[data-element]').css('pointer-events', 'none');
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
$target.closest('[data-element]').css('pointer-events', '');
}
if ($target.children('.az-overlay').length) {
$target.children('.az-overlay').get(0).style.setProperty("pointer-events", "all", "important");
$target.children('.az-overlay').children().each(function () {
this.style.setProperty("pointer-events", "all", "important");
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
});
$target.children('.az-overlay').get(0).style.setProperty("pointer-events", "", "");
$target.children('.az-overlay').children().each(function () {
this.style.setProperty("pointer-events", "", "");
});
}
if ($target.closest('.az-remarkable').length) {
//скрываем обернутые элементы
$target.closest('.az-remarkable').children().first().css('pointer-events', 'none');
//минимальный индекс у абсолютных обертышей
var min_z_index = 0;
$target.closest('.az-remarkable').children().last().children('.az-remark-wrapper').each(function () {
if (min_z_index > parseInt(azh.$(this).css('z-index'), 10)) {
min_z_index = parseInt(azh.$(this).css('z-index'), 10);
}
});
//опускаем индекс до 0 у абсолютных обертышей
$target.closest('.az-remarkable').children().last().children('.az-remark-wrapper').each(function () {
azh.$(this).data('azh-old-z-index', azh.$(this).css('z-index'));
azh.$(this).css('z-index', parseInt(azh.$(this).css('z-index'), 10) - min_z_index);
});
//проходим по всем абсолютным обертышам в таком положении
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
//проходим по каждому обертышу при скрытии остальных
$target.closest('.az-remarkable').children().last().children('.az-remark-wrapper').each(function () {
azh.$(this).css('pointer-events', 'all');
azh.$(this).siblings().css('pointer-events', 'none');
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
});
//показываем всех обертышей
$target.closest('.az-remarkable').children().last().children('.az-remark-wrapper').css('pointer-events', '');
//востанавливаем индекс всех обертышей
$target.closest('.az-remarkable').children().last().children('.az-remark-wrapper').each(function () {
azh.$(this).css('z-index', azh.$(this).data('azh-old-z-index'));
});
//показываем обернутые элементы и скрываем всех обертышей
$target.closest('.az-remarkable').children().first().css('pointer-events', '');
$target.closest('.az-remarkable').children().last().css('pointer-events', 'none');
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
$target.closest('.az-remarkable').children().last().css('pointer-events', '');
}
$target.children().each(function () {
var $this = azh.$(this);
if ($this.css('pointer-events') === 'none') {
$this.css('pointer-events', 'all');
fill_context_menu($controls_list, azh.$(azh.document.get(0).elementFromPoint(event.clientX, event.clientY)));
$this.css('pointer-events', '');
}
});
$controls_list.children().off('mouseenter').on('mouseenter', function () {
var $this = $(this);
if ($this.data('azh-linked-node')) {
$this.data('azh-linked-node').addClass('azh-over');
}
});
$controls_list.children().off('mouseleave').on('mouseleave', function () {
var $this = $(this);
if ($this.data('azh-linked-node')) {
$this.data('azh-linked-node').removeClass('azh-over');
}
});
setTimeout(function () {
$controls_list.children().each(function () {
$(this).trigger('azh-init');
});
group_controls($controls_list);
var $cm_panel = $controls_list.closest('.azh-context-menu');
if ($cm_panel.length) {
if (($cm_panel.offset().top + $cm_panel.outerHeight()) > $body.height()) {
$cm_panel.css('top', ($body.height() - $cm_panel.outerHeight()) + 'px');
}
}
});
if ($controls_list.children().length) {
$context_menu.show().trigger('azh-showed');
$context_menu.find('.azh-modal-button, .azh-button').on('click', function () {
$context_menu.hide();
});
$context_menu.draggable({
handle: ".azh-context-menu-title"
});
}
});
$wrapper.find('.az-contenteditable').addBack().filter('.az-contenteditable').each(function () {
enable_contenteditable(azh.$(this));
});
$wrapper.find('span.az-inline').each(function () {
var $this = azh.$(this);
if ($this.parent().children().length === $this.parent().children().filter('span.az-inline').length) {
enable_contenteditable($this.parent());
}
});
$wrapper.contents().filter(function () {
return this.nodeType === 3;
}).each(function () {
if ($.trim(this.textContent)) {
enable_contenteditable(azh.$(this).parent());
}
});
$wrapper.find('*:not(iframe)').contents().filter(function () {
return this.nodeType === 3;
}).each(function () {
if ($.trim(this.textContent)) {
enable_contenteditable(azh.$(this).parent());
}
});
$wrapper.find('a:not(.azh-id-attr):not(.azh-hash-attr)').each(function () {
var $link = azh.$(this);
if ($link.closest(azh.dynamic_content).length) {
return;
}
var $button = $('
' + azh.i18n.edit_link + '
').on('click', function (event) {
var $link = $(this).data('azh-linked-node');
azh.open_link_select_dialog(event, function (url, target, title) {
$link.attr('href', url);
$link.attr('target', target ? target : '_self');
set_stored_attribute($link, 'href', url);
set_stored_attribute($link, 'target', target ? target : '_self');
set_stored_attribute($link, 'title', title);
}, $link.attr('href'), $link.attr('target'), $link.attr('title'));
});
var context = $link.data('azh-context');
if (!context) {
context = [];
}
context.push($button);
$link.data('azh-context', context);
$link.addClass('azh-context');
});
$wrapper.find('a[href]').on('click', function (event) {
event.preventDefault();
});
$wrapper.find('img, [style*="background-image"]').addBack().filter('img, [style*="background-image"]').each(function () {
var $image = azh.$(this);
if ($image.closest(azh.dynamic_content).length) {
return;
}
if ($image.prop('tagName') !== 'IMG') {
var url = /background-image\:[^;]*url\(['"]?([^'"\)]+)['"]?\)/gi.exec($image.attr('style'));
if (url) {
if (url[1].indexOf('http') < 0 && url[1].indexOf('//') !== 0) {
return;
}
}
}
var $button = $('
' + azh.i18n.edit_image + '
').on('click', function (event) {
var $image = $(this).data('azh-linked-node');
azh.open_image_select_dialog(event, function (url, id) {
if ($image.prop('tagName') === 'IMG') {
$image.attr('src', url);
set_stored_attribute($image, 'src', url);
} else {
$image.css('background-image', "url('" + url + "')");
set_stored_style($image, 'background-image', "url('" + url + "')");
}
});
});
var context = $image.data('azh-context');
if (!context) {
context = [];
}
context.push($button);
$image.data('azh-context', context);
$image.addClass('azh-context');
});
$wrapper.find('select').each(function () {
var $select = azh.$(this);
var $button = $('
' + azh.i18n.select_options_edit + '
').on('click', function (event) {
var $this = $(this);
var $select = $this.data('azh-linked-node');
open_select_modal($select);
});
var context = $select.data('azh-context');
if (!context) {
context = [];
}
context.push($button);
$select.data('azh-context', context);
$select.addClass('azh-context');
});
$wrapper.find('.az-icon').each(function () {
var $icon = azh.$(this);
var $button = $('
' + azh.i18n.edit_icon + '
').on('click', function (event) {
var $icon = $(this).data('azh-linked-node');
var az_classes = $icon.attr('class').split(' ').filter(function (v) {
return (v.indexOf('az-') >= 0 ? v : false);
}).join(' ');
var azh_classes = $icon.attr('class').split(' ').filter(function (v) {
return (v.indexOf('azh-') >= 0 ? v : false);
}).join(' ');
azh.open_icon_select_dialog(event, $icon.attr('class'), function (icon_class) {
load_required_scripts(icon_class);
$icon.attr('class', icon_class);
$icon.addClass(az_classes);
set_stored_attribute($icon, 'class', $icon.attr('class'));
$icon.addClass(azh_classes);
});
});
var context = $icon.data('azh-context');
if (!context) {
context = [];
}
context.push($button);
$icon.data('azh-context', context);
$icon.addClass('azh-context');
});
$wrapper.find('[data-element]').each(function () {
var $element = azh.$(this);
if ($element.parents('[data-element*="shortcode"]').length) {
return;
}
$element.contents().filter(function () {
return this.nodeType === 3;
}).each(function () {
if (!$.trim(this.textContent)) {
azh.$(this).remove();
}
});
if ($element.contents().length && ($element.attr('data-element') === '' || $element.attr('data-element') === ' ')) {
$element.attr('data-element', 'not-empty');
}
var $element_controls = create_element_controls($element);
$element.on('mouseenter', function (event) {
var $this = azh.$(this);
var $element_controls = $this.data('azh-controls');
setTimeout(function () {
set_element_controls_position($this, $element_controls);
if (azh.controls_container.find('.azh-element-controls.azh-active').length == 0) {
$element_controls.css('display', '');
if ($this.parent().is('.az-free-positioning') || $this.parent('.az-elements-list').parent('.az-free-positioning').length) {
$element_controls.find('.azh-move-element').show();
} else {
$element_controls.find('.azh-move-element').hide();
}
}
});
}).on('mousemove', function () {
var $this = azh.$(this);
var $element_controls = $this.data('azh-controls');
set_element_controls_position($this, $element_controls);
}).on('mouseleave', function () {
var $this = azh.$(this);
var $element_controls = $this.data('azh-controls');
setTimeout(function () {
if (!$element_controls.is(':hover') && azh.controls_container.find('.azh-resizer:hover').length === 0 && azh.controls_container.find('.azh-shortcode:hover').length === 0) {
$element_controls.hide();
} else {
$element_controls.one('mouseleave', function () {
$element_controls.hide();
});
}
});
}).on('click', function (event) {
var $element = azh.$(this);
if ($element.contents().length === 0) {
azh.child_suggestions = [];
$($element.parents('[data-element]')).each(function () {
var $e = $('.azh-library .azh-elements .azh-element[data-path="' + azh.$(this).attr('data-element') + '"]');
if ($e.length && $e.data('child-suggestions')) {
$($e.data('child-suggestions')).each(function () {
azh.child_suggestions.push($(this).data('path'));
});
}
});
var default_category = azh.default_category;
if ($element.parent('.az-free-positioning').length || $element.parent('.az-elements-list').parent('.az-free-positioning').length) {
azh.default_category = 'content';
}
azh.open_elements_dialog(function (path, html, require_scripts) {
if ($element.parents('a').length && ($(html).find('a').length || $(html).is('a'))) {
alert(azh.i18n.html_is_not_valid);
return;
}
var $button = get_remove_element_button($element.data('azh-controls'));
$button.data('azh-linked-element', $element);
$button = get_add_element_button($element.data('azh-controls'), true);
$button.data('azh-linked-element', $element);
$button = get_add_element_button($element.data('azh-controls'), false);
$button.data('azh-linked-element', $element);
$button = get_copy_element_button($element.data('azh-controls'));
$button.data('azh-linked-element', $element);
if (path === 'shortcode') {
var instance = 'shortcode: ' + makeid();
var shortcode = html;
$element.attr('data-element', instance);
set_stored_attribute($element, 'data-element', instance);
$element.data('azh-controls').find('> .azh-name').text($.trim(instance));
$element.addClass('azh-loading');
azh.shortcode_instances[instance] = shortcode;
$.post(azh.ajaxurl, {
'action': 'azh_update_shortcode',
'post_id': azh.post_id,
'instance': instance,
'shortcode': shortcode
}, function (data) {
$element.html(data);
$element.find('a[href]').on('click', function (event) {
event.preventDefault();
});
$element.removeClass('azh-loading');
customization_init($element);
$element.trigger('click');
open_shortcode_settings($element, function () {
if (azh.frontend_init) {
azh.frontend_init($element);
}
});
});
} else {
var scrollTop = azh.window.scrollTop();
$element.attr('data-element', path);
set_stored_attribute($element, 'data-element', path);
$element.data('azh-controls').find('> .azh-name').text($.trim(path));
init_element_wrapper_class($element, $element.data('azh-controls'));
$element.html(html);
$element.children().each(function () {
store_html(this);
});
clone_shortcodes($element);
shortcodes_refresh($element);
refresh_section_linked_ids($element.closest('[data-section]'));
customization_init($element);
clear_element_utility($element);
if (azh.frontend_init) {
azh.frontend_init($element);
}
//azh.window.trigger('resize'); //why need it? too expensive - fire "azh-init" events of all controls of section
$element.trigger('click');
azh.window.scrollTop(scrollTop);
if (require_scripts) {
//$element.addClass('azh-loading');
load_required_scripts('
' + html + '
', path, function () {
//$element.removeClass('azh-loading');
});
}
$element.trigger('azh-refresh');
}
azh.change();
});
azh.default_category = default_category;
return false;
}
});
if ($element.contents().length !== 0 || $element.is('[data-element*="shortcode"]')) {
var $button = get_remove_element_button($element_controls);
$button.data('azh-linked-element', $element);
$button = get_add_element_button($element.data('azh-controls'), true);
$button.data('azh-linked-element', $element);
$button = get_add_element_button($element.data('azh-controls'), false);
$button.data('azh-linked-element', $element);
$button = get_copy_element_button($element.data('azh-controls'));
$button.data('azh-linked-element', $element);
}
});
$wrapper.find('[data-element]').addBack().filter('[data-element]').each(function () {
var $element = azh.$(this);
if ($element.parent('.az-free-positioning').length || $element.parent('.az-elements-list').parent('.az-free-positioning').length || $element.css('position') === 'absolute') {
if ($element.contents().length > 0) {
var transform = get_stored_style($element, 'transform');
var match = /rotate\(([-\.\d]+)rad\)/g.exec(transform);
var radians = 0;
if (match) {
radians = parseFloat(match[1]);
}
}
enable_free_transform($element);
var context = $element.data('azh-context');
if (!context) {
context = [];
}
if (!$element.is('.az-left, .az-center, .az-right')) {
add_to_stored_classes($element, 'az-left');
$element.addClass('az-left');
}
if (!$element.is('.az-top, .az-middle, .az-bottom')) {
add_to_stored_classes($element, 'az-top');
$element.addClass('az-top');
}
var $alignment = $('').on('azh-init', function () {
var $this = $(this);
$this.find('.azh-active').removeClass('azh-active');
var $element = $this.data('azh-linked-node');
if ($element.is('.az-top')) {
$this.find('.azh-top-alignment').addClass('azh-active');
}
if ($element.is('.az-middle')) {
$this.find('.azh-middle-alignment').addClass('azh-active');
}
if ($element.is('.az-bottom')) {
$this.find('.azh-bottom-alignment').addClass('azh-active');
}
if ($element.is('.az-left')) {
$this.find('.azh-left-alignment').addClass('azh-active');
}
if ($element.is('.az-center')) {
$this.find('.azh-center-alignment').addClass('azh-active');
}
if ($element.is('.az-right')) {
$this.find('.azh-right-alignment').addClass('azh-active');
}
});
var $v_alignment = $('').appendTo($alignment);
var $h_alignment = $('').appendTo($alignment);
$('').appendTo($v_alignment).on('click', function (event) {
var $element = $(this).closest('.azh-control').data('azh-linked-node');
reset_free_positioning($element);
set_top_alignment($element);
$(this).closest('.azh-control').trigger('azh-init');
});
$('').appendTo($v_alignment).on('click', function (event) {
var $element = $(this).closest('.azh-control').data('azh-linked-node');
reset_free_positioning($element);
set_middle_alignment($element);
$(this).closest('.azh-control').trigger('azh-init');
});
$('').appendTo($v_alignment).on('click', function (event) {
var $element = $(this).closest('.azh-control').data('azh-linked-node');
reset_free_positioning($element);
set_bottom_alignment($element);
$(this).closest('.azh-control').trigger('azh-init');
});
$('').appendTo($h_alignment).on('click', function (event) {
var $element = $(this).closest('.azh-control').data('azh-linked-node');
reset_free_positioning($element);
set_left_alignment($element);
$(this).closest('.azh-control').trigger('azh-init');
});
$('').appendTo($h_alignment).on('click', function (event) {
var $element = $(this).closest('.azh-control').data('azh-linked-node');
reset_free_positioning($element);
set_center_alignment($element);
$(this).closest('.azh-control').trigger('azh-init');
});
$('').appendTo($h_alignment).on('click', function (event) {
var $element = $(this).closest('.azh-control').data('azh-linked-node');
reset_free_positioning($element);
set_right_alignment($element);
$(this).closest('.azh-control').trigger('azh-init');
});
context.push($alignment);
$element.data('azh-context', context);
$element.addClass('azh-context');
}
});
$wrapper.find('[data-shortcode], [data-element*="shortcode"]').addBack().filter('[data-shortcode], [data-element*="shortcode"]').each(function () {
var $shortcode = azh.$(this);
$shortcode.find('.azh-context').removeClass('azh-context');
$shortcode.find('[draggable]').attr('draggable', 'false');
$shortcode.find('[contenteditable]').attr('contenteditable', 'false');
$shortcode.attr('contenteditable', 'false');
var $element_controls = $shortcode.data('azh-controls');
if ($element_controls) {
var $button = $('').prependTo($element_controls).on('click', function () {
var $button = $(this);
var $shortcode = $button.data('azh-linked-node');
$element_controls = $shortcode.data('azh-controls');
open_shortcode_settings($shortcode, function () {
if (azh.frontend_init) {
azh.frontend_init($shortcode);
}
});
return false;
});
$button.data('azh-linked-node', $shortcode);
}
$shortcode.on('mouseenter.azh-shortcode', function (event) {
var $shortcode = azh.$(this);
var $hover = $('').appendTo(azh.controls_container).on('mouseleave', function (event) {
$(this).remove();
$shortcode.trigger('mouseleave');
}).css({
"top": $shortcode.offset().top,
"left": $shortcode.offset().left,
"width": $shortcode.outerWidth(),
"height": $shortcode.outerHeight()
});
if ($shortcode.is('[data-shortcode]')) {
$hover.on('click', function () {
open_shortcode_settings($shortcode, function () {
if (azh.frontend_init) {
azh.frontend_init($shortcode);
}
});
return false;
}).css('pointer-events', 'all');
}
}).on('mouseleave.azh-shortcode', function (event) {
azh.controls_container.find('.azh-shortcode:not(:hover)').remove();
});
});
var shortcode_attributes = ['data-azh-form'];
$(shortcode_attributes).each(function () {
var attribute = this;
$wrapper.find('[' + attribute + ']').addBack().filter('[' + attribute + ']').each(function () {
var $shortcode = azh.$(this);
var $element = $shortcode.closest('[data-element], [data-section]');
var $element_controls = $element.data('azh-controls');
if ($element_controls) {
var $button = $('').prependTo($element_controls).on('click', function () {
var $button = $(this);
var attribute = $button.data('azh-attribute');
var index = $button.data('azh-index');
var $element = $button.data('azh-linked-element');
var $shortcode = azh.$($element.find('[' + attribute + ']').get(index));
var match = get_shortcode_match($shortcode.attr(attribute).replace(/\\"/g, '"').replace(/\\'/g, "'"));
if (match) {
var name = match[2];
var values = wp_shortcode.attrs(match[3]).named;
var content = match[5];
values['content'] = content;
if (azh.shortcodes[name]) {
var settings = azh.shortcodes[name];
var $modal = azh.open_element_settings_dialog(settings, values, function (shortcode, attrs) {
var shortcode = make_shortcode(settings, shortcode, attrs).replace(/"/g, '\\"').replace(/'/g, "\\'");
$shortcode.attr(attribute, shortcode);
set_stored_attribute($shortcode, attribute, shortcode);
});
if (attribute === 'data-azh-form') {
var names = {};
$element.find('[name]').each(function () {
names[azh.$(this).attr('name')] = true;
});
$modal.find('em').each(function () {
var $this = $(this);
if ($this.text().indexOf('{form-fields}') >= 0) {
$this.text($this.text().replace('{form-fields}', ''));
var $param = $this.closest('[data-param-name]');
var $textarea = $param.find('textarea, input');
for (var name in names) {
(function (name) {
$('{' + name + '}').appendTo($this).on('click', function () {
insert_at_cursor($textarea.get(0), '{' + name + '}');
}).after('');
})(name);
}
}
});
}
}
}
return false;
});
$button.data('azh-linked-element', $element);
$button.data('azh-attribute', attribute);
var index = $element.find('[' + attribute + ']').filter($shortcode).index();
$button.data('azh-index', index);
}
});
});
if (azh.table_editor) {
$wrapper.find('table').each(function () {
function cell_init($cell) {
var context = $cell.data('azh-context');
if (!context) {
context = [];
}
if ($cell.is('td')) {
var $delete_row = $('
' + azh.i18n.delete_row + '
').on('click', function (event) {
var $cell = $(this).data('azh-linked-node');
$cell.closest('tr').remove();
});
context.push($delete_row);
}
if ($cell.is('th')) {
var $delete_column = $('
' + azh.i18n.delete_column + '
').on('click', function (event) {
var $cell = $(this).data('azh-linked-node');
var index = $cell.get(0).cellIndex;
$cell.closest('table').find('tr').each(function () {
this.removeChild(this.cells[index]);
});
});
context.push($delete_column);
}
if ($cell.is('td')) {
var $insert_row_before = $('
' + azh.i18n.insert_row_before + '
').on('click', function (event) {
var $cell = $(this).data('azh-linked-node');
var $tr = azh.$('
');
for (var i = 0; i < $cell.closest('tr').children().length; i++) {
$tr.append('
').on('click', function (event) {
var $cell = $(this).data('azh-linked-node');
var $tr = azh.$('
');
for (var i = 0; i < $cell.closest('tr').children().length; i++) {
$tr.append('
');
}
store_html($tr.get(0));
customization_init($tr);
$tr.children().each(function () {
cell_init(azh.$(this));
});
$cell.closest('tr').after($tr);
});
context.push($insert_row_after);
}
if ($cell.is('th')) {
var $insert_column_before = $('
' + azh.i18n.insert_column_before + '
').on('click', function (event) {
var $cell = $(this).data('azh-linked-node');
var index = $cell.get(0).cellIndex;
$cell.closest('table').find('tr').each(function () {
if (azh.$(this).parent('thead').length) {
var $th = azh.$('
').on('click', function (event) {
var $cell = $(this).data('azh-linked-node');
var index = $cell.get(0).cellIndex;
$cell.closest('table').find('tr').each(function () {
if (azh.$(this).parent('thead').length) {
var $th = azh.$('
').appendTo(azh.controls_container).on('mousedown', function (event) {
if (event.which == 1) {
var $resizer = $(this);
$resizer.data('azh-linked-node').trigger('click');
$resizer.data('azh-drag', true);
$resizer.data('azh-drag-y', event.pageY);
$resizer.css({
"height": parseInt(get_stored_responsive_style($element, 'height'), 10)
});
azh.document_off('mouseup.resizer').document_on('mouseup.resizer', function (e) {
$resizer.data('azh-drag', false);
if (!$resizer.is(':hover')) {
azh.document_off('mouseup.resizer');
}
});
azh.document_off('mousemove.resizer').document_on('mousemove.resizer', function (e) {
if ($resizer.data('azh-drag')) {
var d = e.pageY - $resizer.data('azh-drag-y');
var v = (parseInt(get_stored_responsive_style($element, 'height'), 10) + d);
if (!isNaN(v)) {
if (v < 0) {
v = 0;
}
v = v + 'px';
if (azh.device_prefix === 'lg') {
set_stored_style($element, 'height', v);
$element.css('height', v);
} else {
set_stored_attr_style($element, 'data-responsive-' + azh.device_prefix, 'height', v);
var style = get_stored_attribute($element, 'data-responsive-' + azh.device_prefix);
if (style !== false) {
$element.attr('data-responsive-' + azh.device_prefix, style);
}
azh.window.get(0).azh.refresh_responsive_css_rules($element);
}
$resizer.text(v);
$resizer.data('azh-drag-y', e.pageY);
$resizer.height(v);
}
}
});
event.stopPropagation();
event.preventDefault();
}
return false;
}).on('mousemove', function (event) {
var $resizer = $(this);
if (!$resizer.data('azh-drag')) {
$resizer.css('pointer-events', 'none');
var $target = azh.$(azh.document.get(0).elementFromPoint(event.clientX - azh.device_left, event.clientY));
$resizer.css('pointer-events', '');
if (!$target.is('.az-resize')) {
$element.data('azh-height-resizer', false);
$resizer.remove();
}
}
}).on('mouseleave', function (event) {
var $resizer = $(this);
if (!$resizer.data('azh-drag')) {
$element.data('azh-height-resizer', false);
$resizer.remove();
$element.trigger('mouseleave');
}
}).on('contextmenu', function (event) {
event.preventDefault();
open_context_menu(event, $(this).data('azh-linked-node'));
}).css({
"top": $element.offset().top,
"left": $element.offset().left,
"width": $element.innerWidth(),
"height": parseInt(get_stored_responsive_style($element, 'height'), 10) > 10 ? parseInt(get_stored_responsive_style($element, 'height'), 10) : 10
});
$element.data('azh-height-resizer', $resizer);
$resizer.data('azh-linked-node', $element);
}
}).on('mouseleave', function () {
var $element = azh.$(this);
setTimeout(function () {
if ($element.data('azh-padding-top-resizer')) {
if (!$element.data('azh-padding-top-resizer').is(':hover') && !$element.data('azh-padding-top-resizer').data('azh-drag')) {
$element.data('azh-padding-top-resizer').remove();
$element.data('azh-padding-top-resizer', false);
}
}
if ($element.data('azh-padding-bottom-resizer')) {
if (!$element.data('azh-padding-bottom-resizer').is(':hover') && !$element.data('azh-padding-bottom-resizer').data('azh-drag')) {
$element.data('azh-padding-bottom-resizer').remove();
$element.data('azh-padding-bottom-resizer', false);
}
}
if ($element.data('azh-height-resizer')) {
if (!$element.data('azh-height-resizer').is(':hover') && !$element.data('azh-height-resizer').data('azh-drag')) {
$element.data('azh-height-resizer').remove();
$element.data('azh-height-resizer', false);
}
}
});
});
});
$wrapper.find('.az-free-positioning').each(function () {
function rescale($free_position) {
if ($free_position.attr('data-height') && $free_position.attr('data-width') && $free_position.css('background-image') !== 'none') {
if ($free_position.css('background-size') === 'cover' || $free_position.css('background-size') === 'contain') {
var scale_width = $free_position.width() / parseInt($free_position.attr('data-width'), 10);
var scale_height = $free_position.height() / parseInt($free_position.attr('data-height'), 10);
if (scale_width !== 1 || scale_height !== 1) {
var img = new Image;
img.src = $free_position.css('background-image').replace(/url\(|'|"|\)$/ig, "");
img.onload = function() {
var background_width = img.width;
var background_height = img.height;
if (background_width && background_height) {
var old_scale_background_width = parseInt($free_position.attr('data-width'), 10) / background_width;
var old_scale_background_height = parseInt($free_position.attr('data-height'), 10) / background_height;
var new_scale_background_width = $free_position.width() / background_width;
var new_scale_background_height = $free_position.height() / background_height;
var background_scale = 1;
if ($free_position.css('background-size') === 'cover') {
var old_background_scale = 1;
if (old_scale_background_height > old_scale_background_width) {
old_background_scale = old_scale_background_height;
} else {
old_background_scale = old_scale_background_width;
}
var new_background_scale = 1;
if (new_scale_background_height > new_scale_background_width) {
new_background_scale = new_scale_background_height;
} else {
new_background_scale = new_scale_background_width;
}
background_scale = new_background_scale / old_background_scale;
}
if ($free_position.css('background-size') === 'contain') {
var old_background_scale = 1;
if (old_scale_background_height < old_scale_background_width) {
old_background_scale = old_scale_background_height;
} else {
old_background_scale = old_scale_background_width;
}
var new_background_scale = 1;
if (new_scale_background_height < new_scale_background_width) {
new_background_scale = new_scale_background_height;
} else {
new_background_scale = new_scale_background_width;
}
background_scale = new_background_scale / old_background_scale;
}
$free_position.find('> [data-element], > .az-elements-list > [data-element]').each(function () {
make_scale(azh.$(this), background_scale);
});
}
}
}
}
}
}
var $free_position = azh.$(this);
$free_position.closest('[data-element]').on('drag dragstart dragenter dragover dragleave drop dragend', function (e) {
e.stopPropagation();
return false;
});
$window.one('azh-set-device-width', function () {
var $full_width = $free_position.closest('[data-full-width="true"]');
if ($full_width.length) {
setTimeout(function () {
if($full_width.is('.az-full-width')) {
rescale($free_position);
} else {
$full_width.one('az-full-width', function () {
rescale($free_position);
});
}
});
} else {
rescale($free_position);
}
});
if ($free_position.parents('.az-free-positioning').length && $free_position.parent().is('[data-element]')) {
add_unmerge_button($free_position.closest('[data-element]'));
} else {
enable_lasso($free_position);
width_height_resizer($free_position.closest('[data-element]'));
var $element_controls = $free_position.closest('[data-element]').data('azh-controls');
if (!$element_controls.find('.azh-add-element').length && $free_position.parent().is('[data-element]')) {
$('').on('click', function (event) {
var $this = $(this);
var $element_controls = $this.closest('.azh-element-controls');
var $group = $element_controls.data('azh-linked-element');
var $elements = closest_descendents($group, '[data-element]');
var $element_wrapper = $elements.last();
var $new_element = $element_wrapper;
if ($element_wrapper.children().length) {
$new_element = clone_for_new_element($element_wrapper);
$new_element.removeClass('az-group');
remove_from_stored_classes($new_element, 'az-group');
}
$new_element.trigger('click');
azh.change();
return false;
}).appendTo($element_controls);
}
}
});
$wrapper.find('.az-polygone').each(function () {
var $polygon = azh.$(this);
var points = $polygon.find('polygon').get(0).getAttribute('points').split(' ').filter(function (n) {
return n !== '';
});
if (points.length === 0) {
$polygon.addClass('azh-adding');
}
$polygon.on('azh-clone', function () {
azh.$(this).data('azh-polygone-handlers', false);
});
$polygon.find('svg').on('mouseenter', function (event) {
function store_points() {
var points = [];
if ($polygon.data('azh-polygone-handlers')) {
var $handlers = $polygon.data('azh-polygone-handlers');
$handlers.find('.azh-handler').each(function () {
points.push($(this).data('azh-x') + ',' + $(this).data('azh-y'));
});
$polygon.find('polygon').get(0).setAttribute('points', points.join(' '));
set_stored_attribute($polygon.find('polygon'), 'points', points.join(' '));
}
}
function init_handler($handler) {
$handler.on('contextmenu', function (event) {
$handler.remove();
$polygon.data('azh-current-handler', false);
store_points();
return false;
}).draggable({
start: function (event, ui) {
azh.body.css('cursor', 'none');
scrollTop = azh.window.scrollTop();
},
stop: function (event, ui) {
azh.body.css('cursor', 'auto');
azh.window.scrollTop(scrollTop);
$polygon.data('azh-current-handler', $(ui.helper));
azh.document_off('keydown.azh-free-positioning-move');
},
drag: function (event, ui) {
azh.window.scrollTop(scrollTop);
var x = (ui.position.left + 5) / svg_width * viewbox_width;
var y = (ui.position.top + 5) / svg_height * viewbox_height;
if (x < 0) {
x = 0;
}
if (x > viewbox_width) {
x = viewbox_width;
}
if (y < 0) {
y = 0;
}
if (y > viewbox_height) {
y = viewbox_height;
}
$(ui.helper).data('azh-x', x);
$(ui.helper).data('azh-y', y);
$polygon.data('azh-current-handler', $(ui.helper));
store_points();
}
});
}
function refresh_handlers($handlers, drag) {
var $free_position = $polygon.closest('.az-free-positioning');
$free_position.css({
transform: 'translate(' + $free_position.data('azh-shift-x') + 'px,' + $free_position.data('azh-shift-y') + 'px) scale(' + $free_position.data('azh-scale') + ')'
});
var $element = $free_position.closest('[data-element]');
var element_rect = $element.get(0).getBoundingClientRect();
var svg_rect = $svg.get(0).getBoundingClientRect();
svg_width = svg_rect.width;
svg_height = svg_rect.height;
if (!drag) {
$handlers.css({
left: svg_rect.left,
top: svg_rect.top + azh.window.scrollTop(),
width: svg_rect.width,
height: svg_rect.height
});
}
$handlers.find('.azh-handler').each(function () {
var $handler = $(this);
$handler.css({
left: (parseFloat($handler.data('azh-x')) / viewbox_width * svg_width - 5) + 'px',
top: (parseFloat($handler.data('azh-y')) / viewbox_height * svg_height - 5) + 'px',
position: 'absolute'
});
$handler.show();
// if (svg_rect.top + $handler.position().top < element_rect.top) {
// $handler.hide();
// }
// if (svg_rect.left + $handler.position().left < element_rect.left) {
// $handler.hide();
// }
// if (svg_rect.top + $handler.position().top > element_rect.top + element_rect.height) {
// $handler.hide();
// }
// if (svg_rect.left + $handler.position().left > element_rect.left + element_rect.width) {
// $handler.hide();
// }
});
}
function distance($handler_before, $handler_after, offsetX, offsetY) {
var x0 = offsetX;
var y0 = offsetY;
var x1 = parseInt($handler_before.css('left'), 10);
var x2 = parseInt($handler_after.css('left'), 10);
var y1 = parseInt($handler_before.css('top'), 10);
var y2 = parseInt($handler_after.css('top'), 10);
var a = Math.pow(x0 - x1, 2) + Math.pow(y0 - y1, 2);
var b = Math.pow(x0 - x2, 2) + Math.pow(y0 - y2, 2);
var c = Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2);
var s = Math.abs((x0 - x2) * (y1 - y2) - (x1 - x2) * (y0 - y2)) / 2;
var h = 2 * s / Math.sqrt(c);
if ((a + c < b) || (b + c < a)) {
if (a < b) {
return Math.sqrt(a);
} else {
return Math.sqrt(b);
}
} else {
return h;
}
}
function get_closest_handlers_pair($handlers, offsetX, offsetY) {
var $handler_before = $handlers.children().first();
var $handler_after = $handler_before.next();
var min = distance($handler_before, $handler_after, offsetX, offsetY);
$handlers.children().each(function () {
var $before = $(this);
var $next = $before.next();
if ($next.length === 0) {
$next = $handlers.children().first();
}
var d = distance($before, $next, offsetX, offsetY);
if (min > d) {
$handler_before = $before;
$handler_after = $next;
min = d;
}
});
return {
'handler_before': $handler_before,
'handler_after': $handler_after,
'distance': min
};
}
if (event.which === 0) {
var scrollTop = azh.window.scrollTop();
var $svg = azh.$(this);
var $polygon = $svg.closest('.az-polygone');
var $element = $svg.closest('[data-element]');
if (!$element.data('azh-transformer') || !$element.data('azh-transformer').is('.azh-editing')) {
return;
}
var $free_position = $polygon.closest('.az-free-positioning');
$free_position.find('[data-element].azh-hover').each(function () {
var $element = azh.$(this);
var $polygon = $element.find('.az-polygone');
if ($polygon.length && $polygon.data('azh-polygone-handlers')) {
$polygon.data('azh-polygone-handlers').triggerHandler('mouseleave');
}
});
$polygon.closest('[data-element]').addClass('azh-hover');
setTimeout(function () {
$polygon.closest('[data-element]').triggerHandler(azh.$.Event('mouseenter', {which: 0}));
});
$polygon.addClass('az-hover');
var svg_rect = $svg.get(0).getBoundingClientRect();
var svg_width = svg_rect.width;
var svg_height = svg_rect.height;
var viewbox = $svg.get(0).getAttribute('viewBox').split(' ');
var viewbox_width = viewbox[2];
var viewbox_height = viewbox[3];
var points = $polygon.find('polygon').get(0).getAttribute('points').split(' ').filter(function (n) {
return n !== '';
});
if (!$polygon.data('azh-polygone-handlers')) {
var $handlers = azh.controls_container.find('.azh-polygone-handlers');
if ($handlers.length) {
azh.document_off('keydown.azh-polygone-handler-move');
$handlers.data('azh-polygone').removeClass('az-hover').data('azh-polygone-handlers', false);
$handlers.remove();
}
$handlers = $('').prependTo(azh.controls_container).on('mouseleave', function (event) {
var $this = $(this);
var $element = $this.data('azh-polygone').closest('[data-element]');
$this.data('azh-polygone').removeClass('az-hover').data('azh-polygone-handlers', false);
azh.document_off('keydown.azh-polygone-handler-move');
$this.remove();
$element.removeClass('azh-hover');
$element.triggerHandler(azh.$.Event('mouseleave', {which: 0}));
}).on('mousewheel', function (e) {
var delta = 1.2;
var $handlers = $(this);
var $polygon = $handlers.data('azh-polygone');
var $free_position = $polygon.closest('.az-free-positioning');
$free_position.addClass('azh-zoom');
azh.controls_container.addClass('azh-zooming');
var scale = Math.pow(delta, e.originalEvent.wheelDelta / 120);
var old_scale = ($free_position.data('azh-scale') ? $free_position.data('azh-scale') : 1);
var x = $free_position.data('azh-shift-x') ? $free_position.data('azh-shift-x') : 0;
var y = $free_position.data('azh-shift-y') ? $free_position.data('azh-shift-y') : 0;
var $element = $polygon.closest('[data-element]');
var element_top = parseFloat($element.css('top'));
if ($element.is('.az-middle')) {
element_top = element_top - $element.height() / 2;
}
var element_left = parseFloat($element.css('left'));
if ($element.is('.az-center')) {
element_left = element_left - $element.width() / 2;
}
var center_shift_x = $free_position.width() / 2 - (element_left + parseInt($polygon.css('padding-left'), 10) + e.offsetX / old_scale);
var center_shift_y = $free_position.height() / 2 - (element_top + parseInt($polygon.css('padding-top'), 10) + e.offsetY / old_scale);
x = x + center_shift_x * old_scale * (scale - 1);
y = y + center_shift_y * old_scale * (scale - 1);
scale = scale * old_scale;
if (scale < 1) {
scale = 1;
x = 0;
y = 0;
$free_position.removeClass('azh-zoom');
azh.controls_container.removeClass('azh-zooming');
}
$free_position.data('azh-scale', scale);
$free_position.data('azh-shift-x', x);
$free_position.data('azh-shift-y', y);
refresh_handlers($handlers);
$element.triggerHandler(azh.$.Event('mouseleave', {which: 0}));
$element.removeClass('azh-hover');
setTimeout(function () {
$element.triggerHandler(azh.$.Event('mouseenter', {which: 0}));
}, 100);
return false;
}).on('mousemove', function (event) {
var pair = get_closest_handlers_pair($handlers, event.offsetX, event.offsetY);
if (pair.distance < 10) {
$handlers.css('cursor', 'pointer');
} else {
$handlers.css('cursor', 'move');
}
}).on('contextmenu', function (event) {
if ($free_position.data('azh-scale') > 1 || $free_position.data('azh-shift-x') > 0 || $free_position.data('azh-shift-y') > 0) {
$free_position.data('azh-shift-x', 0).data('azh-shift-y', 0).data('azh-scale', 1);
refresh_handlers($handlers);
$free_position.removeClass('azh-zoom');
azh.controls_container.removeClass('azh-zooming');
} else {
var $element = $polygon.closest('[data-element]');
open_context_menu(event, $element);
}
return false;
}).draggable({
start: function (event, ui) {
scrollTop = azh.window.scrollTop();
$handlers.data('azh-dragging', true);
$(ui.helper).data('azh-start-top', ui.position.top);
$(ui.helper).data('azh-start-left', ui.position.left);
var $polygon = $handlers.data('azh-polygone');
var $free_position = $polygon.closest('.az-free-positioning');
$(ui.helper).data('azh-start-x', $free_position.data('azh-shift-x') ? $free_position.data('azh-shift-x') : 0);
$(ui.helper).data('azh-start-y', $free_position.data('azh-shift-y') ? $free_position.data('azh-shift-y') : 0);
},
stop: function (event, ui) {
azh.window.scrollTop(scrollTop);
setTimeout(function () {
$handlers.data('azh-dragging', false);
});
},
drag: function (event, ui) {
azh.window.scrollTop(scrollTop);
var $polygon = $handlers.data('azh-polygone');
var $free_position = $polygon.closest('.az-free-positioning');
var scale = $free_position.data('azh-scale') ? $free_position.data('azh-scale') : 1;
var x = $(ui.helper).data('azh-start-x') + ui.position.left - $(ui.helper).data('azh-start-left');
var y = $(ui.helper).data('azh-start-y') + ui.position.top - $(ui.helper).data('azh-start-top');
$free_position.data('azh-shift-x', x).data('azh-shift-y', y).data('azh-scale', scale);
refresh_handlers($handlers, true);
var $element = $polygon.closest('[data-element]');
$element.triggerHandler(azh.$.Event('mouseleave', {which: 0}));
$element.removeClass('azh-hover');
setTimeout(function () {
$element.triggerHandler(azh.$.Event('mouseenter', {which: 0}));
}, 100);
}
});
$handlers.data('azh-polygone', $polygon);
$polygon.data('azh-polygone-handlers', $handlers);
for (var i = 0; i < points.length; i++) {
var $handler = $('').appendTo($handlers);
$handler.data('azh-x', parseFloat(points[i].split(',')[0]));
$handler.data('azh-y', parseFloat(points[i].split(',')[1]));
init_handler($handler);
}
refresh_handlers($handlers);
}
var $handlers = $polygon.data('azh-polygone-handlers');
if (points.length) {
$handlers.off('click').on('click', function (event) {
if (!$handlers.data('azh-dragging')) {
var pair = get_closest_handlers_pair($handlers, event.offsetX, event.offsetY);
if (pair.distance < 10) {
var $handler = $('').insertAfter(pair.handler_before).css({
left: (event.offsetX - 5) + 'px',
top: (event.offsetY - 5) + 'px',
position: 'absolute'
}).data('azh-x', (event.offsetX) / svg_width * viewbox_width).data('azh-y', (event.offsetY) / svg_height * viewbox_height);
init_handler($handler);
$polygon.data('azh-current-handler', $handler);
azh.document_off('keydown.azh-free-positioning-move');
store_points();
} else {
var $element = $polygon.closest('[data-element]');
$element.trigger('click');
var $element_controls = $element.data('azh-controls');
if ($element_controls) {
$element_controls.find('.azh-utility-wrapper').trigger('click');
}
}
}
return false;
});
} else {
$polygon.addClass('azh-adding');
$handlers.off('click').on('click', function (event) {
if (!$handlers.data('azh-dragging')) {
var $handler = $('').appendTo($handlers).css({
left: (event.offsetX - 5) + 'px',
top: (event.offsetY - 5) + 'px',
position: 'absolute'
}).data('azh-x', (event.offsetX) / svg_width * viewbox_width).data('azh-y', (event.offsetY) / svg_height * viewbox_height);
init_handler($handler);
$polygon.data('azh-current-handler', $handler);
azh.document_off('keydown.azh-free-positioning-move');
store_points();
}
return false;
});
azh.document.on('click.azh-polygon', function (event) {
var $handlers = azh.$('.azh-adding').removeClass('azh-adding').data('azh-polygone-handlers');
if ($handlers) {
$handlers.off('click');
}
azh.document.off('click.azh-polygon');
});
}
$polygon.data('azh-current-handler', false);
azh.document_off('keydown.azh-polygone-handler-move').document_on('keydown.azh-polygone-handler-move', function (event) {
var $current_handler = $polygon.data('azh-current-handler');
if ($current_handler) {
var $free_position = $polygon.closest('.az-free-positioning');
var scale = $free_position.data('azh-scale') ? $free_position.data('azh-scale') : 1;
var left = parseInt($current_handler.css('left'), 10);
var top = parseInt($current_handler.css('top'), 10);
switch ((event.keyCode ? event.keyCode : event.which)) {
//case 13: // Enter
//case 27: // Esc
//case 32: // Space
case 37: // Left Arrow
left--;
event.preventDefault();
break;
case 38: // Up Arrow
top--;
event.preventDefault();
break;
case 39: // Right Arrow
left++;
event.preventDefault();
break;
case 40: // Down Arrow
top++;
event.preventDefault();
break;
}
$current_handler.css('left', left + 'px');
$current_handler.css('top', top + 'px');
var x = (left + 5) / $polygon.find('svg').width() * viewbox_width / scale;
var y = (top + 5) / $polygon.find('svg').height() * viewbox_height / scale;
if (x < 0) {
x = 0;
}
if (x > viewbox_width) {
x = viewbox_width;
}
if (y < 0) {
y = 0;
}
if (y > viewbox_height) {
y = viewbox_height;
}
$current_handler.data('azh-x', x);
$current_handler.data('azh-y', y);
store_points();
}
});
}
});
});
$wrapper.find('form button').on('keypress keydown', function (event) {
if (event.keyCode == 32) {
azh.document.get(0).execCommand("insertHTML", false, " ");
}
}).on('keypress keydown keyup', function (event) {
if (event.keyCode == 13 || event.keyCode == 32) {
event.preventDefault();
}
}).on('click', function (event) {
event.preventDefault();
});
$wrapper.find('.az-editable-html').each(function () {
var $editable_html = azh.$(this);
var $element_controls = $editable_html.closest('[data-element]').data('azh-controls');
if (!$element_controls.find('.azh-html-edit').length) {
$('').on('click', function (event) {
var $this = $(this);
var $element_controls = $this.closest('.azh-element-controls');
var $element = $element_controls.data('azh-linked-element');
var $editable_html = $element.find('.az-editable-html');
var $html = $editable_html.clone(true);
azh.liquid_prepare($html);
var html = extract_html($html, true);
html = make_html_unsafe(html);
open_html_editor_modal({
"title": azh.i18n.edit_element_html,
"desc": "",
"label": azh.i18n.source_code
}, html_beautify(html), function (source_code) {
source_code = html_uglify(source_code);
if (source_code !== false) {
source_code = make_html_safe(source_code);
$editable_html.html(source_code);
store_html($editable_html.get(0));
customization_init($editable_html);
if (azh.frontend_init) {
azh.frontend_init($editable_html);
}
return true;
} else {
alert(azh.i18n.html_is_not_valid);
return false;
}
});
}).prependTo($element_controls);
}
});
generate_modal_buttons($wrapper);
//generate_controls($wrapper);
$wrapper.addClass('azh-deferred-context-controls');
if ($wrapper.is('[data-element], [data-section]')) {
get_utility($wrapper);
//generate_modal_buttons($wrapper, get_utility($wrapper));
//generate_controls($wrapper, get_utility($wrapper));
$wrapper.find('[data-element]:not([data-element=" "], [data-element=""]), [class*="azh-col-"]').each(function () {
get_utility(azh.$(this));
});
$wrapper.find('[data-cloneable], [data-cloneable-inline]').each(function () {
var $cloneable = azh.$(this);
if (is_section_refresh($cloneable)) {
var $element = $cloneable.closest('[data-element]');
if ($element.length) {
get_utility($element);
//generate_modal_buttons($element, get_utility($element));
//generate_controls($element, get_utility($element));
}
}
});
if (azh.fill_utility_on_init.length) {
$wrapper.find(azh.fill_utility_on_init.join(',')).each(function () {
var $section_or_element = azh.$(this).closest('[data-element], [data-section]');
fill_utility($section_or_element, get_utility($section_or_element), true);
});
}
}
// azh.controls_container.find('.azh-utility .azh-controls-list').each(function () {
// elements_sort($(this), ':not(.azh-group):not(.azh-subgroup):not(.azh-cloneable-group):not(.azh-utility-title):not(.azh-context-menu-title)');
// group_controls($(this));
// });
azh.controls_container.appendTo($body);
wrapper_restore($wrapper);
_.defer(refresh_elements_hierarchy_partial, $wrapper);
azh.window.trigger("azh-customization-after-init", {
wrapper: $wrapper
});
};
var refresh_section_linked_ids = function ($wrapper) {
var ids = {};
var id_attributes = ['id', 'for'];
var hash_attributes = ['href', 'data-target', 'data-id'];
$(id_attributes).each(function () {
var id_attribute = this;
$wrapper.find('[' + id_attribute + ']').each(function () {
var id = azh.$(this).attr(id_attribute);
if (!(id in ids)) {
ids[id] = [];
}
azh.$(this).addClass('azh-id-attr');
azh.$(this).data('azh-id-attr', id_attribute);
ids[id].push(azh.$(this));
});
});
$(hash_attributes).each(function () {
var hash_attribute = this;
$wrapper.find('[' + hash_attribute + '^="#"]').each(function () {
var id = azh.$(this).attr(hash_attribute).replace('#', '');
if ($.trim(id)) {
if (!(id in ids)) {
ids[id] = [];
}
azh.$(this).addClass('azh-hash-attr');
azh.$(this).data('azh-hash-attr', hash_attribute);
ids[id].push(azh.$(this));
}
});
});
var linked_ids = {};
for (var id in ids) {
if (ids[id].length > 1) {
linked_ids[id] = ids[id];
}
}
$wrapper.data('azh-linked-ids', linked_ids);
};
azh.section_customization_init = function ($wrapper) {
if (azh.recognition) {
recognition($wrapper);
}
azh.controls_container = false;
if ($('.azh-controls-container').length) {
azh.controls_container = $('.azh-controls-container');
} else {
azh.controls_container = $('').appendTo($body);
}
var $section_controls = $wrapper.data('azh-controls');
if (!$section_controls) {
$section_controls = $('').appendTo(azh.controls_container).on('mouseenter', function () {
$(this).data('azh-linked-element').addClass('azh-over');
}).on('mouseleave', function () {
$(this).data('azh-linked-element').removeClass('azh-over');
}).data('azh-linked-element', $wrapper);
$wrapper.data('azh-controls', $section_controls);
$section_controls.data('azh-linked-element', $wrapper);
}
$section_controls.hide();
$wrapper.addClass('azh-controls');
$wrapper.off('mouseenter.azh-section').on('mouseenter.azh-section', function (event) {
if (azh.controls_container.find('.azh-section-controls.azh-active').length == 0) {
var $wrapper = azh.$(this);
$section_controls.css('display', '');
$section_controls.css('right', (get_full_width() - $wrapper.offset().left - $wrapper.outerWidth()) + 'px');
$section_controls.css('top', $wrapper.offset().top + 'px');
}
}).off('mousemove.azh-section').on('mousemove.azh-section', function () {
var $wrapper = azh.$(this);
$section_controls.css('right', (get_full_width() - $wrapper.offset().left - $wrapper.outerWidth()) + 'px');
$section_controls.css('top', $wrapper.offset().top + 'px');
}).off('mouseleave.azh-section').on('mouseleave.azh-section', function () {
var $wrapper = azh.$(this);
setTimeout(function () {
if (!$section_controls.is(':hover') && azh.controls_container.find('.azh-resizer:hover').length == 0) {
$section_controls.hide();
} else {
$section_controls.on('mouseleave', function () {
$section_controls.hide();
});
}
});
});
refresh_section_linked_ids($wrapper);
customization_init($wrapper);
if (!$section_controls.find('.azh-html-edit').length) {
$('').on('click', function (event) {
var $this = $(this);
var $section = $this.closest('.azh-section-controls').data('azh-linked-element');
var $html = $section.clone(true);
azh.liquid_prepare($html);
var html = extract_html($html.wrap('').parent(), true);
html = make_html_unsafe(html);
open_html_editor_modal({
"title": azh.i18n.edit_section_html,
"desc": "",
"label": azh.i18n.source_code
}, html_beautify(html), function (source_code) {
source_code = html_uglify(source_code);
if (source_code !== false) {
source_code = make_html_safe(source_code);
section_refresh($section, html_uglify(source_code));
return true;
} else {
alert(azh.i18n.html_is_not_valid);
return false;
}
});
}).prependTo($section_controls);
}
};
var focus = function ($target, duration) {
var focus_padding = 0;
if ($('.azh-focus').length == 0) {
$('
').appendTo($body).on('click', function () {
$('.azh-focus').remove();
return false;
});
$('.azh-focus .top, .azh-focus .right, .azh-focus .bottom, .azh-focus .left').css({
'z-index': '50',
'position': 'fixed',
'background-color': 'black',
'opacity': '0.4'
});
}
var $top = $('.azh-focus .top');
var $right = $('.azh-focus .right');
var $bottom = $('.azh-focus .bottom');
var $left = $('.azh-focus .left');
var target_top = $target.offset()['top'] - focus_padding;
var target_left = $target.offset()['left'] - focus_padding;
if (azh.body.has($target).length) {
if ($window.get(0) != azh.window.get(0)) {
target_top = target_top - azh.window.scrollTop();
} else {
target_top = target_top - azh.body.scrollTop();
}
if (azh.device_left) {
target_left = target_left + azh.device_left;
}
}
var target_width = $target.outerWidth() + focus_padding * 2;
var target_height = $target.outerHeight() + focus_padding * 2;
$top.stop().animate({
top: 0,
left: 0,
right: 0,
height: target_top,
}, duration, 'linear');
$right.stop().animate({
top: target_top,
left: target_left + target_width,
right: 0,
height: target_height,
}, duration, 'linear');
$bottom.stop().animate({
top: target_top + target_height,
left: 0,
right: 0,
bottom: 0,
}, duration, 'linear');
$left.stop().animate({
top: target_top,
left: 0,
height: target_height,
width: target_left,
}, duration, 'linear', function () {
});
if (duration > 0) {
setTimeout(function () {
$window.on('scroll.focus', function () {
$('.azh-focus').remove();
$window.off('scroll.focus');
});
$('.azh-focus .top, .azh-focus .right, .azh-focus .bottom, .azh-focus .left').stop().animate({
'opacity': '0'
}, duration * 10);
setTimeout(function () {
$window.trigger('scroll');
}, duration * 10);
}, duration);
}
};
function scroll_and_focus($element) {
if ($window.get(0) != azh.window.get(0)) {
azh.window.scrollTop($element.offset().top - $window.height() / 2 + $element.height() / 2);
} else {
azh.body.stop().animate({'scrollTop': $element.offset().top - azh.window.height() / 2 + $element.height() / 2
}, 300);
}
setTimeout(function () {
azh.scroll_top = azh.window.scrollTop();
azh.controls_container.css('top', (-azh.window.scrollTop()) + 'px');
$('').appendTo($body);
focus($('.azh-overlay'), 0);
setTimeout(function () {
$('.azh-overlay').remove();
focus($element, 300);
}, 0);
}, 300);
}
function reset_free_positioning($element) {
$element.css({
left: '',
right: '',
top: '',
bottom: '',
transform: ''
});
set_stored_style($element, 'left', '');
set_stored_style($element, 'right', '');
set_stored_style($element, 'top', '');
set_stored_style($element, 'bottom', '');
set_stored_style($element, 'transform', '');
set_stored_style($element, 'height', $element.height() + 'px');
set_stored_style($element, 'width', $element.width() + 'px');
}
function get_v_alignment($element) {
if ($element.is('.az-top')) {
return 'top';
}
if ($element.is('.az-middle')) {
return 'middle';
}
if ($element.is('.az-bottom')) {
return 'bottom';
}
}
function get_h_alignment($element) {
if ($element.is('.az-left')) {
return 'left';
}
if ($element.is('.az-center')) {
return 'center';
}
if ($element.is('.az-right')) {
return 'right';
}
}
function get_top($element) {
var p = 0;
if ($element.get(0).style.top) {
p = $element.get(0).style.top;
p = parseInt(p);
}
return p;
}
function get_middle($element) {
var p = 0;
if ($element.get(0).style.top) {
p = $element.get(0).style.top;
p = /calc\(50% ([-+]) ([\d\.]+)px\)/gi.exec(p);
if (p) {
p = parseInt(p[1] + p[2]);
} else {
p = 0;
}
}
return p;
}
function get_bottom($element) {
var p = 0;
if ($element.get(0).style.bottom) {
p = $element.get(0).style.bottom;
p = parseInt(p);
}
return p;
}
function get_left($element) {
var p = 0;
if ($element.get(0).style.left) {
p = $element.get(0).style.left;
p = parseInt(p);
}
return p;
}
function get_center($element) {
var p = 0;
if ($element.get(0).style.left) {
p = $element.get(0).style.left;
p = /calc\(50% ([-+]) ([\d\.]+)px\)/gi.exec(p);
if (p) {
p = parseInt(p[1] + p[2]);
} else {
p = 0;
}
}
return p;
}
function get_right($element) {
var p = 0;
if ($element.get(0).style.right) {
p = $element.get(0).style.right;
p = parseInt(p);
}
return p;
}
function set_width($element, p) {
$element.css('width', p + 'px');
set_stored_style($element, 'width', p + 'px');
}
function set_height($element, p) {
$element.css('height', p + 'px');
set_stored_style($element, 'height', p + 'px');
}
function set_top($element, p) {
$element.css('top', p + 'px');
set_stored_style($element, 'top', p + 'px');
}
function set_middle($element, p) {
if (p >= 0) {
$element.css('top', 'calc(50% + ' + p + 'px)');
set_stored_style($element, 'top', 'calc(50% + ' + p + 'px)');
} else {
$element.css('top', 'calc(50% - ' + Math.abs(p) + 'px)');
set_stored_style($element, 'top', 'calc(50% - ' + Math.abs(p) + 'px)');
}
}
function set_bottom($element, p) {
$element.css('bottom', p + 'px');
set_stored_style($element, 'bottom', p + 'px');
}
function set_left($element, p) {
$element.css('left', p + 'px');
set_stored_style($element, 'left', p + 'px');
}
function set_center($element, p) {
if (p >= 0) {
$element.css('left', 'calc(50% + ' + p + 'px)');
set_stored_style($element, 'left', 'calc(50% + ' + p + 'px)');
} else {
$element.css('left', 'calc(50% - ' + Math.abs(p) + 'px)');
set_stored_style($element, 'left', 'calc(50% - ' + Math.abs(p) + 'px)');
}
}
function set_right($element, p) {
$element.css('right', p + 'px');
set_stored_style($element, 'right', p + 'px');
}
function set_top_alignment($element) {
$element.css('bottom', '');
set_stored_style($element, 'bottom', '');
$element.removeClass('az-middle');
remove_from_stored_classes($element, 'az-middle');
$element.removeClass('az-bottom');
remove_from_stored_classes($element, 'az-bottom');
add_to_stored_classes($element, 'az-top');
$element.addClass('az-top');
}
function set_middle_alignment($element) {
$element.css('bottom', '');
set_stored_style($element, 'bottom', '');
$element.removeClass('az-top');
remove_from_stored_classes($element, 'az-top');
$element.removeClass('az-bottom');
remove_from_stored_classes($element, 'az-bottom');
add_to_stored_classes($element, 'az-middle');
$element.addClass('az-middle');
}
function set_bottom_alignment($element) {
$element.css('top', '');
set_stored_style($element, 'top', '');
$element.removeClass('az-top');
remove_from_stored_classes($element, 'az-top');
$element.removeClass('az-middle');
remove_from_stored_classes($element, 'az-middle');
add_to_stored_classes($element, 'az-bottom');
$element.addClass('az-bottom');
}
function set_left_alignment($element) {
$element.css('right', '');
set_stored_style($element, 'right', '');
$element.removeClass('az-right');
remove_from_stored_classes($element, 'az-right');
$element.removeClass('az-center');
remove_from_stored_classes($element, 'az-center');
add_to_stored_classes($element, 'az-left');
$element.addClass('az-left');
}
function set_center_alignment($element) {
$element.css('right', '');
set_stored_style($element, 'right', '');
$element.removeClass('az-right');
remove_from_stored_classes($element, 'az-right');
$element.removeClass('az-left');
remove_from_stored_classes($element, 'az-left');
add_to_stored_classes($element, 'az-center');
$element.addClass('az-center');
}
function set_right_alignment($element) {
$element.css('left', '');
set_stored_style($element, 'left', '');
$element.removeClass('az-center');
remove_from_stored_classes($element, 'az-center');
$element.removeClass('az-left');
remove_from_stored_classes($element, 'az-left');
add_to_stored_classes($element, 'az-right');
$element.addClass('az-right');
}
function make_v_scale($element, scale) {
var v_alignment = get_v_alignment($element);
switch (v_alignment) {
case 'top':
set_top($element, get_top($element) * scale);
break;
case 'middle':
set_middle($element, get_middle($element) * scale);
break;
case 'bottom':
set_bottom($element, get_bottom($element) * scale);
break;
}
var h = $element.height();
$element.css('height', h * scale + 'px');
set_stored_style($element, 'height', h * scale + 'px');
}
function make_h_scale($element, scale) {
var h_alignment = get_h_alignment($element);
switch (h_alignment) {
case 'left':
set_left($element, get_left($element) * scale);
break;
case 'center':
set_center($element, get_center($element) * scale);
break;
case 'right':
set_right($element, get_right($element) * scale);
break;
}
var w = $element.width();
$element.css('width', w * scale + 'px');
set_stored_style($element, 'width', w * scale + 'px');
}
function make_scale($element, scale) {
make_v_scale($element, scale);
make_h_scale($element, scale);
}
function change_v_alignment($element, new_v_alignment) {
var v_alignment = get_v_alignment($element);
switch (v_alignment) {
case 'top':
var p = get_top($element);
switch (new_v_alignment) {
case 'middle':
p = p - $element.closest('.az-free-positioning').height() / 2 + $element.height() / 2;
set_middle_alignment($element);
set_middle($element, Math.round(p));
break;
case 'bottom':
p = -p + $element.closest('.az-free-positioning').height() - $element.height();
set_bottom_alignment($element);
set_bottom($element, Math.round(p));
break;
}
break;
case 'middle':
var p = get_middle($element);
switch (new_v_alignment) {
case 'top':
p = p + $element.closest('.az-free-positioning').height() / 2 - $element.height() / 2;
set_top_alignment($element);
set_top($element, Math.round(p));
break;
case 'bottom':
p = -p + $element.closest('.az-free-positioning').height() / 2 - $element.height() / 2;
set_bottom_alignment($element);
set_bottom($element, Math.round(p));
break;
}
break;
case 'bottom':
var p = get_bottom($element);
switch (new_v_alignment) {
case 'top':
p = -p + $element.closest('.az-free-positioning').height() - $element.height();
set_top_alignment($element);
set_top($element, Math.round(p));
break;
case 'middle':
p = -p + $element.closest('.az-free-positioning').height() / 2 - $element.height() / 2;
set_middle_alignment($element);
set_middle($element, Math.round(p));
break;
}
break;
}
}
function change_h_alignment($element, new_h_alignment) {
var h_alignment = get_h_alignment($element);
switch (h_alignment) {
case 'left':
var p = get_left($element);
switch (new_h_alignment) {
case 'center':
p = p - $element.closest('.az-free-positioning').width() / 2 + $element.width() / 2;
set_center_alignment($element);
set_center($element, Math.round(p));
break;
case 'right':
p = -p + $element.closest('.az-free-positioning').width() - $element.width();
set_right_alignment($element);
set_right($element, Math.round(p));
break;
}
break;
case 'center':
var p = get_center($element);
switch (new_h_alignment) {
case 'left':
p = p + $element.closest('.az-free-positioning').width() / 2 - $element.width() / 2;
set_left_alignment($element);
set_left($element, Math.round(p));
break;
case 'right':
p = -p + $element.closest('.az-free-positioning').width() / 2 - $element.width() / 2;
set_right_alignment($element);
set_right($element, Math.round(p));
break;
}
break;
case 'right':
var p = get_right($element);
switch (new_h_alignment) {
case 'left':
p = -p + $element.closest('.az-free-positioning').width() - $element.width();
set_left_alignment($element);
set_left($element, Math.round(p));
break;
case 'center':
p = -p + $element.closest('.az-free-positioning').width() / 2 - $element.width() / 2;
set_center_alignment($element);
set_center($element, Math.round(p));
break;
}
break;
}
}
function align_points(points) {
var med = 0;
var m = 0;
do {
med = m;
var points_with_index = [];
for (var i = 0; i < points.length; i++) {
points_with_index.push([points[i], i]);
}
points_with_index.sort(function (left, right) {
return left[0] < right[0] ? -1 : 1;
});
var index = [];
for (var i = 0; i < points_with_index.length; i++) {
index.push(points_with_index[i][1]);
}
var centers = [];
for (var i = 1; i < points.length; i++) {
centers.push(points[index[i - 1]] + (points[index[i]] - points[index[i - 1]]) / 2);
}
var diff = [];
diff.push(centers[0] - points[index[0]]);
for (var i = 1; i < points.length; i++) {
diff.push(points[index[i]] - centers[i - 1]);
}
m = 0;
for (var i = 0; i < diff.length; i++) {
m = m + diff[i];
}
m = m / diff.length;
points[index[0]] = centers[0] - m;
for (var i = 1; i < points.length; i++) {
points[index[i]] = centers[i - 1] + m;
}
} while (Math.abs(med - m) > 0.01);
return points;
}
function between_v_align($group, between_alignment) {
$group.each(function () {
var $element = azh.$(this);
change_v_alignment($element, between_alignment);
});
switch (between_alignment) {
case 'top':
var points = [];
$group.each(function () {
var $element = azh.$(this);
var alignment = get_v_alignment($element);
switch (alignment) {
case 'top':
points.push(get_top($element));
break;
case 'middle':
points.push((get_middle($element) - $element.height() / 2));
break;
case 'bottom':
points.push((get_bottom($element) - $element.height()));
break;
}
});
points = align_points(points);
$group.each(function (index) {
var $element = azh.$(this);
var alignment = get_v_alignment($element);
switch (alignment) {
case 'top':
var p = Math.round(points[index]);
set_top($element, p);
break;
case 'middle':
var p = Math.round(points[index] + $element.height() / 2);
set_middle($element, p);
break;
case 'bottom':
var p = Math.round(points[index] + $element.height());
set_bottom($element, p);
break;
}
});
break;
case 'middle':
var points = [];
$group.each(function () {
var $element = azh.$(this);
var alignment = get_v_alignment($element);
switch (alignment) {
case 'top':
points.push(get_top($element) + $element.height() / 2);
break;
case 'middle':
points.push((get_middle($element)));
break;
case 'bottom':
points.push((get_bottom($element) - $element.height() / 2));
break;
}
});
points = align_points(points);
$group.each(function (index) {
var $element = azh.$(this);
var alignment = get_v_alignment($element);
switch (alignment) {
case 'top':
var p = Math.round(points[index] - $element.height() / 2);
set_top($element, p);
break;
case 'middle':
var p = Math.round(points[index]);
set_middle($element, p);
break;
case 'bottom':
var p = Math.round(points[index] + $element.height() / 2);
set_bottom($element, p);
break;
}
});
break;
case 'bottom':
var points = [];
$group.each(function () {
var $element = azh.$(this);
var alignment = get_v_alignment($element);
switch (alignment) {
case 'top':
points.push(get_top($element) + $element.height());
break;
case 'middle':
points.push((get_middle($element) + $element.height() / 2));
break;
case 'bottom':
points.push((get_bottom($element)));
break;
}
});
points = align_points(points);
$group.each(function (index) {
var $element = azh.$(this);
var alignment = get_v_alignment($element);
switch (alignment) {
case 'top':
var p = Math.round(points[index] - $element.height());
set_top($element, p);
break;
case 'middle':
var p = Math.round(points[index] - $element.height() / 2);
set_middle($element, p);
break;
case 'bottom':
var p = Math.round(points[index]);
set_bottom($element, p);
break;
}
});
break;
}
}
function between_h_align($group, between_alignment) {
$group.each(function () {
var $element = azh.$(this);
change_h_alignment($element, between_alignment);
});
switch (between_alignment) {
case 'left':
var points = [];
$group.each(function () {
var $element = azh.$(this);
var alignment = get_h_alignment($element);
switch (alignment) {
case 'left':
points.push(get_left($element));
break;
case 'center':
points.push((get_center($element) - $element.width() / 2));
break;
case 'right':
points.push((get_right($element) - $element.width()));
break;
}
});
points = align_points(points);
$group.each(function (index) {
var $element = azh.$(this);
var alignment = get_h_alignment($element);
switch (alignment) {
case 'left':
var p = Math.round(points[index]);
set_left($element, p);
break;
case 'center':
var p = Math.round(points[index] + $element.width() / 2);
set_center($element, p);
break;
case 'right':
var p = Math.round(points[index] + $element.width());
set_right($element, p);
break;
}
});
break;
case 'center':
var points = [];
$group.each(function () {
var $element = azh.$(this);
var alignment = get_h_alignment($element);
switch (alignment) {
case 'left':
points.push(get_left($element) + $element.width() / 2);
break;
case 'center':
points.push((get_center($element)));
break;
case 'right':
points.push((get_right($element) - $element.width() / 2));
break;
}
});
points = align_points(points);
$group.each(function (index) {
var $element = azh.$(this);
var alignment = get_h_alignment($element);
switch (alignment) {
case 'left':
var p = Math.round(points[index] - $element.width() / 2);
set_left($element, p);
break;
case 'center':
var p = Math.round(points[index]);
set_center($element, p);
break;
case 'right':
var p = Math.round(points[index] + $element.width() / 2);
set_right($element, p);
break;
}
});
break;
case 'right':
var points = [];
$group.each(function () {
var $element = azh.$(this);
var alignment = get_h_alignment($element);
switch (alignment) {
case 'left':
points.push(get_left($element) + $element.width());
break;
case 'center':
points.push((get_center($element) + $element.width() / 2));
break;
case 'right':
points.push((get_right($element)));
break;
}
});
points = align_points(points);
$group.each(function (index) {
var $element = azh.$(this);
var alignment = get_h_alignment($element);
switch (alignment) {
case 'left':
var p = Math.round(points[index] - $element.width());
set_left($element, p);
break;
case 'center':
var p = Math.round(points[index] - $element.width() / 2);
set_center($element, p);
break;
case 'right':
var p = Math.round(points[index]);
set_right($element, p);
break;
}
});
break;
}
}
function v_align($group, alignment) {
$group.each(function () {
var $element = azh.$(this);
change_v_alignment($element, alignment);
});
switch (alignment) {
case 'top':
var min = get_top($group.first());
$group.each(function () {
if (min > get_top(azh.$(this))) {
min = get_top(azh.$(this));
}
});
$group.each(function () {
set_top(azh.$(this), Math.round(min));
});
break;
case 'middle':
var average = 0;
$group.each(function () {
average += get_middle(azh.$(this));
});
average = average / $group.length;
$group.each(function () {
set_middle(azh.$(this), Math.round(average));
});
break;
case 'bottom':
var min = get_bottom($group.first());
$group.each(function () {
if (min > get_bottom(azh.$(this))) {
min = get_bottom(azh.$(this));
}
});
$group.each(function () {
set_bottom(azh.$(this), Math.round(min));
});
break;
}
}
function h_align($group, alignment) {
$group.each(function () {
var $element = azh.$(this);
change_h_alignment($element, alignment);
});
switch (alignment) {
case 'left':
var min = get_left($group.first());
$group.each(function () {
if (min > get_left(azh.$(this))) {
min = get_left(azh.$(this));
}
});
$group.each(function () {
set_left(azh.$(this), Math.round(min));
});
break;
case 'center':
var average = 0;
$group.each(function () {
average += get_center(azh.$(this));
});
average = average / $group.length;
$group.each(function () {
set_center(azh.$(this), Math.round(average));
});
break;
case 'right':
var min = get_right($group.first());
$group.each(function () {
if (min > get_right(azh.$(this))) {
min = get_right(azh.$(this));
}
});
$group.each(function () {
set_right(azh.$(this), Math.round(min));
});
break;
}
}
function fill_elements_hierarchy($parent, $child) {
var section_or_element = 'data-section';
if (!$parent.attr(section_or_element)) {
section_or_element = 'data-element';
}
var path = $parent.attr(section_or_element);
$('' + $parent.attr(section_or_element) + '').prependTo($child).on('blur', function () {
$parent.attr(section_or_element, $(this).text());
set_stored_attribute($parent, section_or_element, $(this).text());
var $controls = $parent.data('azh-controls');
if ($controls) {
$controls.find('> .azh-name').text($(this).text());
}
}).on('mousedown', function (event) {
event.stopPropagation();
});
$child.data('azh-linked-element', $parent);
if ($parent.is('.azh-expand')) {
$child.addClass('azh-expand');
}
$parent.data('azh-linked-hierarchy-child', $child);
var path = $parent.attr(section_or_element);
var background_image = false;
var element = $();
if (section_or_element == 'data-section') {
element = $('.azh-library .azh-sections .azh-section[data-path="' + path + '"]');
} else {
element = $('.azh-library .azh-elements .azh-element[data-path="' + path + '"]');
}
if (element.length > 0) {
background_image = $(element).get(0).style['background-image'].replace('url(', '').replace(')', '').replace(/\'/gi, '').replace(/\"/gi, '');
}
if (!background_image || background_image == 'none') {
background_image = azh.plugin_url + '/images/box.png';
}
$child.prepend('');
var $visibility = $('').prependTo($child).on('click', function () {
var $visibility = $(this);
if ($(this).is('.azh-hidden')) {
$visibility.removeClass('azh-hidden');
$parent.show();
set_stored_style($parent, 'display', 'block');
} else {
$visibility.addClass('azh-hidden');
$parent.hide();
set_stored_style($parent, 'display', 'none');
}
return false;
});
if (!$parent.is(':visible')) {
$visibility.addClass('azh-hidden');
}
var $descendents = closest_descendents($parent, '[data-element]');
if ($descendents.length) {
var $parent_children = $('').appendTo($child);
$descendents.each(function () {
var $element = azh.$(this);
if ($element.attr('data-element') && $element.attr('data-element') != ' ') {
var $child = $('').appendTo($parent_children);
fill_elements_hierarchy($element, $child);
}
});
if ($descendents.parent().length === 1) {
$parent_children.sortable({
placeholder: 'azh-placeholder',
forcePlaceholderSize: true,
update: function (event, ui) {
var $element = $(ui.item).data('azh-linked-element');
$element.detach();
if ($(ui.item).next().length) {
var $next_element = $(ui.item).next().data('azh-linked-element');
$next_element.before($element);
} else {
if ($(ui.item).prev().length) {
var $prev_element = $(ui.item).prev().data('azh-linked-element');
$prev_element.after($element);
}
}
},
over: function (event, ui) {
ui.placeholder.attr('class', ui.helper.attr('class'));
ui.placeholder.removeClass('ui-sortable-helper');
ui.placeholder.attr('style', ui.helper.attr('style'));
ui.placeholder.css('position', 'relative');
ui.placeholder.css('z-index', 'auto');
ui.placeholder.css('left', 'auto');
ui.placeholder.css('top', 'auto');
ui.placeholder.addClass('azh-placeholder');
}
});
}
}
return $child;
}
function show_alignments($hierarchy, $group) {
function show_active_alignment() {
$alignment.find('.azh-active').removeClass('azh-active');
if ($group.is('.az-top')) {
$alignment.find('.azh-top-alignment').addClass('azh-active');
}
if ($group.is('.az-middle')) {
$alignment.find('.azh-middle-alignment').addClass('azh-active');
}
if ($group.is('.az-bottom')) {
$alignment.find('.azh-bottom-alignment').addClass('azh-active');
}
if ($group.is('.az-left')) {
$alignment.find('.azh-left-alignment').addClass('azh-active');
}
if ($group.is('.az-center')) {
$alignment.find('.azh-center-alignment').addClass('azh-active');
}
if ($group.is('.az-right')) {
$alignment.find('.azh-right-alignment').addClass('azh-active');
}
}
return;
var $title = $hierarchy.children('.azh-hierarchy-title');
$hierarchy.find('.azh-alignment').remove();
$hierarchy.find('.azh-root').css('height', 'calc(100% - 40px)');
if ($group.parent().is('.az-free-positioning') || $group.parent().parent().is('.az-free-positioning') || $group.css('position') === 'absolute') {
var $alignment = $('').insertAfter($title);
var $e_alignment = $('').appendTo($alignment);
$('').appendTo($e_alignment).on('click', function (event) {
if ($group.length > 1) {
v_align($group, 'top');
} else {
reset_free_positioning($group);
set_top_alignment($group);
}
show_active_alignment();
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($group.length > 1) {
v_align($group, 'middle');
} else {
reset_free_positioning($group);
set_middle_alignment($group);
}
show_active_alignment();
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($group.length > 1) {
v_align($group, 'bottom');
} else {
reset_free_positioning($group);
set_bottom_alignment($group);
}
show_active_alignment();
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($group.length > 1) {
h_align($group, 'left');
} else {
reset_free_positioning($group);
set_left_alignment($group);
}
show_active_alignment();
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($group.length > 1) {
h_align($group, 'center');
} else {
reset_free_positioning($group);
set_center_alignment($group);
}
show_active_alignment();
});
$('').appendTo($e_alignment).on('click', function (event) {
if ($group.length > 1) {
h_align($group, 'right');
} else {
reset_free_positioning($group);
set_right_alignment($group);
}
show_active_alignment();
});
$hierarchy.find('.azh-root').css('height', 'calc(100% - 95px)');
show_active_alignment();
if ($group.length > 2) {
var $g_alignment = $('').appendTo($alignment);
$('').appendTo($g_alignment).on('click', function (event) {
if ($e_alignment.find('.azh-active').length === 2) {
between_v_align($group, 'top');
}
});
$('').appendTo($g_alignment).on('click', function (event) {
if ($e_alignment.find('.azh-active').length === 2) {
between_v_align($group, 'middle');
}
});
$('').appendTo($g_alignment).on('click', function (event) {
if ($e_alignment.find('.azh-active').length === 2) {
between_v_align($group, 'bottom');
}
});
$('').appendTo($g_alignment).on('click', function (event) {
if ($e_alignment.find('.azh-active').length === 2) {
between_h_align($group, 'left');
}
});
$('').appendTo($g_alignment).on('click', function (event) {
if ($e_alignment.find('.azh-active').length === 2) {
between_h_align($group, 'center');
}
});
$('').appendTo($g_alignment).on('click', function (event) {
if ($e_alignment.find('.azh-active').length === 2) {
between_h_align($group, 'right');
}
});
}
azh.document_off('keydown.azh-free-positioning-move').document_on('keydown.azh-free-positioning-move', function (event) {
if ($group.length) {
switch ((event.keyCode ? event.keyCode : event.which)) {
//case 13: // Enter
//case 27: // Esc
//case 32: // Space
case 37: // Left Arrow
switch (get_h_alignment($group)) {
case 'left':
$group.each(function () {
set_left(azh.$(this), get_left(azh.$(this)) - 1);
});
break;
case 'center':
$group.each(function () {
set_center(azh.$(this), get_center(azh.$(this)) - 1);
});
break;
case 'right':
$group.each(function () {
set_right(azh.$(this), get_right(azh.$(this)) + 1);
});
break;
}
event.preventDefault();
break;
case 38: // Up Arrow
switch (get_v_alignment($group)) {
case 'top':
$group.each(function () {
set_top(azh.$(this), get_top(azh.$(this)) - 1);
});
break;
case 'middle':
$group.each(function () {
set_middle(azh.$(this), get_middle(azh.$(this)) - 1);
});
break;
case 'bottom':
$group.each(function () {
set_bottom(azh.$(this), get_bottom(azh.$(this)) + 1);
});
break;
}
event.preventDefault();
break;
case 39: // Right Arrow
switch (get_h_alignment($group)) {
case 'left':
$group.each(function () {
set_left(azh.$(this), get_left(azh.$(this)) + 1);
});
break;
case 'center':
$group.each(function () {
set_center(azh.$(this), get_center(azh.$(this)) + 1);
});
break;
case 'right':
$group.each(function () {
set_right(azh.$(this), get_right(azh.$(this)) - 1);
});
break;
}
event.preventDefault();
break;
case 40: // Down Arrow
switch (get_v_alignment($group)) {
case 'top':
$group.each(function () {
set_top(azh.$(this), get_top(azh.$(this)) + 1);
});
break;
case 'middle':
$group.each(function () {
set_middle(azh.$(this), get_middle(azh.$(this)) + 1);
});
break;
case 'bottom':
$group.each(function () {
set_bottom(azh.$(this), get_bottom(azh.$(this)) - 1);
});
break;
}
event.preventDefault();
break;
}
}
});
} else {
azh.document_off('keydown.azh-free-positioning-move');
}
}
function open_context_menu(event, $node) {
if ($node) {
azh.controls_container.find('.azh-element-controls.azh-active, .azh-column-controls.azh-active, .azh-section-controls.azh-active').removeClass('azh-active');
var $element = $node.closest('[data-element]');
if ($element.data('azh-controls')) {
$element.data('azh-controls').find('.azh-utility-wrapper').trigger('click');
}
var e = azh.$.Event('contextmenu', {target: $node.get(0)});
e.clientX = event.clientX - azh.device_left;
e.clientY = event.clientY;
$element.trigger(e);
}
}
function clear_utility_state() {
azh.body.find('.azh-saved-utility').each(function () {
var $section_or_element = azh.$(this);
remove_from_stored_classes($section_or_element, 'azh-saved-utility');
$section_or_element.removeClass('azh-saved-utility');
});
azh.utility_state = false;
}
function save_utility_state() {
clear_utility_state();
var $utility = azh.controls_container.find('.azh-section-controls.azh-active .azh-utility-wrapper .azh-utility, .azh-element-controls.azh-active .azh-utility-wrapper .azh-utility');
if ($utility.length && $utility.data('azh-linked-element')) {
add_to_stored_classes($utility.data('azh-linked-element'), 'azh-saved-utility');
$utility.data('azh-linked-element').addClass('azh-saved-utility');
var group = $utility.find('[data-group].azh-group.azh-active').data('group');
var subgroup = $utility.find('[data-subgroup].azh-subgroup.azh-active').data('subgroup');
azh.utility_state = {
scrollTop: $utility.children('.azh-controls-list').scrollTop(),
group: group,
subgroup: subgroup
};
}
}
function restore_utility_state() {
if (azh.utility_state) {
var $section_or_element = azh.body.find('.azh-saved-utility');
if ($section_or_element.length) {
$section_or_element = azh.$($section_or_element.get(0));
$section_or_element.trigger('click');
var $element_controls = $section_or_element.data('azh-controls');
$element_controls.find('.azh-utility-wrapper').trigger('click');
var $utility = $element_controls.find('.azh-utility');
if ($utility.length && $utility.data('azh-linked-element')) {
$utility.closest('.azh-utility-wrapper').trigger('click');
$utility.find('[data-group="' + azh.utility_state.group + '"].azh-title').trigger('click');
$utility.find('[data-subgroup="' + azh.utility_state.subgroup + '"].azh-title').trigger('click');
$utility.children('.azh-controls-list').scrollTop(azh.utility_state.scrollTop);
clear_utility_state();
}
}
}
}
var elements_hierarchy_mouse = function ($hierarchy, $start) {
$start.find('.azh-name').off('mouseenter').on('mouseenter', function () {
var $child = $(this).parent();
if ($child.data('azh-linked-element')) {
$child.data('azh-linked-element').addClass('azh-over');
}
}).off('mouseleave').on('mouseleave', function () {
var $child = $(this).parent();
if ($child.data('azh-linked-element')) {
$child.data('azh-linked-element').removeClass('azh-over');
}
}).off('click').on('click', function (event) {
var $child = $(this).parent();
if ($child.is('.azh-active.azh-expand')) {
$child.removeClass('azh-expand');
if ($child.data('azh-linked-element')) {
$child.data('azh-linked-element').removeClass('azh-expand');
}
} else {
$child.addClass('azh-expand');
if ($child.data('azh-linked-element')) {
$child.data('azh-linked-element').addClass('azh-expand');
}
}
if ($child.data('azh-linked-element')) {
var $element = $child.data('azh-linked-element');
if ($element.is('[data-element]')) {
if (event.ctrlKey) {
var $group = $hierarchy.data('azh-group');
if ($child.is('.azh-active')) {
if ($group) {
$group = $group.not($element);
$group = $group.length ? $group : false;
} else {
$group = false;
}
$child.removeClass('azh-active');
} else {
if ($group) {
if ($group.parent().get(0) == $element.parent().get(0)) {
$group = $group.add($element);
$child.addClass('azh-active');
}
} else {
$hierarchy.find('.azh-child.azh-active').removeClass('azh-active');
$group = $element;
$child.addClass('azh-active');
}
}
$hierarchy.data('azh-group', $group);
if ($group) {
show_alignments($hierarchy, $group);
}
} else {
$hierarchy.data('azh-group', $element);
show_alignments($hierarchy, $element);
$hierarchy.find('.azh-child.azh-active').removeClass('azh-active');
$child.addClass('azh-active');
}
var $element_controls = $element.data('azh-controls');
//if ($element_controls && $element.is(':visible')) {
if ($element_controls) {
azh.controls_container.find('.azh-element-controls.azh-active, .azh-column-controls.azh-active, .azh-section-controls.azh-active').removeClass('azh-active');
set_element_controls_position($element, $element_controls);
//$element_controls.addClass('azh-active');
$element_controls.find('.azh-utility-wrapper').trigger('click');
}
}
if ($element.is('[data-section]')) {
var $section_controls = $element.data('azh-controls');
//if ($section_controls && $element.is(':visible')) {
if ($section_controls) {
azh.controls_container.find('.azh-section-controls.azh-active').removeClass('azh-active');
$section_controls.find('.azh-utility-wrapper').trigger('click');
$hierarchy.find('.azh-child.azh-active').removeClass('azh-active');
$child.addClass('azh-active');
}
}
}
return false;
}).off('contextmenu').on('contextmenu', function (event) {
event.preventDefault();
var $child = $(event.target).closest('.azh-child');
var $element = $child.data('azh-linked-element');
open_context_menu(event, $element);
});
};
var refresh_elements_hierarchy_partial = function ($element) {
var $section = $element.closest('[data-section]');
if ($section.length && $section.data('azh-linked-hierarchy-child')) {
var $child = $section.data('azh-linked-hierarchy-child');
$child.empty();
fill_elements_hierarchy($section, $child);
elements_hierarchy_mouse($('.azh-elements-hierarchy'), $child);
} else {
refresh_elements_hierarchy();
}
};
var refresh_elements_hierarchy = function () {
function roll($child) {
var $root = $hierarchy.find('.azh-root');
$root.scrollTop($child.offset().top - $root.offset().top + $root.scrollTop() - $root.height() / 2);
}
var $hierarchy = $('.azh-elements-hierarchy');
if ($hierarchy.length) {
$hierarchy.empty();
var $title = $('
' + azh.i18n.elements_hierarchy + '
').prependTo($hierarchy);
var $root = $('').appendTo($hierarchy);
azh.content_wrapper.find('[data-section]').addBack().filter('[data-section]').each(function () {
if (!azh.$(this).parents('[data-element]').length) {
var $child = $('').appendTo($root);
fill_elements_hierarchy(azh.$(this), $child);
}
});
elements_hierarchy_mouse($hierarchy, $hierarchy);
azh.document.off('click.azh-hierarchy contextmenu.azh-hierarchy').on('click.azh-hierarchy contextmenu.azh-hierarchy', function (event) {
//$ or azh.$
var $target = azh.$(event.target);
if ($target.closest('.az-no-utility').length) {
$target = $target.closest('.az-no-utility').parent();
}
var $element = $target.is('[data-element]') ? $target : $target.closest('[data-element]');
if ($element.length) {
show_alignments($hierarchy, $element);
var $child = $element.data('azh-linked-hierarchy-child');
if ($child && $child.length) {
$hierarchy.find('.azh-child.azh-active').removeClass('azh-active');
$child.addClass('azh-active');
$child.parents('.azh-child').each(function () {
var $this = $(this);
$this.addClass('azh-expand');
if ($this.data('azh-linked-element')) {
$this.data('azh-linked-element').addClass('azh-expand');
}
});
roll($child);
}
if (event.which === 1) {
if ($element.data('azh-controls')) {
var $utility_wrapper = $element.data('azh-controls').find('.azh-utility-wrapper');
if ($utility_wrapper.length) {
azh.controls_container.find('.azh-element-controls.azh-active, .azh-column-controls.azh-active, .azh-section-controls.azh-active').removeClass('azh-active');
setTimeout(function () {
$utility_wrapper.trigger('click');
});
}
}
}
} else {
var $section = $target.closest('[data-section]');
if ($section.length) {
var $child = $section.data('azh-linked-hierarchy-child');
$hierarchy.find('.azh-child.azh-active').removeClass('azh-active');
$child.addClass('azh-active');
if ($child && $child.length) {
$child.parents('.azh-child').each(function () {
var $this = $(this);
$this.addClass('azh-expand');
if ($this.data('azh-linked-element')) {
$this.data('azh-linked-element').addClass('azh-expand');
}
});
roll($child);
}
if (event.which === 1) {
if ($section.data('azh-controls')) {
var $utility_wrapper = $section.data('azh-controls').find('.azh-utility-wrapper');
if ($utility_wrapper.length) {
azh.controls_container.find('.azh-element-controls.azh-active, .azh-column-controls.azh-active, .azh-section-controls.azh-active').removeClass('azh-active');
setTimeout(function () {
$utility_wrapper.trigger('click');
});
}
}
}
}
}
});
$document.off('click.azh-hierarchy-active').on('click.azh-hierarchy-active', function (event) {
if (event.which && !$(event.srcElement).is('.azh-resizer')) {
if (!$(event.target).closest(azh.click_not_hide_contextmenu).length && !$body.children('.select2-container').length) {
if (!$(event.target).closest('.azh-element-controls').length) {
azh.controls_container.find('.azh-element-controls.azh-active').removeClass('azh-active');
}
if (!$(event.target).closest('.azh-column-controls').length) {
azh.controls_container.find('.azh-column-controls.azh-active').removeClass('azh-active');
}
if (!$(event.target).closest('.azh-section-controls').length) {
azh.controls_container.find('.azh-section-controls.azh-active').removeClass('azh-active');
}
}
}
});
// $hierarchy.find('.azh-root').resizable({
// handles: 's'
// });
//
// $hierarchy.draggable({
// handle: ".azh-hierarchy-title"
// });
}
};
azh.get_wrapper_controls = function ($wrapper) {
var $controls = $();
$wrapper.find('.azh-controls').addBack().filter('.azh-controls').each(function () {
$controls = $controls.add(azh.$(this).data('azh-controls'));
});
$wrapper.find('.azh-cloneable-controls').addBack().filter('.azh-cloneable-controls').each(function () {
$controls = $controls.add(azh.$(this).data('azh-cloneable-controls'));
});
$wrapper.find('.azh-cloneable-child-controls').addBack().filter('.azh-cloneable-child-controls').each(function () {
$controls = $controls.add(azh.$(this).data('azh-cloneable-child-controls'));
});
$wrapper.find('.azh-item-controls').addBack().filter('.azh-item-controls').each(function () {
$controls = $controls.add(azh.$(this).data('azh-item-controls'));
});
$wrapper.find('.azh-grid').addBack().filter('.azh-grid').each(function () {
azh.$(this).children().each(function () {
var $this = azh.$(this);
var $resizer = $this.data('azh-resizer');
if ($resizer) {
$controls = $controls.add($resizer);
}
var $column_controls = $this.data('azh-controls');
if ($column_controls) {
$controls = $controls.add($column_controls);
}
});
});
return $controls;
};
azh.structure_refresh = function ($content) {
$('.azh-structure').empty();
$content.find('[data-section]').each(function () {
if (!azh.$(this).parents('[data-element]').length) {
var $section_path = $('
' + azh.$(this).data('section') + '
').appendTo($('.azh-structure'));
$section_path.data('azh-section', azh.$(this));
azh.$(this).data('azh-section-path', $section_path);
$('').appendTo($section_path).on('click', function () {
var $section = $section_path.data('azh-section');
var $html = $section.clone(true);
azh.liquid_prepare($html);
var html = extract_html($html.wrap('').parent(), true);
var scrollTop = azh.window.scrollTop();
var $new_section = azh.$(html);
store_html($new_section.get(0));
$section.after($new_section);
azh.section_customization_init($new_section);
if (azh.frontend_init) {
azh.frontend_init($new_section);
}
azh.window.trigger('resize');
azh.window.scrollTop(scrollTop);
_.defer(refresh_elements_hierarchy);
azh.structure_refresh($content);
return false;
});
$('').appendTo($section_path).on('click', function () {
remove_visible_controls($section_path.data('azh-section'));
$section_path.data('azh-section').remove();
$section_path.remove();
_.defer(refresh_elements_hierarchy);
return false;
});
$section_path.on('click', function () {
scroll_and_focus($(this).data('azh-section'));
return false;
});
}
});
$('.azh-structure').sortable({
placeholder: 'azh-placeholder',
forcePlaceholderSize: true,
update: function (event, ui) {
var section = $(ui.item).data('azh-section');
$(section).detach();
if ($(ui.item).next().length) {
var next_section = $(ui.item).next().data('azh-section');
$(next_section).before(section);
} else {
if ($(ui.item).prev().length) {
var prev_section = $(ui.item).prev().data('azh-section');
$(prev_section).after(section);
}
}
_.defer(refresh_elements_hierarchy);
},
over: function (event, ui) {
ui.placeholder.attr('class', ui.helper.attr('class'));
ui.placeholder.removeClass('ui-sortable-helper');
ui.placeholder.attr('style', ui.helper.attr('style'));
ui.placeholder.css('position', 'relative');
ui.placeholder.css('z-index', 'auto');
ui.placeholder.css('left', 'auto');
ui.placeholder.css('top', 'auto');
ui.placeholder.addClass('azh-placeholder');
}
});
if ($('.azh-structure').length) {
$('.azh-structure').scrollTop($('.azh-structure')[0].scrollHeight);
}
if ($('.azh-structure').children().length) {
$('#azexo-html-library .azh-panel.azh-builder').removeClass('azh-empty');
} else {
$('#azexo-html-library .azh-panel.azh-builder').addClass('azh-empty');
}
};
azh.library_init = function ($content) {
function filters_change() {
function tags_select_refresh() {
$tags_select.children().attr('hidden', 'hidden');
for (var tag in azh.tags) {
if (tag && $('.azh-library .azh-sections .azh-section[data-tags*="' + tag + '"]:visible').length) {
$tags_select.children('[value="' + tag + '"]').removeAttr('hidden');
}
}
$tags_select.children('[value=""]').removeAttr('hidden');
if ($tags_select.children('[value="' + $tags_select.val() + '"][hidden]').length) {
$tags_select.val('');
setTimeout(function () {
filters_change();
});
}
}
var category = $categories_select.find('option:selected').val();
var tag = $tags_select.find('option:selected').val();
if (category == '' && tag == '') {
$('.azh-library .azh-sections .azh-section').show();
} else {
if (category != '' && tag == '') {
$('.azh-library .azh-sections .azh-section').hide();
$('.azh-library .azh-sections .azh-section[data-path^="' + category + '"]').show();
tags_select_refresh();
}
if (category == '' && tag != '') {
$('.azh-library .azh-sections .azh-section').hide();
$('.azh-library .azh-sections .azh-section[data-tags*="' + tag + '"]').show();
}
if (category != '' && tag != '') {
$('.azh-library .azh-sections .azh-section').show();
$('.azh-library .azh-sections .azh-section:not([data-path^="' + category + '"])').hide();
tags_select_refresh();
$('.azh-library .azh-sections .azh-section:not([data-tags*="' + tag + '"])').hide();
}
}
}
function add_tags($section_or_element, new_tags) {
var tags = $section_or_element.attr('data-tags');
tags = tags ? tags : '';
tags = tags.split(',').filter(function (el) {
return el;
});
tags = tags.concat(new_tags);
tags = tags.filter(function (value, index, self) {
return self.indexOf(value) === index;
});
$section_or_element.attr('data-tags', tags.join(','));
}
azh.tags = {};
var files_tags = {};
for (var dir in azh.dirs_options) {
if ('tags' in azh.dirs_options[dir]) {
for (var file in azh.dirs_options[dir].tags) {
var tags = azh.dirs_options[dir].tags[file].split(',').map(function (tag) {
if ($.trim(tag)) {
azh.tags[$.trim(tag).toLowerCase()] = true;
}
return $.trim(tag).toLowerCase();
});
files_tags[dir + '/' + file] = tags;
}
}
}
$('.azh-library .azh-sections .azh-section, .azh-library .azh-elements .azh-element').each(function () {
var tags = $(this).attr('data-tags');
tags = tags ? tags : '';
tags.split(',').map(function (tag) {
if ($.trim(tag)) {
azh.tags[$.trim(tag).toLowerCase()] = true;
}
});
});
$('.azh-library .azh-sections .azh-section, .azh-library .azh-elements .azh-element').each(function () {
var key = $(this).data('dir') + '/' + $(this).data('path');
if (key in files_tags) {
add_tags($(this), files_tags[key]);
}
});
var child_suggestions = {};
for (var dir in azh.dirs_options) {
if ('child-suggestions' in azh.dirs_options[dir]) {
for (var file in azh.dirs_options[dir]['child-suggestions']) {
var path = dir + '/' + file;
if (!(path in child_suggestions)) {
child_suggestions[path] = [];
}
$(azh.dirs_options[dir]['child-suggestions'][file]).each(function () {
child_suggestions[path].push(dir + '/' + this);
});
if (!(file in child_suggestions)) {
child_suggestions[file] = [];
}
$(azh.dirs_options[dir]['child-suggestions'][file]).each(function () {
child_suggestions[file].push(this);
});
}
}
}
var child_suggestions_elements = {};
for (var path in child_suggestions) {
$(child_suggestions[path]).each(function () {
var suggestion = this;
$('.azh-library .azh-elements .azh-element').each(function () {
var key = $(this).data('dir') + '/' + $(this).data('path');
if (key == suggestion) {
if (!(path in child_suggestions_elements)) {
child_suggestions_elements[path] = [];
}
child_suggestions_elements[path].push(this);
}
if ($(this).data('path') == suggestion) {
if (!(path in child_suggestions_elements)) {
child_suggestions_elements[path] = [];
}
child_suggestions_elements[path].push(this);
}
});
});
}
$('.azh-library .azh-elements .azh-element').each(function () {
var key = $(this).data('dir') + '/' + $(this).data('path');
if (key in child_suggestions_elements) {
$(this).data('child-suggestions', child_suggestions_elements[key]);
}
if ($(this).data('path') in child_suggestions_elements) {
$(this).data('child-suggestions', child_suggestions_elements[$(this).data('path')]);
}
});
$('.azh-add-section').off('click').on('click', function () {
$('.azh-sections .azh-section.azh-fuzzy').removeClass('azh-fuzzy');
if ($('.azh-library').css('display') == 'none') {
$('.azh-structure').animate({
'max-height': "100px"
}, 400, function () {
$('.azh-structure').scrollTop($('.azh-structure')[0].scrollHeight);
});
$('.azh-sections').height($('#azexo-html-library > .azh-builder').height() - ($('.azh-structure').outerHeight() > 100 ? 100 : $('.azh-structure').outerHeight()) - 120);
$('.azh-library').slideDown();
$(this).text($(this).data('close'));
$(this).addClass('azh-open');
$('.azh-section-operations').addClass('azh-open');
if ($(this).data('category')) {
$categories_select.val($(this).data('category'));
} else {
if (azh.default_category && $categories_select.find('[value="' + azh.default_category + '"]').length) {
$categories_select.val(azh.default_category);
}
}
filters_change();
} else {
$('.azh-structure').animate({
'max-height': "600px"
}, 400);
$('.azh-library').slideUp();
$(this).text($(this).data('open'));
$(this).removeClass('azh-open');
$('.azh-section-operations').removeClass('azh-open');
}
return false;
});
$('.azh-copy-sections-list').off('click').on('click', function () {
var paths = []
$('.azh-structure .azh-section-path').each(function () {
paths.push($(this).text());
});
var $temp = $("");
$("body").append($temp);
$temp.val(paths.join('|')).select();
document.execCommand("copy");
$temp.remove();
azh.notify(azh.i18n.copied);
return false;
});
$('.azh-insert-sections-list').off('click').on('click', function () {
var sections = prompt(azh.i18n.paste_sections_list_here);
if ($.trim(sections) !== '') {
$(sections.split('|')).each(function () {
$('.azh-library .azh-sections .azh-section[data-path="' + this + '"]').click();
});
}
return false;
});
var $categories_select = $('.azh-library .azh-categories').off('change').on('change', filters_change);
var $tags_select = $('').appendTo('.azh-library-filters').on('change', filters_change);
$('').appendTo($tags_select);
Object.keys(azh.tags).sort().forEach(function (tag, i) {
$('').appendTo($tags_select);
});
if (azh.default_category && $categories_select.find('[value="' + azh.default_category + '"]').length) {
$categories_select.val(azh.default_category);
filters_change();
}
if ($('#azexo-html-library .azh-panel.azh-builder').is('.azh-empty')) {
$('.azh-add-section:not([data-category])').trigger('click');
}
$('.azh-library .azh-sections .azh-section').off('click').on('click', function () {
function click_process(data) {
var scrollTop = azh.window.scrollTop();
var section_exists = false;
data = data.replace(/{{azh-uri}}/g, $preview.data('dir-uri'));
data = azh.do_replaces(data);
data = html_uglify(data);
var $section = azh.$('
' + data + '
');
store_html($section.get(0));
$content.append($section);
azh.section_customization_init($section);
if (azh.frontend_init) {
azh.frontend_init($section);
}
azh.window.trigger('resize');
azh.structure_refresh($content);
var pattern = /(data-section|data-element)=[\'"]([^\'"]+)[\'"]/gi;
var match = null;
azh.window.scrollTop(scrollTop);
if (!$preview.is('.general')) {
//$section.addClass('azh-loading');
load_required_scripts('
' + data + '
', $preview.data('path'), function () {
//$section.removeClass('azh-loading');
});
}
}
var $preview = $(this);
if ($preview.data('url') in azh.sections_cache) {
click_process(azh.sections_cache[$preview.data('url')]);
} else {
$.get($preview.data('url'), function (data) {
azh.sections_cache[$preview.data('url')] = data;
click_process(data);
});
}
return false;
});
if ($('#azexo-html-library > .azh-general .azh-section').length === 0) {
$('.azh-library .azh-sections .azh-section.general').each(function () {
var $section = $(this).clone(true);
$section.css('display', '').removeClass('azh-fuzzy');
$('#azexo-html-library > .azh-general').append($section);
});
}
if ($('#azexo-html-library > .azh-general .azh-section').length === 0) {
$('#azexo-html-library > .azh-library-actions > .azh-general').remove();
}
$('.azh-library-actions > div:not(.azh-save)').off('click').on('click', function () {
$('#azexo-html-library > .azh-active, #azexo-html-library .azh-library-actions .azh-active').removeClass('azh-active');
var tab = $.trim($(this).attr('class'));
$(this).addClass('azh-active');
$('#azexo-html-library > .' + tab).addClass('azh-active');
});
$('.azh-library-actions > .azh-save').off('click').on('click', function (event) {
azh.save();
if (event.which) {
var $this = $(this);
$this.css({
'pointer-events': 'none',
'opacity': '0.5'
});
$window.one('azh-saved', function () {
$this.css({
'pointer-events': '',
'opacity': ''
});
azh.notify(azh.i18n.saved);
});
$window.one('azh-not-saved', function () {
$this.css({
'pointer-events': '',
'opacity': ''
});
});
}
});
$('#azexo-html-library .azh-panel .azh-panel-content').each(function () {
var $this = $(this);
$(this).height($this.closest('.azh-panel').height() - $this.closest('.azh-panel').find('.azh-panel-title').outerHeight() - 20);
});
$('#azexo-html-library .azh-revisions.azh-panel').off('click').on('click', function (event) {
var $revision = $(event.target).closest('.azh-revision');
if ($revision.length) {
azh.content_wrapper.addClass('azh-loading');
$.post(azh.ajaxurl, {
'action': 'azh_load_post',
'post_id': $revision.data('id')
}, function (data) {
if (data && data['content']) {
if (data['settings'] && data['settings']['shortcodes']) {
azh.shortcode_instances = data['settings']['shortcodes'];
}
azh.controls_container.empty();
azh.content_wrapper.html(data['content']);
store_html(azh.content_wrapper.get(0));
azh.content_wrapper = azh.$(azh.content_wrapper);
azh.content_wrapper.find('[data-section]').each(function () {
if (!azh.$(this).parents('[data-element]').length) {
azh.section_customization_init(azh.$(this));
}
});
if (azh.frontend_init) {
azh.frontend_init(azh.content_wrapper);
}
azh.structure_refresh(azh.content_wrapper);
azh.changed = false;
$revision.closest('.azh-panel').find('.azh-restore-revision').show().data('id', $revision.data('id'));
} else {
alert(azh.i18n.revision_does_not_have_builder_content);
}
azh.content_wrapper.removeClass('azh-loading');
}, 'json');
}
var $restore = $(event.target).closest('.azh-restore-revision');
if ($restore.length) {
$.post(azh.ajaxurl, {
'action': 'azh_restore_revision',
'post_id': $restore.data('id')
}, function (data) {
if (data) {
azh.notify(azh.i18n.revision_has_been_restored);
}
$restore.hide();
});
}
});
$('.azh-elements-hierarchy').remove();
var $hierarchy = $('').appendTo($body).show();
$window.trigger('azh-library-init');
setTimeout(function () {
refresh_elements_hierarchy();
$('.azh-library-actions > .azh-builder').trigger('click');
});
};
azh.customizer_init = function () {
createEditor(azh.window.get(0), azh.document.get(0));
if ($.ui && $.ui.mouse) {
$.widget("ui.rotatable", $.ui.mouse, {
options: {
handle: false,
angle: false,
// callbacks
start: null,
rotate: null,
stop: null
},
handle: function (handle) {
if (handle === undefined) {
return this.options.handle;
}
this.options.handle = handle;
},
angle: function (angle) {
if (angle === undefined) {
return this.options.angle;
}
this.options.angle = angle;
this.elementCurrentAngle = angle;
this.performRotation(this.options.angle);
}, _create: function () {
var handle;
if (!this.options.handle) {
handle = $(azh.document.get(0).createElement('div'));
handle.addClass('ui-rotatable-handle');
} else {
handle = this.options.handle;
}
this.listeners = {
rotateElement: $.proxy(this.rotateElement, this),
startRotate: $.proxy(this.startRotate, this),
stopRotate: $.proxy(this.stopRotate, this),
wheelRotate: $.proxy(this.wheelRotate, this)
};
this.element.bind('wheel', this.listeners.wheelRotate);
handle.draggable({helper: 'clone', start: this.dragStart, handle: handle});
handle.bind('mousedown', this.listeners.startRotate);
handle.appendTo(this.element);
if (this.options.angle != false) {
this.elementCurrentAngle = this.options.angle;
this.performRotation(this.elementCurrentAngle);
} else {
this.elementCurrentAngle = 0;
}
},
_destroy: function () {
this.element.removeClass('ui-rotatable');
this.element.find('.ui-rotatable-handle').remove();
},
performRotation: function (angle) {
var transform = this.element.get(0).style['transform'];
transform = transform ? transform : '';
var match = /rotate\(([-\.\d]+)rad\)/g.exec(transform);
if (match) {
transform = transform.replace(match[0], 'rotate(' + angle + 'rad)');
} else {
transform = 'rotate(' + angle + 'rad)';
}
this.element.get(0).style['transform'] = transform;
},
getElementOffset: function () {
this.performRotation(0);
var offset = this.element[0].getBoundingClientRect();
this.performRotation(this.elementCurrentAngle);
return offset;
},
getElementCenter: function () {
var elementOffset = this.getElementOffset();
var elementCentreX = elementOffset.left + this.element.width() / 2;
var elementCentreY = elementOffset.top + this.element.height() / 2;
return Array(elementCentreX, elementCentreY);
}, dragStart: function (event) {
if (this.element) {
return false;
}
},
startRotate: function (event) {
var center = this.getElementCenter();
var startXFromCenter = event.pageX - center[0];
var startYFromCenter = event.pageY - center[1];
this.mouseStartAngle = Math.atan2(startYFromCenter, startXFromCenter);
this.elementStartAngle = this.elementCurrentAngle;
this.hasRotated = false;
this._propagate("start", event);
azh.document.bind('mousemove', this.listeners.rotateElement);
azh.document.bind('mouseup', this.listeners.stopRotate);
return false;
},
rotateElement: function (event) {
if (!this.element || this.element.disabled) {
return false;
}
var center = this.getElementCenter();
var xFromCenter = event.pageX - center[0];
var yFromCenter = event.pageY - center[1];
var mouseAngle = Math.atan2(yFromCenter, xFromCenter);
var rotateAngle = mouseAngle - this.mouseStartAngle + this.elementStartAngle;
if (event.shiftKey) {
var predefinedAngle = 15 / 180 * Math.PI;
if (rotateAngle < 0)
predefinedAngle *= -1;
rotateAngle -= (rotateAngle + predefinedAngle / 2) % (predefinedAngle) - predefinedAngle / 2;
}
rotateAngle = Math.round(rotateAngle * 1000) / 1000;
this.performRotation(rotateAngle);
var previousRotateAngle = this.elementCurrentAngle;
this.elementCurrentAngle = rotateAngle;
this._propagate("rotate", event);
if (previousRotateAngle != rotateAngle) {
this._trigger("rotate", event, this.ui());
this.hasRotated = true;
}
return false;
},
stopRotate: function (event) {
if (!this.element || this.element.disabled) {
return;
}
azh.document.unbind('mousemove', this.listeners.rotateElement);
azh.document.unbind('mouseup', this.listeners.stopRotate);
this.elementStopAngle = this.elementCurrentAngle;
this._propagate("stop", event);
setTimeout(function () {
this.element = false;
}, 10);
return false;
}, wheelRotate: function (event) {
var angle = Math.round(event.originalEvent.deltaY / 10) * Math.PI / 180;
angle = Math.round(angle * 1000) / 1000;
angle = this.elementCurrentAngle + angle;
this.elementStartAngle = this.elementCurrentAngle;
this._trigger("start", event, this.ui());
this.angle(angle);
this._trigger("rotate", event, this.ui());
this.elementStopAngle = this.elementCurrentAngle;
this.hasRotated = true;
this._trigger("stop", event, this.ui());
event.preventDefault();
}, _propagate: function (n, event) {
$.ui.plugin.call(this, n, [event, this.ui()]);
(n !== "rotate" && this._trigger(n, event, this.ui()));
},
plugins: {}, ui: function () {
return {
api: this,
element: this.element,
angle: {
start: this.elementStartAngle,
current: this.elementCurrentAngle,
stop: this.elementStopAngle
}
};
}
});
}
azh.window.trigger("azh-customizer-before-init", {
azh: azh
});
$window.trigger("azh-customizer-before-init", {
azh: azh
});
azh.window.one('az-frontend-before-init', function (event, data) {
var $wrapper = data.wrapper;
if (azh.content_wrapper.has($wrapper).length) {
$wrapper.find('[data-section]').addBack().filter('[data-section]').each(function () {
if (!azh.$(this).parents('[data-element]').length) {
azh.section_customization_init(azh.$(this));
}
});
} else {
if ($wrapper.is(azh.body)) {
azh.content_wrapper.find('[data-section]').addBack().filter('[data-section]').each(function () {
if (!azh.$(this).parents('[data-element]').length) {
azh.section_customization_init(azh.$(this));
}
});
}
}
azh.changed = false;
});
azh.window.one('az-frontend-after-init', function (event, data) {
trigger_mousemove_controls(azh.content_wrapper);
});
azh.window.on('click', function (event, data) {
trigger_mousemove_controls(azh.content_wrapper);
});
// azh.window.on("az-full-width", function (event, data) {
// var $wrapper = data.element.closest('[data-section]');
// if ($wrapper.length === 0) {
// $wrapper = data.element;
// }
// $wrapper.find('.azh-controls').addBack().filter('.azh-controls').each(function () {
// if (azh.$(this).data('azh-controls')) {
// azh.$(this).data('azh-controls').find('.azh-control').trigger('azh-init');
// }
// });
// });
azh.content_wrapper = false;
if (azh.body.find('.azh-content-wrapper').length) {
azh.content_wrapper = azh.body.find('.azh-content-wrapper').first();
azh.structure_refresh(azh.content_wrapper);
azh.library_init(azh.content_wrapper);
} else {
if (azh.body.is('.page-template-azexo-html-template')) {
azh.content_wrapper = azh.body.find('> .page');
azh.structure_refresh(azh.content_wrapper);
azh.library_init(azh.content_wrapper);
} else {
if (azh.body.is('.page')) {
if ('azexo' in azh.window) {
azh.content_wrapper = azh.body.find('#content > .entry > .entry-content');
} else {
azh.content_wrapper = azh.body.find('[data-section]').first().parent();
}
azh.structure_refresh(azh.content_wrapper);
azh.library_init(azh.content_wrapper);
}
}
}
if (azh.content_wrapper) {
if ($('.azh-controls-container').length) {
azh.controls_container = $('.azh-controls-container');
} else {
azh.controls_container = $('').appendTo($body);
}
setTimeout(function () {
scroll_and_focus(azh.content_wrapper);
});
azh.body.find('a[href]').on('click', function (event) {
event.preventDefault();
});
} else {
alert(azh.i18n.content_wrapper_does_not_exists);
}
azh.window.trigger("azh-customizer-after-init", {
azh: azh
});
$window.trigger("azh-customizer-after-init", {
azh: azh
});
azh.changed = false;
};
azh.prepare = function (node) {
if (node) {
store_html(node);
} else {
store_html(azh.body.get(0));
}
};
azh.save = function (action) {
if (!action) {
action = 'azh_save';
}
azh.window.trigger('azh-before-save', {
action: action
});
azh.content_wrapper.find('.az-free-positioning').each(function () {
set_stored_attribute(azh.$(this), 'data-height', azh.$(this).height());
set_stored_attribute(azh.$(this), 'data-width', azh.$(this).width());
});
azh.content_wrapper.find('.az-auto-rescale').each(function () {
var $this = azh.$(this);
if (!$this.is('[data-width]')) {
set_stored_attribute($this, 'data-width', $this.width());
}
if (!$this.is('[data-height]')) {
set_stored_attribute($this, 'data-height', $this.height());
}
});
var $html = azh.$(azh.content_wrapper.get(0)).clone(true); //??? почему неверная версия jQuery
azh.liquid_prepare($html);
var html = html_uglify(extract_html($html));
if (html === false) {
alert(azh.i18n.html_is_not_valid);
return false;
}
html = make_html_unsafe(html);
$.post(azh.ajaxurl, {
'action': action,
'post_id': azh.post_id,
'shortcodes': azh.shortcode_instances,
'content': html
}, function (data) {
if (data) {
if (action === 'azh_autosave') {
$('#azexo-html-library .azh-revisions .azh-panel-content .azh-revision[data-type="autosave"]').first().remove();
}
if (data === 1) {
$window.trigger('azh-saved');
}
if(typeof data === 'object') {
$window.trigger('azh-saved');
$('#azexo-html-library .azh-revisions .azh-panel-content').prepend('
' + data['gravatar'] + '
' + data['date'] + '
' + data['author'] + '
');
}
if(typeof data === 'string') {
alert(data);
$window.trigger('azh-not-saved');
}
}
}, 'json');
azh.changed = false;
};
azh.change = function () {
azh.changed = true;
};
azh.liquid_prepare = function ($wrapper, $liquid_child) {
//удаление повторений темплейта в контейнере
$wrapper.find('.az-liquid-container').addBack().filter('.az-liquid-container').each(function () {
var $liquid_list = azh.$(this);
//удаление текстовых нод
$liquid_list.contents().filter(function () {
return this.nodeType === 3;
}).remove();
//закрывающий снипет должен быть в конце
var $close_snippet = $liquid_list.children('script[data-liquid]').last();
if ($close_snippet.is(':not(:last-child)')) {
$close_snippet.detach();
$liquid_list.append($close_snippet);
}
//оставляем только снипеты и продолжение шаблона между ними
$liquid_list.children().not('script[data-liquid]').each(function () {
var $child = azh.$(this);
if ($child.is('.az-liquid-content')) {
return;
}
var $content = closest_descendents($child, '.az-liquid-content');
if ($content.length) {
//удаляем сгенерированные обертыши
if ($child.is($liquid_child)) {
$liquid_child = $content;
}
$content.detach();
$child.replaceWith($content);
return;
}
$child.remove();
});
//$liquid_list.children().not('script[data-liquid]').not('.az-liquid-content').remove();
//удаляем повторения в случае цикла
while ($liquid_list.children('.az-liquid-content').length > 1) {
if ($liquid_child) {
$liquid_list.children('.az-liquid-content').not($liquid_child).not($liquid_list.children('.az-liquid-content').has($liquid_child)).first().remove();
} else {
$liquid_list.children('.az-liquid-content').first().remove();
}
}
});
//удаление всего отрендеренного не-контейнерными снипетами HTML
$wrapper.find('script[data-liquid]').each(function () {
var $snippet_wrapper = azh.$(this).parent();
if ($snippet_wrapper.is(':not(.az-liquid-container)')) {
$snippet_wrapper.contents().filter(function () {
return this.nodeType === 3;
}).remove();
$snippet_wrapper.children().not('script[data-liquid]').remove();
}
});
};
azh.liquid_process = function ($liquid, context) {
if ($liquid.closest('.az-liquid-process').length) {
return;
}
//перенести все атрибуты в контекст и сохранить снипеты с идентификатором
var snippets = {};
$liquid.find('script[data-liquid]').each(function () {
var $snippet = azh.$(this);
var id = makeid();
$snippet.attr('data-liquid', id);
snippets[id] = extract_html($snippet.wrap('').parent(), true);
$snippet.unwrap();
azh.$.each($snippet.get(0).attributes, function (index, attribute) {
var name = attribute.name.split('-');
if (name.length === 2) {
context[name[1]] = attribute.value;
}
});
//удалить скобки script[data-liquid] и обернуть коментариями с идентификатором
$snippet.data('azh-open-tag', '');
$snippet.data('azh-close-tag', '');
});
save_utility_state();
var liquid = extract_html($liquid, true);
if ($.trim(liquid)) {
try {
var liquid_doc = Liquid.parse(liquid);
var html = liquid_doc.render(context);
//востановить снипеты по коментариям
//коменты с одинаковым id не могут пересекатся, всегда вначале открывается потом закрываются, необходимо искать минимального размера скобки
for (var id in snippets) {
var pattern = new RegExp('((.|[\r\n])*?)', 'gi');
var $snippet = $liquid.find('script[data-liquid="' + id + '"]');
if ($snippet.parent('.az-liquid-container').length) {
//если снипет ребенок az-liquid-container - то он должен оставатся на своем месте
if ($snippet.is(':first-child')) {
html = html.replace(pattern, snippets[id] + "$1");
}
if ($snippet.is(':last-child')) {
html = html.replace(pattern, "$1" + snippets[id]);
}
} else {
html = html.replace(pattern, snippets[id] + "$1");
}
}
//удаляем неиспользованые коментарии (которые возникают в цикле)
html = html.replace(/()/gi, '');
html = html.replace(/()/gi, '');
//инициализация кастомизации - отрендеренный HTML недоступен для кастомизации ???
var $section_or_element = $liquid.closest('[data-element], [data-section]');
if ($section_or_element.length && $section_or_element.data('azh-controls')) {
var $utility = $section_or_element.data('azh-controls').find('.azh-utility');
if ($utility.length) {
$utility.children('.azh-controls-list').empty();
$utility.data('azh-filled', false);
$utility.closest('.azh-active').removeClass('azh-active');
}
}
$liquid.children().each(function () {
remove_visible_controls(azh.$(this));
});
$liquid.html(html);
$liquid.children().each(function () {
store_html(this);
});
$liquid.find('a[href]').on('click', function (event) {
event.preventDefault();
});
$liquid.addClass('az-liquid-process');
customization_init($liquid);
if (azh.frontend_init) {
azh.frontend_init($liquid);
}
$liquid.removeClass('az-liquid-process');
restore_utility_state();
} catch (e) {
console.log(e);
}
}
};
var tabs_init = function ($wrapper) {
$wrapper.each(function () {
var $tabs = $(this);
if (!$tabs.data('azh-tabs')) {
$tabs.find('> div:first-child > span > a[href^="#"]').on('click', function (event) {
var $this = $(this);
event.preventDefault();
$this.parent().addClass("azh-active");
$this.parent().siblings().removeClass("azh-active");
var tab = $this.attr("href");
$tabs.find('> div:last-child > div').not(tab).css("display", "none");
$(tab).fadeIn();
});
$tabs.find('> div:first-child > span:first-child > a[href^="#"]').click();
$tabs.data('azh-tabs', true);
}
});
};
$.QueryString = parse_query_string(window.location.search.substr(1).split('&'));
if ('azh' in $.QueryString && $.QueryString['azh'] == 'customize') {
$('head link[rel="stylesheet"][href]').each(function () {
var $this = $(this);
loaded_scripts[$this.attr('href').split('?')[0]] = $this;
});
if (window === window.parent) {
if (window.adminpage === 'post-php') {
$(function () {
$body = $('body');
});
} else {
on_ready_first(function () {
azh.body = azh.$('body');
store_html(azh.body.get(0));
});
$(function () {
$body = $('body');
azh.customizer_init();
});
}
}
} else {
$(function () {
if (window === window.parent) {
azh.edit_links_refresh = function () {
function show_edit_link($element) {
$($element.data('edit-link-control')).css({
"top": $element.offset().top,
"left": $element.offset().left,
"width": $element.outerWidth(),
"height": $element.outerHeight()
}).show();
}
function hide_edit_link($element) {
$($element.data('edit-link-control')).hide();
}
function is_visible($element) {
var visible = true;
if ($window.width() < $element.offset().left + $element.outerWidth()) {
visible = false;
}
if (!$element.is(":visible")) {
visible = false;
}
$element.parents().each(function () {
var $parent = $(this);
var elements = $parent.data('elements-with-azh-edit-link');
if (!elements) {
elements = [];
}
elements = elements.concat($element.get());
elements = $.unique(elements);
$parent.data('elements-with-azh-edit-link', elements);
if ($parent.css("display") == 'none' || $parent.css("opacity") == '0' || $parent.css("visibility") == 'hidden') {
visible = false;
$parent.off('click.azh mouseenter.azh mouseleave.azh').on('click.azh mouseenter.azh mouseleave.azh', function () {
var elements = $parent.data('elements-with-azh-edit-link');
$(elements).each(function () {
if (is_visible($(this))) {
show_edit_link($(this));
} else {
hide_edit_link($(this));
}
});
});
}
});
return visible;
}
var $container = false;
if ($('.azh-edit-links').length) {
$container = $('.azh-edit-links');
$container.detach();
} else {
$container = $('');
}
for (var links_type in azh.edit_links) {
if (!('elements' in azh.edit_links[links_type])) {
azh.edit_links[links_type].elements = [];
var selectors = Object.keys(azh.edit_links[links_type].links);
selectors.sort(function (a, b) {
return b.length - a.length;
});
for (var i = 0; i < selectors.length; i++) {
var selector = selectors[i];
var occurrence = 0;
$(selector).each(function () {
if (!$(this).data('edit-link-control')) {
var control = $('