-
Notifications
You must be signed in to change notification settings - Fork 662
/
feedback.js
160 lines (137 loc) · 4.25 KB
/
feedback.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
/**
* @title WET-BOEW Feedback form
* @overview Allows users to submit feedback for a specific Web page or Web site.
* @license wet-boew.github.io/wet-boew/License-en.html / wet-boew.github.io/wet-boew/Licence-fr.html
* @author @pjackson28
*/
( function( $, window, document, wb ) {
"use strict";
/*
* Variable and function definitions.
* These are global to the plugin - meaning that they will be initialized once per page,
* not once per instance of plugin on the page. So, this is a good place to define
* variables that are common to all instances of the plugin on a page.
*/
var componentName = "wb-fdbck",
selector = "." + componentName,
initEvent = "wb-init" + selector,
$document = wb.doc,
fbrsn, fbaxs, fbcntc1, fbcntc2, $fbweb, $fbmob, $fbcomp, $fbinfo,
/**
* @method init
* @param {jQuery Event} event Event that triggered this handler
*/
init = function( event ) {
// Start initialization
// returns DOM object = proceed with init
// returns undefined = do not proceed with init (e.g., already initialized)
var elm = wb.init( event, componentName, selector ),
referrerUrl = document.referrer,
$elm, $fbrsn, urlParams;
if ( elm ) {
$elm = $( elm );
$fbrsn = $elm.find( "#fbrsn" );
urlParams = wb.pageUrlParts.params;
// Cache the form areas
fbrsn = $fbrsn[ 0 ];
fbaxs = document.getElementById( "fbaxs" );
fbcntc1 = document.getElementById( "fbcntc1" );
fbcntc2 = document.getElementById( "fbcntc2" );
$fbweb = $elm.find( "#fbweb" );
$fbmob = $fbweb.find( "#fbmob" );
$fbcomp = $fbweb.find( "#fbcomp" );
$fbinfo = $elm.find( "#fbinfo" );
// Set the initial value for the fbrsn field based on the query string
if ( !urlParams.submit && urlParams.fbrsn ) {
$fbrsn.find( "option[value='" + urlParams.fbrsn + "']" ).attr( "selected", "selected" );
}
// Set aria-controls
fbrsn.setAttribute( "aria-controls", "fbweb" );
fbaxs.setAttribute( "aria-controls", "fbmob fbcomp" );
// Set the initial show/hide state of the form
showHide( fbrsn );
showHide( fbaxs );
showHide( fbcntc1 );
showHide( fbcntc2 );
// Prepopulates URL form field with referrer
document.getElementById( "fbpg" ).setAttribute( "value", referrerUrl );
// Identify that initialization has completed
wb.ready( $elm, componentName );
}
},
/**
* @method showHide
* @param {DOM element} elm The element triggering the show/hide
*/
showHide = function( elm ) {
var $hide, $show,
classHide = "hide",
classShow = "show",
funcToggle = "toggle",
targetId = elm.id;
switch ( targetId ) {
case "fbrsn":
if ( elm.value === "web" ) {
$show = $fbweb;
} else {
$hide = $fbweb;
}
break;
case "fbaxs":
if ( elm.value === "mobile" ) {
$show = $fbmob;
$hide = $fbcomp;
} else {
$show = $fbcomp;
$hide = $fbmob;
}
break;
case "fbcntc1":
case "fbcntc2":
if ( document.getElementById( "fbcntc1" ).checked || document.getElementById( "fbcntc2" ).checked ) {
$show = $fbinfo;
} else {
$hide = $fbinfo;
}
break;
}
// Element to show
if ( $show ) {
$show
.attr( "aria-hidden", "false" )
.wb( funcToggle, classShow, classHide );
}
// Element to hide
if ( $hide ) {
$hide
.attr( "aria-hidden", "true" )
.wb( funcToggle, classHide, classShow );
}
};
// Bind the init event of the plugin
$document.on( "timerpoke.wb " + initEvent, selector, init );
// Show/hide form areas when certain form fields are changed
$document.on( "keydown click change", "#fbrsn, #fbaxs, #fbcntc1, #fbcntc2", function( event ) {
var which = event.which;
// Ignore middle/right mouse buttons
if ( !which || which === 1 ) {
showHide( event.target );
}
} );
// Return to the form defaults when the reset button is activated
$document.on( "click", selector + " input[type=reset]", function( event ) {
var which = event.which;
// Ignore middle/right mouse buttons
if ( !which || which === 1 ) {
// Manually reset the form as this event handler can be triggered
// before the browser invokes the native form reset.
event.target.form.reset();
showHide( fbrsn );
showHide( fbaxs );
showHide( fbcntc1 );
showHide( fbcntc2 );
}
} );
// Add the timer poke to initialize the plugin
wb.add( selector );
} )( jQuery, window, document, wb );