-
Notifications
You must be signed in to change notification settings - Fork 141
1.9 dev #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dafriend
wants to merge
7
commits into
codedance:1.9-dev
Choose a base branch
from
dafriend:1.9-dev
base: 1.9-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
1.9 dev #54
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,190 +1,195 @@ | ||
| /*! | ||
| * jQuery Plugin: Are-You-Sure (Dirty Form Detection) | ||
| * https://github.com/codedance/jquery.AreYouSure/ | ||
| * | ||
| * Copyright (c) 2012-2014, Chris Dance and PaperCut Software http://www.papercut.com/ | ||
| * Dual licensed under the MIT or GPL Version 2 licenses. | ||
| * http://jquery.org/license | ||
| * | ||
| * Author: chris.dance@papercut.com | ||
| * Version: 1.8.0 | ||
| * Date: 22nd June 2014 | ||
| */ | ||
| (function($) { | ||
|
|
||
| $.fn.areYouSure = function(options) { | ||
|
|
||
| var settings = $.extend( | ||
| { | ||
| 'message' : 'You have unsaved changes!', | ||
| 'dirtyClass' : 'dirty', | ||
| 'change' : null, | ||
| 'silent' : false, | ||
| 'addRemoveFieldsMarksDirty' : false, | ||
| 'fieldEvents' : 'change keyup propertychange input', | ||
| 'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])" | ||
| }, options); | ||
|
|
||
| var getValue = function($field) { | ||
| if ($field.hasClass('ays-ignore') | ||
| || $field.hasClass('aysIgnore') | ||
| || $field.attr('data-ays-ignore') | ||
| || $field.attr('name') === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($field.is(':disabled')) { | ||
| return 'ays-disabled'; | ||
| } | ||
|
|
||
| var val; | ||
| var type = $field.attr('type'); | ||
| if ($field.is('select')) { | ||
| type = 'select'; | ||
| } | ||
|
|
||
| switch (type) { | ||
| case 'checkbox': | ||
| case 'radio': | ||
| val = $field.is(':checked'); | ||
| break; | ||
| case 'select': | ||
| if($field.val()) { | ||
| val = $field.val().toString(); | ||
| } else { | ||
| val = $field.prop("selectedIndex").toString(); | ||
| } | ||
| break; | ||
| default: | ||
| val = $field.val(); | ||
| } | ||
|
|
||
| return val; | ||
| }; | ||
|
|
||
| var storeOrigValue = function($field) { | ||
| $field.data('ays-orig', getValue($field)); | ||
| }; | ||
|
|
||
| var checkForm = function(evt) { | ||
|
|
||
| var isFieldDirty = function($field) { | ||
| var origValue = $field.data('ays-orig'); | ||
| if (undefined === origValue) { | ||
| return false; | ||
| } | ||
| return (getValue($field) != origValue); | ||
| }; | ||
|
|
||
| var $form = ($(this).is('form')) | ||
| ? $(this) | ||
| : $(this).parents('form'); | ||
|
|
||
| // Test on the target first as it's the most likely to be dirty | ||
| if (isFieldDirty($(evt.target))) { | ||
| setDirtyStatus($form, true); | ||
| return; | ||
| } | ||
|
|
||
| $fields = $form.find(settings.fieldSelector); | ||
|
|
||
| if (settings.addRemoveFieldsMarksDirty) { | ||
| // Check if field count has changed | ||
| var origCount = $form.data("ays-orig-field-count"); | ||
| if (origCount != $fields.length) { | ||
| setDirtyStatus($form, true); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Brute force - check each field | ||
| var isDirty = false; | ||
| $fields.each(function() { | ||
| $field = $(this); | ||
| if (isFieldDirty($field)) { | ||
| isDirty = true; | ||
| return false; // break | ||
| } | ||
| }); | ||
|
|
||
| setDirtyStatus($form, isDirty); | ||
| }; | ||
|
|
||
| var initForm = function($form) { | ||
| var fields = $form.find(settings.fieldSelector); | ||
| $(fields).each(function() { storeOrigValue($(this)); }); | ||
| $(fields).unbind(settings.fieldEvents, checkForm); | ||
| $(fields).bind(settings.fieldEvents, checkForm); | ||
| $form.data("ays-orig-field-count", $(fields).length); | ||
| setDirtyStatus($form, false); | ||
| }; | ||
|
|
||
| var setDirtyStatus = function($form, isDirty) { | ||
| var changed = isDirty != $form.hasClass(settings.dirtyClass); | ||
| $form.toggleClass(settings.dirtyClass, isDirty); | ||
|
|
||
| // Fire change event if required | ||
| if (changed) { | ||
| if (settings.change) settings.change.call($form, $form); | ||
|
|
||
| if (isDirty) $form.trigger('dirty.areYouSure', [$form]); | ||
| if (!isDirty) $form.trigger('clean.areYouSure', [$form]); | ||
| $form.trigger('change.areYouSure', [$form]); | ||
| } | ||
| }; | ||
|
|
||
| var rescan = function() { | ||
| var $form = $(this); | ||
| var fields = $form.find(settings.fieldSelector); | ||
| $(fields).each(function() { | ||
| var $field = $(this); | ||
| if (!$field.data('ays-orig')) { | ||
| storeOrigValue($field); | ||
| $field.bind(settings.fieldEvents, checkForm); | ||
| } | ||
| }); | ||
| // Check for changes while we're here | ||
| $form.trigger('checkform.areYouSure'); | ||
| }; | ||
|
|
||
| var reinitialize = function() { | ||
| initForm($(this)); | ||
| } | ||
|
|
||
| if (!settings.silent && !window.aysUnloadSet) { | ||
| window.aysUnloadSet = true; | ||
| $(window).bind('beforeunload', function() { | ||
| $dirtyForms = $("form").filter('.' + settings.dirtyClass); | ||
| if ($dirtyForms.length == 0) { | ||
| return; | ||
| } | ||
| // Prevent multiple prompts - seen on Chrome and IE | ||
| if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) { | ||
| if (window.aysHasPrompted) { | ||
| return; | ||
| } | ||
| window.aysHasPrompted = true; | ||
| window.setTimeout(function() {window.aysHasPrompted = false;}, 900); | ||
| } | ||
| return settings.message; | ||
| }); | ||
| } | ||
|
|
||
| return this.each(function(elem) { | ||
| if (!$(this).is('form')) { | ||
| return; | ||
| } | ||
| var $form = $(this); | ||
|
|
||
| $form.submit(function() { | ||
| $form.removeClass(settings.dirtyClass); | ||
| }); | ||
| $form.bind('reset', function() { setDirtyStatus($form, false); }); | ||
| // Add a custom events | ||
| $form.bind('rescan.areYouSure', rescan); | ||
| $form.bind('reinitialize.areYouSure', reinitialize); | ||
| $form.bind('checkform.areYouSure', checkForm); | ||
| initForm($form); | ||
| }); | ||
| }; | ||
| })(jQuery); | ||
| /*! | ||
| * jQuery Plugin: Are-You-Sure (Dirty Form Detection) | ||
| * https://github.com/codedance/jquery.AreYouSure/ | ||
| * | ||
| * Copyright (c) 2012-2014, Chris Dance and PaperCut Software http://www.papercut.com/ | ||
| * Dual licensed under the MIT or GPL Version 2 licenses. | ||
| * http://jquery.org/license | ||
| * | ||
| * Author: chris.dance@papercut.com | ||
| * Version: 1.8.0 | ||
| * Date: 22nd June 2014 | ||
| */ | ||
| (function($) { | ||
|
|
||
| $.fn.areYouSure = function(options) { | ||
|
|
||
| var settings = $.extend( | ||
| { | ||
| 'message' : 'You have unsaved changes!', | ||
| 'dirtyClass' : 'dirty', | ||
| 'change' : null, | ||
| 'silent' : false, | ||
| 'addRemoveFieldsMarksDirty' : false, | ||
| 'fieldEvents' : 'change keyup propertychange input', | ||
| 'fieldSelector': ":input:not(input[type=submit]):not(input[type=button])" | ||
| }, options); | ||
|
|
||
| var getValue = function($field) { | ||
| if ($field.hasClass('ays-ignore') | ||
| || $field.hasClass('aysIgnore') | ||
| || $field.attr('data-ays-ignore') | ||
| || $field.attr('name') === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($field.is(':disabled')) { | ||
| return 'ays-disabled'; | ||
| } | ||
|
|
||
| var val; | ||
| var type = $field.attr('type'); | ||
| if ($field.is('select')) { | ||
| type = 'select'; | ||
| } | ||
|
|
||
| switch (type) { | ||
| case 'checkbox': | ||
| case 'radio': | ||
| val = $field.is(':checked'); | ||
| break; | ||
| case 'select': | ||
| if($field.val()) { | ||
| val = $field.val().toString(); | ||
| } else { | ||
| val = $field.prop("selectedIndex").toString(); | ||
| } | ||
| break; | ||
| default: | ||
| val = $field.val(); | ||
| } | ||
|
|
||
| return val; | ||
| }; | ||
|
|
||
| var storeOrigValue = function($field) { | ||
| $field.data('ays-orig', getValue($field)); | ||
| }; | ||
|
|
||
| var checkForm = function(evt) { | ||
|
|
||
| var isFieldDirty = function($field) { | ||
| var origValue = $field.data('ays-orig'); | ||
| if (undefined === origValue) { | ||
| return false; | ||
| } | ||
| return (getValue($field) != origValue); | ||
| }; | ||
|
|
||
| var $form = ($(this).is('form')) | ||
| ? $(this) | ||
| : $(this).parents('form'); | ||
|
|
||
| // Test on the target first as it's the most likely to be dirty | ||
| if (isFieldDirty($(evt.target))) { | ||
| setDirtyStatus($form, true); | ||
| return; | ||
| } | ||
|
|
||
| $fields = $form.find(settings.fieldSelector); | ||
|
|
||
| if (settings.addRemoveFieldsMarksDirty) { | ||
| // Check if field count has changed | ||
| var origCount = $form.data("ays-orig-field-count"); | ||
| if (origCount != $fields.length) { | ||
| setDirtyStatus($form, true); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Brute force - check each field | ||
| var isDirty = false; | ||
| $fields.each(function() { | ||
| $field = $(this); | ||
| if (isFieldDirty($field)) { | ||
| isDirty = true; | ||
| return false; // break | ||
| } | ||
| }); | ||
|
|
||
| setDirtyStatus($form, isDirty); | ||
| }; | ||
|
|
||
| var initForm = function($form) { | ||
| var fields = $form.find(settings.fieldSelector); | ||
| $(fields).each(function() { storeOrigValue($(this)); }); | ||
| $(fields).unbind(settings.fieldEvents, checkForm); | ||
| $(fields).bind(settings.fieldEvents, checkForm); | ||
| $form.data("ays-orig-field-count", $(fields).length); | ||
| setDirtyStatus($form, false); | ||
| }; | ||
|
|
||
| var setDirtyStatus = function($form, isDirty) { | ||
| var changed = isDirty != $form.hasClass(settings.dirtyClass); | ||
| $form.toggleClass(settings.dirtyClass, isDirty); | ||
|
|
||
| // Fire change event if required | ||
| if (changed) { | ||
| if (settings.change) settings.change.call($form, $form); | ||
|
|
||
| if (isDirty) $form.trigger('dirty.areYouSure', [$form]); | ||
| if (!isDirty) $form.trigger('clean.areYouSure', [$form]); | ||
| $form.trigger('change.areYouSure', [$form]); | ||
| } | ||
| }; | ||
|
|
||
| var rescan = function() { | ||
| var $form = $(this); | ||
| var fields = $form.find(settings.fieldSelector); | ||
| $(fields).each(function() { | ||
| var $field = $(this); | ||
| if (!$field.data('ays-orig')) { | ||
| storeOrigValue($field); | ||
| $field.bind(settings.fieldEvents, checkForm); | ||
| } | ||
| }); | ||
| // Check for changes while we're here | ||
| $form.trigger('checkform.areYouSure'); | ||
| }; | ||
|
|
||
| var reinitialize = function() { | ||
| initForm($(this)); | ||
| } | ||
|
|
||
| if (!settings.silent && !window.aysUnloadSet) { | ||
| window.aysUnloadSet = true; | ||
| $(window).bind('beforeunload', function() { | ||
| $dirtyForms = $("form").filter('.' + settings.dirtyClass); | ||
| if ($dirtyForms.length == 0) { | ||
| return; | ||
| } | ||
| // Prevent multiple prompts - seen on Chrome and IE | ||
| if (navigator.userAgent.toLowerCase().match(/msie|chrome/)) { | ||
| if (window.aysHasPrompted) { | ||
| return; | ||
| } | ||
| window.aysHasPrompted = true; | ||
| window.setTimeout(function() {window.aysHasPrompted = false;}, 900); | ||
| } | ||
| return settings.message; | ||
| }); | ||
| } | ||
|
|
||
| return this.each(function(elem) { | ||
| if (!$(this).is('form')) { | ||
| if (typeof console !== 'undefined' && | ||
| typeof console.warn !== 'undefined') { | ||
| console.warn("jquery.AreYouSure can only attach to FORM elements.\n"+ | ||
| "The jQuery selector used included a " + this.tagName + " element."); | ||
| } | ||
| return; | ||
| } | ||
| var $form = $(this); | ||
|
|
||
| $form.submit(function() { | ||
| $form.removeClass(settings.dirtyClass); | ||
| }); | ||
| $form.bind('reset', function() { setDirtyStatus($form, false); }); | ||
| // Add a custom events | ||
| $form.bind('rescan.areYouSure', rescan); | ||
| $form.bind('reinitialize.areYouSure', reinitialize); | ||
| $form.bind('checkform.areYouSure', checkForm); | ||
| initForm($form); | ||
| }); | ||
| }; | ||
| })(jQuery); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Element is not a form.
Check that console object and console.warn method exist.
If they exist, add warning message to console.