summaryrefslogtreecommitdiffstats
path: root/funcweb/funcweb/static/javascript/utils.js
blob: 60295029a9019a627904be9b1273ee662656bf57 (plain)
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
function check_all(class_name){
    var check_boxes = getElementsByTagAndClassName('input',class_name);
    for (var check_element in check_boxes){
        if (compare(check_boxes[check_element].checked,false)==0){
            check_boxes[check_element].checked = true;
        }
    }
}

function uncheck_all(class_name){
    var check_boxes = getElementsByTagAndClassName('input',class_name);
    for (var check_element in check_boxes){
        if (compare(check_boxes[check_element].checked,true)==0)
            check_boxes[check_element].checked = false;
    }
   
}

function checkController(class_name,check_element){
    if (check_element.checked == true){
        check_all(class_name);
    }
    else
        uncheck_all(class_name);
}

/*
 * Some ajaxian methods here (minion related stuff mostly !)
 * These methods will replace the jquery and will let the Mochikit
 * do all the stuff here !
 */

function list_minion_modules(minion_name){
    /*
     * Method that hanles all the stuff on index.html
     * Therefore when you change something on index.html you should change
     * that method also if it affects the related div ids below 
     */

    //firstly do some hidings here 
    hideElement(getElement('resultcontent'));
    hideElement(getElement('widgetcontent'));
    hideElement(getElement('methotdscontent'));
    hideElement(getElement('modulescontent'));
    var base_url = '/funcweb/minion';
    var data_pack = {'name':minion_name};
    var div_to_replace = 'modulescontent';
    //send the JSON request !
    send_some_JSON(base_url,data_pack,div_to_replace);
}

function list_module_methods(minion_name,module_name){
    
    //listing methods for specified module works for modules.html

    hideElement(getElement('widgetcontent'));
    hideElement(getElement('resultcontent'));
    hideElement(getElement('methotdscontent'));
    var base_url = '/funcweb/minion';
    var data_pack = {
        'name':minion_name,
        'module':module_name
    };
    var div_to_replace = 'methotdscontent';
    send_some_JSON(base_url,data_pack,div_to_replace);

}

function get_method_widget(minion_name,module_name,method_name){

    // The method widget generator part works for methods.html 

    hideElement(getElement('resultcontent'));
    hideElement(getElement('widgetcontent'));
    var base_url = '/funcweb/method_display';
    var data_pack = {
        'minion':minion_name,
        'module':module_name,
        'method':method_name
    };
    var div_to_replace = 'widgetcontent';
    send_some_JSON(base_url,data_pack,div_to_replace);
}

function send_some_JSON(base_url,data_pack,div_to_replace){
    /*
     * A common method that will responsible for sending 
     * simple Http request to server !
     */
    var d = doSimpleXMLHttpRequest(base_url, data_pack);
    //var d = loadJSONDoc(base_url+queryString(data_pack));
    d.addCallback(replace_div_success,div_to_replace);
    //The errback will be a common method here !
    d.addErrback(connection_error);

}

function replace_div_success(div_to_replace,result){
    /*
     * The common callback for ajax requests
     */

    var server_text = result.responseText;
    var is_error = true;
    var check_error = null;
    
    //Because we got a response text it may not be a json object we should control it
    //we report the errors with tubogears tg_flash variable so the control below
    //simply tries to convert the response into JSON doc and pull the tg_flash variable's
    //value. If it is null it seems tobe a normal response !
    try{
        check_error = evalJSON(server_text);
    }
    catch(e){
        //There is no error in request
        is_error = false;
    }

    if (is_error == true){
        if (compare(check_error['fg_flash'],null)!=0)
            connection_error(check_error['tg_flash']);
        else{
            is_error = false;
            alert("I marked it as non error");
        }
    }

    if (is_error == false){
        //Put the result into the target div
        var replace_div = getElement(div_to_replace);
        if (replace_div != null){
            //first make it to appear here
            showElement(replace_div);
            replace_div.innerHTML = server_text;
        }
    }
}

function connection_error(error){
    // The common errback method
    var error_msg = "Async Request Error : You may try checking your connection and refresh your page! :" + repr(error);
    alert("We got error in Async Request check the more detailed error report at the TOP of the page !");    
    var error_div = getElement("globalerror");
            if (error_div != null){
                error_div.innerHTML = error_msg;
            }
}