summaryrefslogtreecommitdiff
path: root/static/js/src/profile.js
blob: 1fa66100af3e46c6e0ed104b0a73cd662917ab95 (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
function initProfile(recips) {
    Search.initInpage();
    $(".linkify-text").each(function() {
        var text = jQuery(this).text();
        jQuery(this).html(linkifyWithoutImage(text));
    });

    $(".linkify-full").each(function() {
        $(this).html(buildMsgContent($(this).text(), recips));
    });   

    $('#edit-toggle').click(enableProfileEdit);
    activateProfileEditable();

    $('.dash-dump .content').each(function() {
        var t = $(this);
        t.html(buildMsgContent(t.text()));
    });
};


function enableProfileEdit() {
    $('img#contact').replaceWith('<div id="contact" class="linkify"></div>');
    $('img#bio').replaceWith('<div id="bio" class="linkify"></div>');
    $('#contact, #bio, #avatar').addClass('editable');
    $('#avatar-editing').show();
    var resetPage = function() { location.reload() };
    $('#edit-toggle a').text('done editing').click(resetPage);
    activateProfileEditable();
}


function activateProfileEditable() {
    var onSubmit = function(attr, newVal, oldVal) {
        newVal = $.trim(newVal);
        if (newVal == oldVal) { return oldVal };
        
        $.ajax({
            type: "POST",
            timeout: 5000,
            url: "/update-profile",
            data: { 'attr': attr, 'val': newVal }
        });
        if (attr == 'avatar') {
            if (newVal != "") {
                var s = '<img id="avatarPic" src="' + newVal + '" width="150" />';
                $('#avatarPic').replaceWith(s).show();
            } else {
                $('#avatarPic').hide();
            }
        }
        return escapeHtml(newVal);
    };

    if ($('#avatar-editing').length > 0)
      setupUploadAvatar('uploadp');

    var textareaOpts = { 'default_text': 'Enter here!',
                         'callback': onSubmit,
                         'field_type': 'textarea',
                         'callbackShowErrors': false };
    $('#contact.editable, #bio.editable')
        .editInPlace(textareaOpts)
        .each(makePlainText);
}


function setupUploadAvatar(elementId) {
    // NOTE: AjaxUpload responses aren't converted from JSON.
    var onSubmit = function(file, error) {
        $('#spinner').show();
    };
    var onComplete = function(file, resp) {
        $('#spinner').hide();
	var r = $.trim(resp);

        if (r == 'INVALID_REQUEST') {
            location.reload();
        } else if (r == 'NOT_LOGGED_IN') {
            location.reload();
        } else if (r == 'INVALID_IMAGE') {
            alert("Sorry, dump.fm can't deal with your image. Pick another :(");
            return;
        } else if (r.match(/FILE_TOO_BIG/)) {
	    var maxSize = r.split(" ")[1] / 1024;
	    alert("Sorry. Your avatar is just too fucking big. "
		  + maxSize + "KB or less please.");
	    return;
	} else if (r.match(/INVALID_RESOLUTION/)) {
            var maxWidth = r.split(" ")[1];
            var maxHeight = r.split(" ")[2];
            alert("Sorry, the maximum avatar resolution is "
                  + maxWidth + "x" + maxHeight);
            return;
        }
        var s = '<img id="dashavatarPic" src="' + r + '" />';
        $('#dashavatar').html(s).show();
        $('#dashtotal').css('background-image', 'url(' + r + ')');
    };
    new AjaxUpload(elementId, {
        action: '/upload/avatar',
        autoSubmit: true,
        name: 'image',
        onSubmit: onSubmit,
        onComplete: onComplete
    });
}