view static/links.js @ 20:a8887fb93676

hide share data from the public. bug in links filter box Ignore-this: 2a390b207b8e9c8d430acd268b6d765d
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 17 Mar 2013 01:03:43 -0700
parents e054949143e9
children 8008ec2fd763
line wrap: on
line source


$("#filterTag").focus();

var model = {
    filterTags: ko.observableArray(currentFilter())
};

function currentFilter() {
    var p = window.location.pathname;
    var comps = p.split("/");
    if (toRoot == ".") {
        return [];
    } else {
        return (comps[comps.length-1] || "").split("+");
    }
}

function toggleTag(tag) {
    var selected = currentFilter();

    if (selected.indexOf(tag) == -1) {
        selected.push(tag);
    } else {
        selected.splice(selected.indexOf(tag), 1);
    }
    setPageTags(selected);
}

function setPageTags(tags) {
    
    var newPath = window.location.pathname;
    if (toRoot == ".") {
        newPath += "/";
    } else {
        newPath = newPath.replace(
                /(.*\/)[^\/]*$/, "$1")
    }
    console.log("user root", newPath);
    if (tags.length) {
        newPath += tags.join("+")
    } else {
        newPath = newPath.substr(0, newPath.length - 1);
    }
    console.log("done", newPath);
    
    window.location.pathname = newPath;
}

function backspaceLastTag() {
    var p = window.location.pathname;
    var comps = p.split("/");
    var selected = (comps[comps.length-1] || "").split("+");
    if (selected.length == 0) {
        return;
    }
    toggleTag(selected[selected.length-1]);
}

$("a.tag").click(function () {
    var tag = $(this).text();
    toggleTag(tag);
    return false;
});

$("#filterTag").change(function () {
    var tags = $(this).val().split(",");
    setPageTags(tags);
    return false;
});

var filterCompleteWords = "";
$("#filterTag").select2({
    allowClear: true,
    multiple: true,
    tokenSeparators: [' ', ','],
    query: function (opts) {
        $.ajax({
            url: toRoot + "/tags",
            data: {user: user, have: opts.element.val()},
            success: function (data) {
                opts.callback({results: data.tags});
            }
        });
    },
    change: function (ev) {
        console.log("ch", ev.val);
    },
    initSelection: function (element, callback) {
        var data = [];
        $(element.val().split(",")).each(function () {
            if (this != "") {
                data.push({id: this, text: this});
            }
        });
        callback(data);
    }
});
$("#filterTag").select2("val", model.filterTags());

ko.applyBindings(model);