-
Notifications
You must be signed in to change notification settings - Fork 0
/
bonifier_muse2modx.js
147 lines (124 loc) · 5.13 KB
/
bonifier_muse2modx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
$(document).ready(function () {
// ensure container style is well applied to modx contents
var selector = "p.modx, " +
"h1.modx, " +
"h2.modx, " +
"h3.modx, " +
"h4.modx, " +
"h5.modx, " +
"h6.modx";
var modxContainers = $(selector);
modxContainers.each(function (i, eachContainer) {
var siblingStyleApplied = false;
$(eachContainer).siblings().each(function (j, eachSibling) {
siblingStyleApplied = true;
if($(eachSibling).is(":empty")) {
$(eachSibling).remove();
}
var cssClass = $(eachContainer).attr('class').replace('modx', '').trim();
if(cssClass) {
if(!$(eachSibling).hasClass(cssClass)) {
$(eachSibling).addClass('modxClearCSS ' + cssClass);
}
}
// assume only classes are intended to replicate
// eachSibling.style.cssText += eachContainer.style.cssText;
});
if(siblingStyleApplied && $(eachContainer).is(":empty")) {
$(eachContainer).remove();
}
});
// menu position calculation & adjustment
var menuItemLastWidth = 0;
var menuItemAccumulateWidth = 0;
var menuItemTextNodes = $('.MenuItemLabel > p');
var regex = /(\d+)/g;
var pageLeft = $('div#page').position().left;
var pageRight = pageLeft + $('div#page').width();
var menuBarLeft = $('.MenuBar').parent().position().left;
var menuBarRight = menuBarLeft + $('.MenuBar').parent().width();
var alignLeft = Math.abs(pageLeft - menuBarLeft) < Math.abs(pageRight - menuBarRight);
if(alignLeft) {
for(var i = 0; i < menuItemTextNodes.length; i++) {
var textWidth = $(menuItemTextNodes[i]).width();
var textPadding = $(menuItemTextNodes[i]).parent().css('left');
var tmpMatch = textPadding.match(regex);
// set left attribute of the muse container of this menu item
if(menuItemLastWidth) {
$(menuItemTextNodes[i]).parent().parent().parent().css('left', menuItemAccumulateWidth + "px");
}
// if left attribute is defined with numbers & width unit is px
if(tmpMatch.length == 1 && textPadding.replace(regex, '') == "px") {
menuItemLastWidth = tmpMatch[0] * 2 + textWidth;
$(menuItemTextNodes[i]).parent().width(textWidth);
$(menuItemTextNodes[i]).parent().parent().width(menuItemLastWidth);
menuItemAccumulateWidth += menuItemLastWidth + 2;
}
}
} else {
for(var i = menuItemTextNodes.length - 1; i >= 0; i--) {
var textWidth = $(menuItemTextNodes[i]).width();
var textPadding = $(menuItemTextNodes[i]).parent().css('left');
var tmpMatch = textPadding.match(regex);
// if left attribute is defined with numbers & width unit is px
if(tmpMatch.length == 1 && textPadding.replace(regex, '') == "px") {
menuItemLastWidth = tmpMatch[0] * 2 + textWidth;
$(menuItemTextNodes[i]).parent().width(textWidth);
$(menuItemTextNodes[i]).parent().parent().width(menuItemLastWidth);
}
// set left attribute of the muse container of this menu item
if(menuItemLastWidth) {
menuItemAccumulateWidth = menuItemAccumulateWidth == 0 ? menuItemLastWidth : menuItemAccumulateWidth + menuItemLastWidth + 2;
var position = menuBarRight - menuBarLeft - menuItemAccumulateWidth;
$(menuItemTextNodes[i]).parent().parent().parent().css('left', position + "px");
}
}
}
// CSS class for active menu
var menuItemLinkNodes = $('a.MenuItem');
for(var i = 0; i < menuItemLinkNodes.length; i++) {
var linkTarget = document.baseURI + $(menuItemLinkNodes[i]).attr('href');
var linkCurrent = document.URL;
if(linkTarget == linkCurrent && !$(menuItemLinkNodes[i]).hasClass('MuseMenuActive')) {
!$(menuItemLinkNodes[i]).addClass('MuseMenuActive');
}
}
});
/*
* getStyleObject Plugin for jQuery JavaScript Library
* From: http://upshots.org/?p=112
* Ref: http://stackoverflow.com/questions/754607/can-jquery-get-all-css-styles-associated-with-an-element/6416527#6416527
*/
/*
(function($){
$.fn.getStyleObject = function(){
var dom = this.get(0);
var style;
var returns = {};
if(window.getComputedStyle){
var camelize = function(a,b){
return b.toUpperCase();
};
style = window.getComputedStyle(dom, null);
for(var i = 0, l = style.length; i < l; i++){
var prop = style[i];
var camel = prop.replace(/\-([a-z])/g, camelize);
var val = style.getPropertyValue(prop);
returns[camel] = val;
};
return returns;
};
if(style = dom.currentStyle){
for(var prop in style){
returns[prop] = style[prop];
};
return returns;
};
return this.css();
}
$.fn.copyCSS = function(source){
var styles = $(source).getStyleObject();
this.css(styles);
}
})(jQuery);
*/