~divmod-dev/divmod.org/dangling-1091

« back to all changes in this revision

Viewing changes to Mantissa/xmantissa/js/Mantissa/Validate.js

  • Committer: mithrandi
  • Date: 2006-05-29 16:34:24 UTC
  • Revision ID: svn-v4:866e43f7-fbfc-0310-8f2a-ec88d1da2979:trunk:6874
Merge js-modules-651.

Fixes #651
Author: mithrandi
Reviewer: exarkun

This branch implements {{{athena.AutoJSPackage}}} for use in nevow plugins; it
automatically derives JS module mappings from the filesystem hierarchy, in a
manner similar to Python. Unlike python, __init__.js files are optional, and
will be treated like an empty file if not present. Mantissa and Quotient are
also migrated to use the new mechanism.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// import Mantissa.LiveForm
 
3
 
 
4
/*
 
5
  This just does validation for Mantissa user-info-requiring signup right now,
 
6
  but the principles could hopefully be applied to other forms of LiveForm
 
7
  validation eventually.
 
8
 */
 
9
 
 
10
/*
 
11
  XXX TODO: I really want to say "package Mantissa.Validate" or something.
 
12
 */
 
13
 
 
14
Mantissa.Validate.SignupForm = Mantissa.LiveForm.FormWidget.subclass(
 
15
    "Mantissa.Validate.SignupForm");
 
16
 
 
17
Mantissa.Validate.SignupForm.methods(
 
18
    function __init__(self, node) {
 
19
        Mantissa.Validate.SignupForm.upcall(self, '__init__', node);
 
20
        self.domain = document.location.hostname;
 
21
        var domarr = self.domain.split(".");
 
22
        if (domarr[0] === 'www') {
 
23
            domarr.unshift();
 
24
            self.domain = domarr.join('.');
 
25
        }
 
26
        self.inputCount = 0;
 
27
        var junk = self.gatherInputs();
 
28
        for (var yuck in junk) {self.inputCount++;}
 
29
        // minus one for domain, plus one for confirm...
 
30
        self.verifiedCount = 0;
 
31
        self.testedInputs = {};
 
32
 
 
33
        self.passwordInput = self.nodeByAttribute("name", "password");
 
34
 
 
35
        self.submitButton = self.nodeByAttribute("type", "submit");
 
36
    },
 
37
 
 
38
    // see LiveForm for explanation
 
39
    function runFader(self, fader) {
 
40
        return fader.fadeIn();
 
41
    },
 
42
 
 
43
    function submitSuccess(self, result) {
 
44
        var d = Mantissa.Validate.SignupForm.upcall(self, 'submitSuccess', result);
 
45
        d.addCallback(function() {
 
46
            window.location = '/login';
 
47
        });
 
48
        return d;
 
49
    },
 
50
 
 
51
    function defaultUsername(self, inputnode) {
 
52
        if (inputnode.value.length == 0) {
 
53
            inputnode.value = (self.nodeByAttribute("name", "firstName").value.toLowerCase()
 
54
                               + '.' +
 
55
                               self.nodeByAttribute("name", "lastName").value.toLowerCase());
 
56
        }
 
57
    },
 
58
    function verifyNotEmpty(self, inputnode) {
 
59
        /*
 
60
          This is bound using an athena handler to all the input nodes.
 
61
 
 
62
          We need to look for a matching feedback node for this input node.
 
63
         */
 
64
        self.verifyInput(inputnode, inputnode.value != '');
 
65
    },
 
66
    function verifyUsernameAvailable(self, inputnode) {
 
67
        var username = inputnode.value;
 
68
        self.callRemote("usernameAvailable",username, self.domain).addCallback(
 
69
            function (result) {
 
70
                self.verifyInput(inputnode, result);
 
71
            });
 
72
    },
 
73
    function verifyStrongPassword(self, inputnode) {
 
74
        self.verifyInput(
 
75
            inputnode,
 
76
            inputnode.value.length > 4);
 
77
    },
 
78
    function verifyPasswordsMatch(self, inputnode) {
 
79
        self.verifyInput(
 
80
            inputnode,
 
81
            (self.testedInputs['password']) &&
 
82
            (inputnode.value === self.passwordInput.value));
 
83
    },
 
84
    function verifyValidEmail(self, inputnode) {
 
85
        var cond = null;
 
86
        var addrtup = inputnode.value.split("@");
 
87
        // require localpart *and* domain
 
88
        if (addrtup.length == 2) {
 
89
            var addrloc = addrtup[0];
 
90
            // localpart can't be empty
 
91
            if (addrloc.length > 0) {
 
92
                // domain can't be empty
 
93
                var addrdom = addrtup[1].split('.');
 
94
                if (addrdom.length >= 1) {
 
95
                    for (var i = 0; i < addrdom.length; i++) {
 
96
                        var requiredLength;
 
97
                        if (i === (addrdom.length - 1)) {
 
98
                            // TLDs are all 2 or more chars
 
99
                            requiredLength = 2;
 
100
                        } else {
 
101
                            // other domains can be one letter
 
102
                            requiredLength = 1;
 
103
                        }
 
104
                        if (addrdom[i].length < requiredLength) {
 
105
                            // WHOOPS
 
106
                            cond = false;
 
107
                            break;
 
108
                        } else {
 
109
                            // okay so far...
 
110
                            cond = true;
 
111
                        }
 
112
                    }
 
113
                }
 
114
            } else {
 
115
                cond = false;
 
116
            }
 
117
        } else {
 
118
            cond = false;
 
119
        }
 
120
        self.verifyInput(inputnode, cond);
 
121
    },
 
122
    function verifyInput(self, inputnode, condition) {
 
123
        var statusNode = self._findStatusElement(inputnode);
 
124
        var status = '';
 
125
        var wasPreviouslyVerified = !!self.testedInputs[inputnode.name];
 
126
 
 
127
        if (condition) {
 
128
            statusNode.style.backgroundColor = 'green';
 
129
        } else {
 
130
            statusNode.style.backgroundColor = 'red';
 
131
        }
 
132
 
 
133
        if (condition != wasPreviouslyVerified) {
 
134
            self.testedInputs[inputnode.name] = condition;
 
135
            if (condition) {
 
136
                self.verifiedCount++;
 
137
            } else {
 
138
                self.verifiedCount--;
 
139
            }
 
140
            self.submitButton.disabled = !(
 
141
                self.verifiedCount === self.inputCount);
 
142
        }
 
143
    },
 
144
    function _findStatusElement(self, inputnode) {
 
145
        var fieldgroup = inputnode.parentNode;
 
146
        while (fieldgroup.getAttribute("class") != "verified-field") {
 
147
            fieldgroup = fieldgroup.parentNode;
 
148
        }
 
149
        var theNodes = fieldgroup.childNodes;
 
150
        for (var maybeStatusNodeIdx in theNodes) {
 
151
            var maybeStatusNode = theNodes[maybeStatusNodeIdx];
 
152
            if (typeof maybeStatusNode.getAttribute != 'undefined') {
 
153
                if (maybeStatusNode.getAttribute("class") == "verify-status") {
 
154
                    return maybeStatusNode;
 
155
                }
 
156
            }
 
157
        }
 
158
    },
 
159
    function gatherInputs(self) {
 
160
        inputs = Mantissa.Validate.SignupForm.upcall(
 
161
            self, 'gatherInputs');
 
162
        delete inputs['confirmPassword'];
 
163
        delete inputs['__submit__'];
 
164
        // screw you, hidden fields!
 
165
        inputs['domain'] = [self.domain];
 
166
        return inputs;
 
167
    });