1(function ($, Drupal, once, drupalSettings) { 2 "use strict"; 3 4 Drupal.behaviors.CohesionAccordionTabs = { 5 attach: function (context) { 6 7 var onceTabs = 'cohAccordionTabs'; 8 9 var cmm = new Drupal.CohesionResponsiveBreakpoints(drupalSettings.cohesion.responsive_grid_settings); 10 11 var $at = $('.coh-accordion-tabs > .coh-accordion-tabs-inner', context); 12 13 function matchHeights(elements, remove) { 14 return $(elements).matchHeight({ 15 byRow: false, 16 remove: remove 17 }); 18 } 19 20 /** 21 * Callback when the tabs initially load 22 * @param {type} e 23 * @param {type} tabs 24 * @returns {undefined} 25 */ 26 function tabsLoad(e, tabs) { 27 getTabSettings(tabs); 28 } 29 30 /** 31 * Callback when the tabs have been manually refreshed - normally ajax 32 * @param {type} e 33 * @param {type} tabs 34 * @returns {undefined} 35 */ 36 function tabsRefresh(e, tabs) { 37 38 var opts = tabs.options; 39 40 // Match the heights of the content 41 if (typeof opts.contentMatchHeight !== 'undefined') { 42 matchHeights(tabs.tabs.panels, opts.contentMatchHeight === true ? false : true); 43 } 44 45 // Match the heights of the lid 46 if (typeof opts.tabsMatchHeight !== 'undefined') { 47 matchHeights(tabs.tabs.tabItemAnchors, opts.tabsMatchHeight === true ? false : true); 48 } 49 50 getTabSettings(tabs); 51 } 52 53 /** 54 * Callback when the tabs change state 55 * @param {type} e 56 * @param {type} tabs 57 * @returns {undefined} 58 */ 59 function tabsStateChange(e, tabs) { 60 61 var opts = tabs.tabs.options; 62 63 // Match the heights of the content 64 if (typeof opts.contentMatchHeight !== 'undefined') { 65 matchHeights(tabs.tabs.panels, opts.contentMatchHeight === true ? false : true); 66 } 67 68 // Match the heights of the lid 69 if (typeof opts.tabsMatchHeight !== 'undefined') { 70 matchHeights(tabs.tabs.tabItemAnchors, opts.tabsMatchHeight === true ? false : true); 71 } 72 } 73 74 /** 75 * Callback when switching between tabs and will return the activated tab object 76 * @param {type} e 77 * @param {type} tab 78 * @returns {undefined} 79 */ 80 function tabsActivate(e, tab) { 81 82 // Update Drupal behaviors 83 for (var i = 0; i < tab.panel.length; i++) { 84 Drupal.attachBehaviors(tab.panel[i]); 85 } 86 } 87 88 /** 89 * Callback function to update settings when a breakpoint changes 90 * @param {type} settings 91 * @returns {undefined} 92 */ 93 function updateSettings(settings) { 94 95 var key = settings.cohesion.key; 96 settings = settings.cohesion.settings; 97 98 settings.$element.responsiveTabs(settings.breakpoints[key]); 99 100 // Update the settings for each of the tabs 101 for (var i = 0; i < settings.act.tabs.length; i++) { 102 if (settings.act.tabs[i].hide !== false) { 103 $(settings.act.tabs[i].accordionTab).toggleClass('is-hidden', settings.act.tabs[i].hide[key]); 104 $(settings.act.tabs[i].tab).toggleClass('is-hidden', settings.act.tabs[i].hide[key]); 105 } 106 } 107 } 108 109 /** 110 * 111 * @param {type} settings 112 * @param {type} key 113 * @returns {undefined} 114 */ 115 function manageSettings(settings, key) { 116 117 // Handle non-breakpointed settings (these are passed to the breakpointed settings) 118 // setHash 119 if (typeof settings.setHash !== 'undefined') { 120 settings.styles[key].setHash = settings.setHash; 121 122 // Set the behavior when accordion view to autoscroll if a hash is selected 123 if(settings.styles[key].accordionOrTab === 'accordion') { 124 settings.styles[key].scrollToAccordionOnLoad = true; 125 } 126 } 127 128 // scrollToAccordion 129 if (typeof settings.scrollToAccordion !== 'undefined') { 130 settings.styles[key].scrollToAccordion = settings.scrollToAccordion; 131 } 132 133 // scrollToAccordionOffsetClass 134 if (typeof settings.scrollToAccordionOffsetClass !== 'undefined' && typeof settings.offsetPositionAgainst !== 'undefined' && settings.offsetPositionAgainst === 'class') { 135 136 var offsetClass = settings.scrollToAccordionOffsetClass.match(/^[.]/) ? settings.scrollToAccordionOffsetClass : '.' + settings.scrollToAccordionOffsetClass; 137 138 settings.styles[key].scrollToAccordionOffset = $(offsetClass).outerHeight(true); 139 } 140 141 // Handle breakpointed settings 142 var breakpoint = settings.styles[key]; 143 144 // The active property on the form is from 1 but the plugin expect it to be from 0 so -1 to it 145 if (typeof breakpoint.active !== 'undefined') { 146 settings.styles[key].active = (parseInt(breakpoint.active) - 1).toString(); 147 } 148 149 // Handle a custom animation speed 150 if (typeof breakpoint.duration !== 'undefined' && typeof breakpoint.durationMs !== 'undefined' && breakpoint.duration === 'custom') { 151 settings.styles[key].duration = parseInt(breakpoint.durationMs); 152 } else if (typeof breakpoint.duration !== 'undefined' && breakpoint.duration !== 'custom') { 153 //ensure duration is a number 154 settings.styles[key].duration = parseInt(breakpoint.duration); 155 } 156 return settings; 157 } 158 159 /** 160 * Get the default and breakpointed settings 161 * @param {type} $el 162 * @param {type} settings 163 * @returns {unresolved} 164 */ 165 function getSettings($el, settings) { 166 167 // Set the defaults 168 var defaults = { 169 170 classes: { 171 stateDefault: '', 172 stateActive: 'is-active', 173 stateDisabled: 'is-disabled', 174 stateExcluded: 'is-excluded', 175 container: '', 176 ul: '', 177 tab: '', 178 anchor: '', 179 panel: '', 180 accordionTitle: 'coh-accordion-title', 181 stateTypePrefix: 'coh-accordion-tabs-display' 182 } 183 }; 184 185 settings.breakpoints = {}; 186 settings.$element = $el; 187 188 // Manage the settings 189 190 // Update the settings prior to attaching the listeners 191 for (var i = 0; i < cmm.breakpoints.length; i++) { 192 193 var key = cmm.breakpoints[i].key; 194 195 // Populate all breakpoints regardless of whether the settings are set or not to simulate inheritance 196 settings.breakpoints[key] = {}; 197 198 $.extend(settings.breakpoints[key], defaults); 199 200 if (typeof settings.styles[key] === 'object') { 201 202 // Some settings need to be manually updated 203 settings = manageSettings(settings, key); 204 205 $.extend(settings.breakpoints[key], settings.styles[key]); 206 $.extend(defaults, settings.styles[key]); 207 } 208 209 if(typeof settings.breakpoints[key].animation !== 'undefined') { 210 211 switch(settings.breakpoints[key].animation) { 212 case 'slide': 213 settings.breakpoints[key].animationQueue = false;
214 break; 215 case 'fade': 216 settings.breakpoints[key].animationQueue = true; 217 break; 218 default: 219 settings.breakpoints[key].animationQueue = true; 220 break; 221 } 222 } 223 } 224 225 return settings; 226 } 227 228 /** 229 * Get the settings for each tab 230 * @param {type} tabs 231 * @returns {undefined} 232 */ 233 function getTabSettings(tabs) { 234 235 // Manage tabs responsive settings 236 for (var i = 0; i < tabs.tabs.length; i++) { 237 238 // Visibility settings 239 var previous, key; 240 if (tabs.tabs[i].hide !== false && typeof tabs.tabs[i].hide === 'object') { 241 242 for (var c = 0; c < cmm.breakpoints.length; c++) { 243 244 key = cmm.breakpoints[c].key; 245 246 if (typeof tabs.tabs[i].hide[key] === 'undefined') { 247 tabs.tabs[i].hide[key] = previous; 248 } 249 previous = tabs.tabs[i].hide[key]; 250 } 251 } 252 } 253 } 254 255 /** 256 * Initialise each instance of Accordion tabs 257 */ 258 $.each($at, function (i, e) { 259 260 var $this = $(e); 261 262 var $onecd = once.filter(onceTabs, $this); 263 264 // Has been initialised previously (must be checked first otherwise it gets bound next) 265 once.filter(onceTabs, $this).forEach(function (e, i) { 266 267 var $f = $(e); 268 269 // Refresh the tabs and the settings to makesure it's upto date with all the latest tabs etc 270 $f.responsiveTabs('refresh'); 271 }); 272 273 // No need to do anything after this as we only want to refresh ^^ 274 if($onecd.length > 0) { 275 return true; 276 } 277 278 // Init the tabs 279 var settings = getSettings($this, $this.data('cohAccordion')); 280 281 // Bind the custom events 282 $this.on('tabs-load', tabsLoad); 283 $this.on('tabs-refresh', tabsRefresh); 284 $this.on('tabs-activate-state', tabsStateChange); 285 $this.on('tabs-activate', tabsActivate); 286 287 $(once(onceTabs, $this)).responsiveTabs(settings.breakpoints[cmm.getCurrentBreakpoint().key]); 288 289 // Pass the object for the Accordion tabs to the callback settings 290 settings.act = $.data(this, 'responsivetabs'); 291 292 cmm.addListeners(settings, updateSettings); 293 }); 294 } 295 }; 296 297})(jQuery, Drupal, once, drupalSettings);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.