Project

General

Profile

Feature #2910 ยป textsave.js

Mathias Fischer, 2009-06-24 18:40

 
1
/*
2
File: textsave.js
3

    
4
Description: Initialize an onbeforeunload-Event in Redmine, if changes in a form are made, but do not ask, if we submit a form
5

    
6
Author: mathias.fischer@berlinonline.de
7

    
8
*/
9

    
10
var TextSave = Class.create({
11
    observedForms: false,
12
    observedElements: false,
13
    changedForms: false,
14

    
15
    initialize: function(){
16
        this.observedForms = $$('form');
17
        //Use #main to ignore the Search-Box
18
        this.observedElements =  $$('#main textarea','#main input','#main select');
19

    
20
        this.observedElements.each(this.observeChange.bind(this));
21
        this.observedForms.each(this.submitAction.bind(this));
22

    
23
        //Event.stop returns "false", so we cannot use Prototypes Event.observe() here...
24
        window.onbeforeunload = this.unload.bind(this);
25
    },
26

    
27
    //Stuff to do before unload, confirm-box is display if a return-value is given
28
    unload: function(){
29
        if(this.changedForms)
30
            return "There are unsaved changes.";
31
    },
32

    
33
    setChanged: function(){
34
        this.changedForms = true;
35
    },
36

    
37
    setUnchanged: function(){
38
        this.changedForms = false;
39
    },
40

    
41
    observeChange: function(element){
42
        element.observe('change',this.setChanged.bindAsEventListener(this));
43
    },
44

    
45
    submitAction: function(element){
46
        element.observe('submit',this.setUnchanged.bindAsEventListener(this));
47
    }
48

    
49
});
50

    
51

    
52
document.observe("dom:loaded", function() {
53
    new TextSave();
54
});
    (1-1/1)