cohorts.js 8.75 KB
Newer Older
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
// structure stolen from http://briancray.com/posts/javascript-module-pattern

var CohortManager = (function ($) {
    // private variables and functions

    // using jQuery
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = $.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    var csrftoken = getCookie('csrftoken');
    
    function csrfSafeMethod(method) {
        // these HTTP methods do not require CSRF protection
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }  
    $.ajaxSetup({
        crossDomain: false, // obviates need for sameOrigin test
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type)) {
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }
        }
    });

    // constructor
    var module = function () {
39 40 41 42 43 44 45 46 47 48 49
        var el = $(".cohort_manager");
        // localized jquery
        var $$ = function (selector) {
            return $(selector, el)
        }
        var state_init = "init";
        var state_summary = "summary";
        var state_detail = "detail";
        var state = state_init;
        
        var url = el.data('ajax_url');
50
        var self = this;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

        // Pull out the relevant parts of the html
        // global stuff
        var errors = $$(".errors");

        // cohort summary display
        var summary = $$(".summary");
        var cohorts = $$(".cohorts");
        var show_cohorts_button = $$(".controls .show_cohorts");
        var add_cohort_input = $$(".cohort_name");
        var add_cohort_button = $$(".add_cohort");
        
        // single cohort user display
        var detail = $$(".detail");
        var detail_header = $(".header", detail);
        var detail_users = $$(".users");
        var detail_page_num = $$(".page_num");
        var users_area = $$(".users_area");
        var add_members_button = $$(".add_members");
70 71
        var op_results = $$(".op_results");
        var cohort_id = null;
72 73 74 75 76
        var cohort_title = null;
        var detail_url = null;
        var page = null;

        // *********** Summary view methods
77 78 79 80

        function show_cohort(item) {
            // item is a li that has a data-href link to the cohort base url
            var el = $(this);
81 82
            cohort_title = el.text();
            detail_url = el.data('href');
83
            cohort_id = el.data('id');
84 85
            state = state_detail;
            render();
86
            return false;
87 88 89
        }

        function add_to_cohorts_list(item) {
90
            var li = $('<li><a href="#"></a></li>');
91 92 93 94
            $("a", li).text(item.name)
                .data('href', url + '/' + item.id)
                .addClass('link')
                .click(show_cohort);
95
            cohorts.append(li);
96 97 98
        };

        function log_error(msg) {
99 100
            errors.empty();
            errors.append($("<li />").text(msg).addClass("error"));
101 102 103
        };
        
        function load_cohorts(response) {
104
            cohorts.empty();
105 106 107 108 109
            if (response && response.success) { 
                response.cohorts.forEach(add_to_cohorts_list);
            } else {
                log_error(response.msg || "There was an error loading cohorts");
            }
110
            summary.show();
111 112
        };

113

114 115 116 117 118 119 120 121
        function added_cohort(response) {
            if (response && response.success) {
                add_to_cohorts_list(response.cohort);
            } else {
                log_error(response.msg || "There was an error adding a cohort");
            }                
        }

122 123
        // *********** Detail view methods

124 125 126 127 128 129 130 131 132 133
        function remove_user_from_cohort(username, cohort_id, row) {
            var delete_url = detail_url + '/delete';
            var data = {'username': username}
            $.post(delete_url, data).done(function() {row.remove()})
                .fail(function(jqXHR, status, error) {
                    log_error('Error removing user ' + username + 
                              ' from cohort. ' + status + ' ' + error);
                });
        }

134 135
        function add_to_users_list(item) {
            var tr = $('<tr><td class="name"></td><td class="username"></td>' + 
136 137 138 139
                       '<td class="email"></td>' + 
                       '<td class="remove"></td></tr>');
            var current_cohort_id = cohort_id;

140 141 142
            $(".name", tr).text(item.name);
            $(".username", tr).text(item.username);
            $(".email", tr).text(item.email);
143 144 145 146 147 148

            $(".remove", tr).html('<a href="#">remove</a>')
                .click(function() { 
                    remove_user_from_cohort(item.username, current_cohort_id, tr);
                });
            
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            detail_users.append(tr);
        };

                
        function show_users(response) {
            detail_users.html("<tr><th>Name</th><th>Username</th><th>Email</th></tr>");
            if (response && response.success) { 
                response.users.forEach(add_to_users_list);
                detail_page_num.text("Page " + response.page + " of " + response.num_pages);
            } else {
                log_error(response.msg || 
                          "There was an error loading users for " + cohort.title);
            }
            detail.show();
        }
            

        function added_users(response) {
            function adder(note, color) {
                return function(item) {
                    var li = $('<li></li>')
170
                    if (typeof item === "object" && item.username) {
171
                        li.text(note + ' ' + item.name + ', ' + item.username + ', ' + item.email);
172 173
                    } else if (typeof item === "object" && item.msg) {
                        li.text(note + ' ' + item.username_or_email + ', ' + item.msg);
174 175 176 177
                    } else {
                        // string
                        li.text(note + ' ' + item);
                    }
178 179 180 181
                    li.css('color', color);
                    op_results.append(li);
                }
            }
182
            op_results.empty();
183 184 185
            if (response && response.success) {
                response.added.forEach(adder("Added", "green"));
                response.present.forEach(adder("Already present:", "black"));
186
                response.conflict.forEach(adder("In another cohort:", "purple"));
187
                response.unknown.forEach(adder("Unknown user:", "red"));
188
                users_area.val('')
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
            } else {
                log_error(response.msg || "There was an error adding users");
            }                
        }

        // ******* Rendering

            
        function render() {
            // Load and render the right thing based on the state
             
            // start with both divs hidden
            summary.hide();
            detail.hide();
            // and clear out the errors
            errors.empty();
            if (state == state_summary) {
                $.ajax(url).done(load_cohorts).fail(function() {
                    log_error("Error trying to load cohorts");
                });
            } else if (state == state_detail) {
                detail_header.text("Members of " + cohort_title);
                $.ajax(detail_url).done(show_users).fail(function() {
                    log_error("Error trying to load users in cohort");
                });
            }
        }

217
        show_cohorts_button.click(function() { 
218 219
            state = state_summary;
            render();
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        });
        
        add_cohort_input.change(function() {
            if (!$(this).val()) {
                add_cohort_button.removeClass('button').addClass('button-disabled');
            } else {
                add_cohort_button.removeClass('button-disabled').addClass('button');
            }
        });

        add_cohort_button.click(function() { 
            var add_url = url + '/add';
            data = {'name': add_cohort_input.val()}
            $.post(add_url, data).done(added_cohort);
        });

236 237 238 239 240 241 242
        add_members_button.click(function() { 
            var add_url = detail_url + '/add';
            data = {'users': users_area.val()}
            $.post(add_url, data).done(added_users);
        });


243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    };

    // prototype
    module.prototype = {
        constructor: module,
    };

    // return module
    return module;
})(jQuery);

$(window).load(function () {
    var my_module = new CohortManager();
})