1/*jslint */ 2/*global module: true, exports: true, require: false, define: false */ 3 4/** 5 * jaaulde-jquery-cookies.js 6 * 7 * Copyright (c) 2005-2015, Jim Auldridge MIT License 8 * 9 */ 10 11/** 12 * 13 * @param {object} scope - a reference to the call scope 14 * @param {undefined} undef - an undefined variable for comparison checks 15 * @returns {void} 16 */ 17(function (scope, undef) { 18 'use strict'; 19 20 /** 21 * IIFE for injecting jquery-cookies module into jQuery via whichever require/scope is in use 22 * 23 * @param {function} definition - function which produces the plugin methods and adds them to the injected jQuery's prototype 24 * @returns {void} 25 */ 26 (function (definition) { 27 if (typeof module === 'object' && module !== null && module.exports) { 28 module.exports = exports = definition(require('jquery'), require('cookies')); 29 } else if (typeof define === 'function' && define.amd) { 30 define(['jquery', 'cookies'], definition); 31 } else { 32 definition(scope.jQuery, scope.cookies); 33 } 34 }(function ($, cookies) { 35 var NameTokenIterator, 36 nti; 37 38 NameTokenIterator = function () { 39 this.rewind(); 40 }; 41 42 NameTokenIterator.prototype = { 43 next: function () { 44 this.current = this.name_token_attrs.shift(); 45 46 return this.current !== undef; 47 }, 48 rewind: function () { 49 this.current = undef; 50 51 this.name_token_attrs = [ 52 'name', 53 'id' 54 ]; 55 } 56 }; 57 58 nti = new NameTokenIterator(); 59 60 /* 61 * Alias `cookies` as `$.cookies` 62 */ 63 $.cookies = cookies; 64 65 /* 66 * Extend the jQuery prototype with our new plugin methods 67 */ 68 $.extend($.fn, { 69 /** 70 * $('selector').cookify - set the value of an input field, or the innerHTML of an element, to a cookie by the name or id of the field or element 71 * (field or element MUST have name or id attribute) 72 * 73 * @access public 74 * @param options OBJECT - list of cookie options to specify 75 * @see https://github.com/JAAulde/cookies#cookie-options 76 * @return jQuery 77 */ 78 cookify: function (options) { 79 return this 80 /* 81 * Iterate radio and checkbox inputs 82 * `cookify` not yet implemented for radios and checkboxes 83 */ 84 .filter(':radio, :checkbox').each($.noop) 85 .end() 86 /* 87 * Iterate all other elements 88 */ 89 .not(':radio, :checkbox').each(function () { 90 var $this, 91 name_token_value, 92 value; 93 94 $this = $(this); 95 96 nti.rewind(); 97 98 while (nti.next()) { 99 name_token_value = $this.attr(nti.current); 100 101 if (typeof name_token_value === 'string' && name_token_value !== '') { 102 value = $this.is(':input') ? $this.val() : $this.html(); 103 104 cookies.set( 105 name_token_value, 106 (typeof value === 'string' && value !== '') ? value : null, 107 options 108 ); 109 110 break; 111 } 112 } 113 }) 114 .end(); 115 }, 116 /** 117 * $('selector').cookieFill - set the value of an input field or the innerHTML of an element from a cookie by the name or id of the field or element 118 * 119 * @access public 120 * @return jQuery 121 */ 122 cookieFill: function () { 123 return this 124 /* 125 * Iterate radio and checkbox inputs 126 * `cookieFill` not yet implemented for radios and checkboxes 127 */ 128 .filter(':radio, :checkbox').each($.noop) 129 .end() 130 /* 131 * Iterate all other elements 132 */ 133 .not(':radio, :checkbox').each(function () { 134 var $this, 135 name_token_value, 136 value; 137 138 $this = $(this); 139 140 nti.rewind(); 141 142 while (nti.next()) { 143 name_token_value = $this.attr(nti.current); 144 145 if (typeof name_token_value === 'string' && name_token_value !== '') { 146 value = cookies.get(name_token_value); 147 148 if (value !== null) { 149 if ($this.is(':input')) { 150 $this.val(value); 151 } else { 152 $this.html(value); 153 } 154 } 155 156 break; 157 } 158 } 159 }) 160 .end(); 161 }, 162 /** 163 * $('selector').cookieBind - call cookieFill() on matching input elements, and bind their change events to cookify() 164 * 165 * @access public 166 * @param options OBJECT - list of cookie options to specify 167 * @see https://github.com/JAAulde/cookies#cookie-options 168 * @return jQuery 169 */ 170 cookieBind: function (options) { 171 return this 172 /* 173 * Only run on inputs because of bind to `change` event 174 */ 175 .filter(':input').each(function () { 176 var $this = $(this); 177 178 $this.cookieFill().on('change', function () { 179 $this.cookify(options); 180 }); 181 }) 182 .end(); 183 } 184 }); 185 })); 186}(this));
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.