1/*jslint browser: true */ /*global jQuery: true */ 2 3/** 4 * jQuery Cookie plugin 5 * 6 * Copyright (c) 2010 Klaus Hartl (stilbuero.de) 7 * Dual licensed under the MIT and GPL licenses: 8 * http://www.opensource.org/licenses/mit-license.php 9 * http://www.gnu.org/licenses/gpl.html 10 * 11 */ 12 13// TODO JsDoc 14 15/** 16 * Create a cookie with the given key and value and other optional parameters. 17 * 18 * @example $.cookie('the_cookie', 'the_value'); 19 * @desc Set the value of a cookie. 20 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true }); 21 * @desc Create a cookie with all available options. 22 * @example $.cookie('the_cookie', 'the_value'); 23 * @desc Create a session cookie. 24 * @example $.cookie('the_cookie', null); 25 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain 26 * used when the cookie was set. 27 * 28 * @param String key The key of the cookie. 29 * @param String value The value of the cookie. 30 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes. 31 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object. 32 * If a negative value is specified (e.g. a date in the past), the cookie will be deleted. 33 * If set to null or omitted, the cookie will be a session cookie and will not be retained 34 * when the the browser exits. 35 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie). 36 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie). 37 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will 38 * require a secure protocol (like HTTPS). 39 * @type undefined 40 * 41 * @name $.cookie 42 * @cat Plugins/Cookie 43 * @author Klaus Hartl/[email protected] 44 */ 45 46/** 47 * Get the value of a cookie with the given key. 48 * 49 * @example $.cookie('the_cookie'); 50 * @desc Get the value of a cookie. 51 * 52 * @param String key The key of the cookie. 53 * @return The value of the cookie. 54 * @type String 55 * 56 * @name $.cookie 57 * @cat Plugins/Cookie 58 * @author Klaus Hartl/[email protected] 59 */ 60jQuery.cookie = function (key, value, options) { 61 62 // key and value given, set cookie... 63 if (arguments.length > 1 && (value === null || typeof value !== "object")) { 64 options = jQuery.extend({}, options); 65 66 if (value === null) { 67 options.expires = -1; 68 } 69 70 if (typeof options.expires === 'number') { 71 var days = options.expires, t = options.expires = new Date(); 72 t.setDate(t.getDate() + days); 73 } 74 75 return (document.cookie = [ 76 encodeURIComponent(key), '=', 77 options.raw ? String(value) : encodeURIComponent(String(value)), 78 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE 79 options.path ? '; path=' + options.path : '', 80 options.domain ? '; domain=' + options.domain : '', 81 options.secure ? '; secure' : '' 82 ].join('')); 83 } 84 85 // key and possibly options given, get cookie... 86 options = value || {}; 87 var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent; 88 return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null; 89}; 90; 91(function ($) { 92 93/** 94 * Prevents consecutive form submissions of identical form values. 95 * 96 * Repetitive form submissions that would submit the identical form values are 97 * prevented, unless the form values are different from the previously 98 * submitted values. 99 * 100 * This is a simplified re-implementation of a user-agent behavior that should 101 * be natively supported by major web browsers, but at this time, only Firefox 102 * has a built-in protection. 103 * 104 * A form value-based approach ensures that the constraint is triggered for 105 * consecutive, identical form submissions only. Compared to that, a form 106 * button-based approach would (1) rely on [visible] buttons to exist where 107 * technically not required and (2) require more complex state management if 108 * there are multiple buttons in a form. 109 * 110 * This implementation is based on form-level submit events only and relies on 111 * jQuery's serialize() method to determine submitted form values. As such, the 112 * following limitations exist: 113 * 114 * - Event handlers on form buttons that preventDefault() do not receive a 115 * double-submit protection. That is deemed to be fine, since such button 116 * events typically trigger reversible client-side or server-side operations 117 * that are local to the context of a form only. 118 * - Changed values in advanced form controls, such as file inputs, are not part 119 * of the form values being compared between consecutive form submits (due to 120 * limitations of jQuery.serialize()). That is deemed to be acceptable, 121 * because if the user forgot to attach a file, then the size of HTTP payload 122 * will most likely be small enough to be fully passed to the server endpoint 123 * within (milli)seconds. If a user mistakenly attached a wrong file and is
124 * technically versed enough to cancel the form submission (and HTTP payload) 125 * in order to attach a different file, then that edge-case is not supported 126 * here. 127 * 128 * Lastly, all forms submitted via HTTP GET are idempotent by definition of HTTP 129 * standards, so excluded in this implementation. 130 */ 131Drupal.behaviors.formSingleSubmit = { 132 attach: function () { 133 function onFormSubmit (e) { 134 var $form = $(e.currentTarget); 135 var formValues = $form.serialize(); 136 var previousValues = $form.attr('data-drupal-form-submit-last'); 137 if (previousValues === formValues) { 138 e.preventDefault(); 139 } 140 else { 141 $form.attr('data-drupal-form-submit-last', formValues); 142 } 143 } 144 145 $('body').once('form-single-submit') 146 .delegate('form:not([method~="GET"])', 'submit.singleSubmit', onFormSubmit); 147 } 148}; 149 150})(jQuery); 151;
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.