PageSourceSearch

https://posterfortomorrow.org/js/libs/jquery.are-you-sure.js

js posterfortomorrow.org collected 2026-09-26 06:45:11 UTC 5,555 bytes, 192 lines download raw bytes

1/*!
2 * jQuery Plugin: Are-You-Sure (Dirty Form Detection)
3 * https://github.com/codedance/jquery.AreYouSure/
4 *
5 * Copyright (c) 2012-2014, Chris Dance and PaperCut Software http://www.papercut.com/
6 * Dual licensed under the MIT or GPL Version 2 licenses.
7 * http://jquery.org/license
8 *
9 * Author:  [email protected]
10 * Version: 1.9.0
11 * Date:    13th August 2014
12 */
13(function($) {
14  
15  $.fn.areYouSure = function(options) {
16      
17    var settings = $.extend(
18      {
19        'message' : 'You have unsaved changes!',
20        'dirtyClass' : 'dirty',
21        'change' : null,
22        'silent' : false,
23        'addRemoveFieldsMarksDirty' : false,
24        'fieldEvents' : 'change keyup propertychange input',
25        'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])"
26      }, options);
27
28    var getValue = function($field) {
29      if ($field.hasClass('ays-ignore')
30          || $field.hasClass('aysIgnore')
31          || $field.attr('data-ays-ignore')
32          || $field.attr('name') === undefined) {
33        return null;
34      }
35
36      if ($field.is(':disabled')) {
37        return 'ays-disabled';
38      }
39
40      var val;
41      var type = $field.attr('type');
42      if ($field.is('select')) {
43        type = 'select';
44      }
45
46      switch (type) {
47        case 'checkbox':
48        case 'radio':
49          val = $field.is(':checked');
50          break;
51        case 'select':
52          val = '';
53          $field.find('option').each(function(o) {
54            var $option = $(this);
55            if ($option.is(':selected')) {
56              val += $option.val();
57            }
58          });
59          break;
60        default:
61          val = $field.val();
62      }
63
64      return val;
65    };
66
67    var storeOrigValue = function($field) {
68      $field.data('ays-orig', getValue($field));
69    };
70
71    var checkForm = function(evt) {
72
73      var isFieldDirty = function($field) {
74        var origValue = $field.data('ays-orig');
75        if (undefined === origValue) {
76          return false;
77        }
78        return (getValue($field) != origValue);
79      };
80
81      var $form = ($(this).is('form')) 
82                    ? $(this)
83                    : $(this).parents('form');
84
85      // Test on the target first as it's the most likely to be dirty
86      if (isFieldDirty($(evt.target))) {
87        setDirtyStatus($form, true);
88        return;
89      }
90
91      $fields = $form.find(settings.fieldSelector);
92
93      if (settings.addRemoveFieldsMarksDirty) {              
94        // Check if field count has changed
95        var origCount = $form.data("ays-orig-field-count");
96        if (origCount != $fields.length) {
97          setDirtyStatus($form, true);
98          return;
99        }
100      }
101
102      // Brute force - check each field
103      var isDirty = false;
104      $fields.each(function() {
105        $field = $(this);
106        if (isFieldDirty($field)) {
107          isDirty = true;
108          return false; // break
109        }
110      });
111      
112      setDirtyStatus($form, isDirty);
113    };
114
115    var initForm = function($form) {
116      var fields = $form.find(settings.fieldSelector);
117      $(fields).each(function() { storeOrigValue($(this)); });
118      $(fields).unbind(settings.fieldEvents, checkForm);
119      $(fields).bind(settings.fieldEvents, checkForm);
120      $form.data("ays-orig-field-count", $(fields).length);
121      setDirtyStatus($form, false);
122    };
123
124    var setDirtyStatus = function($form, isDirty) {
125      var changed = isDirty != $form.hasClass(settings.dirtyClass);
126      $form.toggleClass(settings.dirtyClass, isDirty);
127        
128      // Fire change event if required
129      if (changed) {
130        if (settings.change) settings.change.call($form, $form);
131
132        if (isDirty) $form.trigger('dirty.areYouSure', [$form]);
133        if (!isDirty) $form.trigger('clean.areYouSure', [$form]);
134        $form.trigger('change.areYouSure', [$form]);
135      }
136    };
137
138    var rescan = function() {
139      var $form = $(this);
140      var fields = $form.find(settings.fieldSelector);
141      $(fields).each(function() {
142        var $field = $(this);
143        if (!$field.data('ays-orig')) {
144          storeOrigValue($field);
145          $field.bind(settings.fieldEvents, checkForm);
146        }
147      });
148      // Check for changes while we're here
149      $form.trigger('checkform.areYouSure');
150    };
151
152    var reinitialize = function() {
153      initForm($(this));
154    }
155
156    if (!settings.silent && !window.aysUnloadSet) {
157      window.aysUnloadSet = true;
158      $(window).bind('beforeunload', function() {
159        $dirtyForms = $("form").filter('.' + settings.dirtyClass);
160        if ($dirtyForms.length == 0) {
161          return;
162        }
163        // Prevent multiple prompts - seen on Chrome and IE
164        if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) {
165          if (window.aysHasPrompted) {
166            return;
167          }
168          window.aysHasPrompted = true;
169          window.setTimeout(function() {window.aysHasPrompted = false;}, 900);
170        }
171        return settings.message;
172      });
173    }
174
175    return this.each(function(elem) {
176      if (!$(this).is('form')) {
177        return;
178      }
179      var $form = $(this);
180        
181      $form.submit(function() {
182        $form.removeClass(settings.dirtyClass);
183      });
184      $form.bind('reset', function() { setDirtyStatus($form, false); });
185      // Add a custom events
186      $form.bind('rescan.areYouSure', rescan);
187      $form.bind('reinitialize.areYouSure', reinitialize);
188      $form.bind('checkform.areYouSure', checkForm);
189      initForm($form);
190    });
191  };
192})(jQuery);

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.