-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathinput.monitor.js
160 lines (120 loc) · 4.82 KB
/
input.monitor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* The form change monitor API.
*
* - Documentation: ../docs/input-monitor.md
*/
+function ($) { "use strict";
var Base = $.wn.foundation.base,
BaseProto = Base.prototype
var ChangeMonitor = function (element, options) {
this.$el = $(element);
this.paused = false
this.options = options || {}
$.wn.foundation.controlUtils.markDisposable(element)
Base.call(this)
this.init()
}
ChangeMonitor.prototype = Object.create(BaseProto)
ChangeMonitor.prototype.constructor = ChangeMonitor
ChangeMonitor.prototype.init = function() {
this.$el.on('change', this.proxy(this.change))
this.$el.on('unchange.oc.changeMonitor', this.proxy(this.unchange))
this.$el.on('pause.oc.changeMonitor', this.proxy(this.pause))
this.$el.on('resume.oc.changeMonitor', this.proxy(this.resume))
this.$el.on('keyup input paste', 'input:not(.ace_search_field), textarea:not(.ace_text-input)', this.proxy(this.onInputChange))
$('input:not([type=hidden]):not(.ace_search_field), textarea:not(.ace_text-input)', this.$el).each(function() {
$(this).data('oldval.oc.changeMonitor', $(this).val());
})
if (this.options.windowCloseConfirm)
$(window).on('beforeunload', this.proxy(this.onBeforeUnload))
this.$el.one('dispose-control', this.proxy(this.dispose))
this.$el.trigger('ready.oc.changeMonitor')
}
ChangeMonitor.prototype.dispose = function() {
if (this.$el === null)
return
this.unregisterHandlers()
this.$el.removeData('oc.changeMonitor')
this.$el = null
this.options = null
BaseProto.dispose.call(this)
}
ChangeMonitor.prototype.unregisterHandlers = function() {
this.$el.off('change', this.proxy(this.change))
this.$el.off('unchange.oc.changeMonitor', this.proxy(this.unchange))
this.$el.off('pause.oc.changeMonitor ', this.proxy(this.pause))
this.$el.off('resume.oc.changeMonitor ', this.proxy(this.resume))
this.$el.off('keyup input paste', 'input:not(.ace_search_field), textarea:not(.ace_text-input)', this.proxy(this.onInputChange))
this.$el.off('dispose-control', this.proxy(this.dispose))
if (this.options.windowCloseConfirm)
$(window).off('beforeunload', this.proxy(this.onBeforeUnload))
}
ChangeMonitor.prototype.change = function(ev, inputChange) {
if (this.paused)
return
if (ev.target.className === 'ace_search_field')
return
if (!inputChange) {
var type = $(ev.target).attr('type')
if (type === 'text' || type === 'password')
return
}
if (!this.$el.hasClass('oc-data-changed')) {
this.$el.trigger('changed.oc.changeMonitor')
this.$el.addClass('oc-data-changed')
}
}
ChangeMonitor.prototype.unchange = function() {
if (this.paused)
return
if (this.$el.hasClass('oc-data-changed')) {
this.$el.trigger('unchanged.oc.changeMonitor')
this.$el.removeClass('oc-data-changed')
}
}
ChangeMonitor.prototype.onInputChange = function(ev) {
if (this.paused)
return
var $el = $(ev.target)
if ($el.data('oldval.oc.changeMonitor') !== $el.val()) {
$el.data('oldval.oc.changeMonitor', $el.val());
this.change(ev, true);
}
}
ChangeMonitor.prototype.pause = function() {
this.paused = true
}
ChangeMonitor.prototype.resume = function() {
this.paused = false
}
ChangeMonitor.prototype.onBeforeUnload = function() {
if ($.contains(document.documentElement, this.$el.get(0)) && this.$el.hasClass('oc-data-changed'))
return this.options.windowCloseConfirm
}
ChangeMonitor.DEFAULTS = {
windowCloseConfirm: false
}
// CHANGEMONITOR PLUGIN DEFINITION
// ===============================
var old = $.fn.changeMonitor
$.fn.changeMonitor = function (option) {
return this.each(function () {
var $this = $(this)
var data = $this.data('oc.changeMonitor')
var options = $.extend({}, ChangeMonitor.DEFAULTS, $this.data(), typeof option === 'object' && option)
if (!data) $this.data('oc.changeMonitor', (data = new ChangeMonitor(this, options)))
})
}
$.fn.changeMonitor.Constructor = ChangeMonitor
// CHANGEMONITOR NO CONFLICT
// ===============================
$.fn.changeMonitor.noConflict = function () {
$.fn.changeMonitor = old
return this
}
// CHANGEMONITOR DATA-API
// ===============================
$(document).render(function(){
$('[data-change-monitor]').changeMonitor()
})
}(window.jQuery);