annotationinput.js 3.23 KB
Newer Older
1
(function () {
2
    var debug = false;
3 4 5 6 7

    var module = {
        debug: debug,
        inputSelector: '.annotation-input',
        tagSelector: '.tag',
8
        tagsSelector: '.tags',
9 10 11
        commentSelector: 'textarea.comment',
        valueSelector: 'input.value', // stash tag selections and comment here as a JSON string...

12 13
        singleSelect: true,

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
        init: function() {
            var that = this;

            if(this.debug) { console.log('annotation input loaded: '); }

            $(this.inputSelector).each(function(index, el) {
                if(!$(el).data('listening')) {
                    $(el).delegate(that.tagSelector, 'click', $.proxy(that.onClickTag, that));
                    $(el).delegate(that.commentSelector, 'change', $.proxy(that.onChangeComment, that));
                    $(el).data('listening', 'yes');
                }
            });
        },
        onChangeComment: function(e) {
            var value_el = this.findValueEl(e.target);
29
            var current_value = this.loadValue(value_el);
30 31 32
            var target_value = $(e.target).val();

            current_value.comment = target_value;
33
            this.storeValue(value_el, current_value);
34 35 36 37 38 39
        },
        onClickTag: function(e) {
            var target_el = e.target, target_value, target_index;
            var value_el, current_value;

            value_el = this.findValueEl(e.target);
40
            current_value = this.loadValue(value_el);
41 42 43
            target_value = $(e.target).data('id');

            if(!$(target_el).hasClass('selected')) {
44 45 46 47 48
                if(this.singleSelect) {
                    current_value.options = [target_value]
                } else {
                    current_value.options.push(target_value);
                }
49
            } else {
50 51 52 53 54 55 56
                if(this.singleSelect) {
                    current_value.options = []
                } else {
                    target_index = current_value.options.indexOf(target_value);
                    if(target_index !== -1) {
                        current_value.options.splice(target_index, 1);
                    }
57 58 59
                }
            }

60
            this.storeValue(value_el, current_value);
61 62 63 64 65 66 67

            if(this.singleSelect) {
                $(target_el).closest(this.tagsSelector)
                    .find(this.tagSelector)
                    .not(target_el)
                    .removeClass('selected')
            } 
68 69 70 71 72 73
            $(target_el).toggleClass('selected');
        },
        findValueEl: function(target_el) {
            var input_el = $(target_el).closest(this.inputSelector);
            return $(this.valueSelector, input_el);
        },
74
        loadValue: function(value_el) {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
            var json = $(value_el).val();

            var result = JSON.parse(json);
            if(result === null) {
                result = {};
            }
            if(!result.hasOwnProperty('options')) {
                result.options = [];
            }
            if(!result.hasOwnProperty('comment')) {
                result.comment = '';
            }

            return result;
        },
90
        storeValue: function(value_el, new_value) {
91 92 93 94 95 96
            var json = JSON.stringify(new_value);
            $(value_el).val(json);
        }
    }

    module.init();
97
}).call(this);