1/* 2 * Initialize JavaScript for the NF User Analytics plugin. 3 */ 4jQuery( document ).ready( function ( $ ) { 5 6 /** 7 * Controller for the plugin. 8 */ 9 var NFUserAnalyticsController = Marionette.Object.extend( { 10 11 /** 12 * Functionality to detect which browser and browser version is used. 13 */ 14 DetectBrowser: { 15 init: function () { 16 this.browser = this.searchString( this.dataBrowser ) || "Unknown browser"; 17 this.version = this.searchVersion( navigator.userAgent ) || this.searchVersion( navigator.appVersion ) || "Unknown version"; 18 }, 19 searchString: function ( data ) { 20 for ( var i = 0; i < data.length; i++ ) { 21 var dataString = data[ i ].string; 22 var dataProp = data[ i ].prop; 23 this.versionSearchString = data[ i ].versionSearch || data[ i ].identity; 24 if ( dataString ) { 25 if ( dataString.indexOf( data[ i ].subString ) != -1 ) { 26 return data[ i ].identity; 27 } 28 } 29 else if ( dataProp ) { 30 return data[ i ].identity; 31 } 32 } 33 }, 34 searchVersion: function ( dataString ) { 35 var index = dataString.indexOf( this.versionSearchString ); 36 if ( index == -1 ) return; 37 return parseFloat( dataString.substring( index + this.versionSearchString.length + 1 ) ); 38 }, 39 dataBrowser: [ 40 { 41 string: navigator.userAgent, 42 subString: "Edg", 43 identity: "Edge" 44 }, { 45 string: navigator.userAgent, 46 subString: "Chrome", 47 identity: "Chrome" 48 }, { 49 string: navigator.userAgent, 50 subString: "OmniWeb", 51 versionSearch: "OmniWeb/", 52 identity: "OmniWeb" 53 }, { 54 string: navigator.vendor, 55 subString: "Apple", 56 identity: "Safari", 57 versionSearch: "Version" 58 }, { 59 prop: window.opera, 60 identity: "Opera" 61 }, { 62 string: navigator.vendor, 63 subString: "iCab", 64 identity: "iCab" 65 }, { 66 string: navigator.vendor, 67 subString: "KDE", 68 identity: "Konqueror" 69 }, { 70 string: navigator.userAgent, 71 subString: "Firefox", 72 identity: "Firefox" 73 }, { 74 string: navigator.vendor, 75 subString: "Camino", 76 identity: "Camino" 77 }, { // for newer Netscapes (6+) 78 string: navigator.userAgent, 79 subString: "Netscape", 80 identity: "Netscape" 81 }, { 82 string: navigator.userAgent, 83 subString: "MSIE", 84 identity: "Internet Explorer", 85 versionSearch: "MSIE" 86 }, { 87 string: navigator.userAgent, 88 subString: "Gecko", 89 identity: "Mozilla", 90 versionSearch: "rv" 91 }, { // for older Netscapes (4-) 92 string: navigator.userAgent, 93 subString: "Mozilla", 94 identity: "Netscape", 95 versionSearch: "Mozilla" 96 } ] 97 }, 98 99 hostInfoData: [], 100 101 /** 102 * Initialize the plugin. 103 */ 104 initialize: function() { 105 this.DetectBrowser.init(); 106 this.listenTo( nfRadio.channel( 'form' ), 'render:view', this.updateFormByView ); 107 }, 108 109 /** 110 * Add localized styling to ensure UA fields have no default margin. 111 * 112 * @param {collection} fields 113 */ 114 addStyles: function(fields) { 115 let uaFields = []; 116 _.each(fields.models, function(field) { 117 let type = field.get('type'); 118 if( type.startsWith('user-analytics-') ) { 119 uaFields.push('.' + type + '-container'); 120 } 121 }); 122 let style = uaFields.join(', ') + ' { margin: 0px !important; }'; 123 let el = document.createElement('style'); 124 el.innerHTML = style; 125 document.head.appendChild(el); 126 }, 127 128 /** 129 * Update the form when the view is rendered 130 * 131 * @param object view Rendered view 132 */ 133 updateFormByView: function( view ) { 134 this.addStyles(view.model.get('fields')); 135 this.updateForm( $( view.el ) ); 136 }, 137 138 /** 139 * Update the form by filling in the User Analytics fields. 140 * 141 * @param object $form Form element on the page 142 */ 143 updateForm: function( $form ) { 144 var data = { 145 'browser': this.DetectBrowser.browser, 146 'browser-version': this.DetectBrowser.version, 147 'os': window.navigator.platform 148 }; 149 150 for ( var key in data ) { 151 var value = data[key]; 152 var $el = $form.find( '.user-analytics-' + key + '-container' + ' .
152nf-element' ); 153 if ( $el.length ) { 154 $el.val( value ).trigger( 'change' ); 155 } 156 } 157 }, 158 159 } ); 160 161 new NFUserAnalyticsController(); 162 163} );
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.