-
Notifications
You must be signed in to change notification settings - Fork 3
/
minimize.js
169 lines (122 loc) · 4.85 KB
/
minimize.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* boxzilla minimized mode - hotjar poll style implementation
*
* box can be minimized, but not dismissed. after first dismissal, triggers
* in minimized mode by default (cookie).
*
* Made by https://github.com/lkraav (a Boxzilla user) and not officially maintained by the Boxzilla team.
* Might or might not work for your theme / website.
*
* @see https://github.com/ibericode/boxzilla-wp/issues/87
* @since 2017.02.05
*/
$(document).ready( function() {
if ( typeof Boxzilla === "undefined" ) {
return;
}
//set the slug for the box you want to apply this to.
var boxzillaSlug = "the-post-slug-of-your-boxzilla-box-goes-here";
/**
* overrides `Box.prototype.toggle()`
*
* @since 2017.02.05
*/
function toggleMinimize( show ) {
// revert visibility if no explicit argument is given
if ( typeof show === "undefined" ) {
show = !this.visible;
}
// is box already at desired visibility?
// minimized mode always needs action
if ( show === ( this.visible && ! this.minimized ) ) {
return false;
}
// set new visibility status
this.visible = show;
// must force show when minimized
// must force show when box has never been dismissed (first load)
show = ( show && this.minimized ) || ( show && ! this.isCookieSet() );
// trigger event
Boxzilla.trigger('box.' + (show ? 'show' : 'hide'), [this]);
return true;
}
/**
* overrides `Box.prototype.dismiss()` close button click handler
*
* @since 2017.02.05
*/
function dismissMinimize( e ) {
// restore
if ( this.minimized ) {
this.show();
return false;
}
// minimize box element
this.hide();
// set cookie
if ( this.config.cookie && this.config.cookie.dismissed ) {
this.setCookie( this.config.cookie.dismissed );
}
this.dismissed = true;
Boxzilla.trigger( 'box.dismiss', [this] );
return true;
}
/**
* overrides default handlers
*
* @since 2017.02.05
*/
Boxzilla.on( "done", function() {
var options = window.boxzilla_options;
for (var i = 0; i < options.boxes.length; i++) {
var boxOpts = options.boxes[i];
var box = Boxzilla.get( boxOpts.id );
if ( boxzillaSlug === box.config.post.slug ) {
// @see http://stackoverflow.com/a/32809957/35946
box.closeIcon.outerHTML = box.closeIcon.outerHTML;
// @see http://stackoverflow.com/a/24446288/35946
$( box.element ).on( "click", "." + box.closeIcon.className, function( e ) {
dismissMinimize.call( box, e );
} );
box.mayAutoShow = function() {
return null === this.minimized;
};
box.minimized = null;
box.toggle = toggleMinimize;
// avoid `addEventListener resize` erroneously restoring minimized state
box.setCustomBoxStyling = function() {};
}
}
} );
/**
* overrides `Box.prototype.show()`, uses `max-height` css.
*
* @since 2017.02.05
*/
Boxzilla.on( "box.show", function( box ) {
if ( boxzillaSlug !== box.config.post.slug ) {
return;
}
$( box.element ).css( { "display": "block", "max-height": "none" } );
$( box.element ).find( ".boxzilla-close-icon" ).html( "×" );
box.minimized = false;
} );
/**
* overrides `Box.prototype.hide()`, uses `max-height` css
*
* @see http://stackoverflow.com/questions/2345784/jquery-get-height-of-hidden-element-in-jquery
* @since 2017.02.05
*/
Boxzilla.on( "box.hide", function( box ) {
if ( boxzillaSlug !== box.config.post.slug ) {
return;
}
$( box.element )
.css( { "display": "block", "max-height": "none", "overflow": "hidden", "visibility": "hidden" } )
.css( { "max-height": $( box.element ).find( ".gform_heading" ).outerHeight() + "px" } )
.css( { "visibility": "visible" } )
;
$( box.element ).find( ".boxzilla-close-icon" ).html( "▴" );
box.minimized = true;
} );
} );