diff static/add.js @ 10:e054949143e9

reworking addlink and shareWith support Ignore-this: b1665b776f3964f7fde219acadc51f32
author drewp@bigasterisk.com
date Fri, 15 Mar 2013 00:29:53 -0700
parents f8c4c7ce5f4a
children 9fc1f5c8aa4e
line wrap: on
line diff
--- a/static/add.js	Wed Mar 06 23:23:18 2013 -0800
+++ b/static/add.js	Fri Mar 15 00:29:53 2013 -0700
@@ -1,23 +1,27 @@
 var model = {
-    href: ko.observable(""),
-    description: ko.observable(""),
-    tag: ko.observable(""),
-    extended: ko.observable(""),
+    linkRecord: {
+        href: ko.observable(""),
+        description: ko.observable(""),
+        tag: ko.observable(""),
+        extended: ko.observable(""),
+        private: ko.observable(false),
+        shareWith: ko.observableArray([]), // foaf uris
+    },
     submitLabel: ko.observable("Add"),
 };
 
 ko.computed(function() {
-    if (model.href() == "") {
+    if (model.linkRecord.href() == "") {
         return;
     }
 
-    $.getJSON("addLink/proposedUri", {uri: model.href()}, function (data) {
+    $.getJSON("addLink/proposedUri", {uri: model.linkRecord.href()}, function (data) {
         // these could arrive after the user has started typing in the fields!
         
-        model.description(data.description);
-        model.tag(data.tag);
-        model.extended(data.extended);
-
+        model.linkRecord.description(data.description);
+        model.linkRecord.tag(data.tag);
+        model.linkRecord.extended(data.extended);
+        model.linkRecord.shareWith(data.shareWith);
         model.submitLabel(data.existed ? "Update existing" : "Add");
         
     });
@@ -25,3 +29,43 @@
 });
 
 ko.applyBindings(model);
+
+$("#shareWith").select2({
+    tokenSeparators: [",", " "],
+    ajax: {
+        url: "/foaf/findPerson",
+        data: function (term, page) {
+            return {q: term};
+        },
+        results: function (data, page) {
+            var ret = {results: data.people.map(
+                function (row) {
+                    return {id: row.uri, text: row.label + " ("+row.uri+")"}
+                }),
+                       more: false,
+                       context: {}
+                      };
+            //ret.results.push({id: "new1", text: this.context});
+            return ret;
+        }
+    },
+    tags: [],
+});
+$("#shareWith").on('change', function (e) { setModelFromShares(e.val); });
+
+var setSharesFromModel = ko.computed(
+    function () {
+        var uris = ko.utils.arrayGetDistinctValues(model.linkRecord.shareWith());
+        console.log("from model", uris)
+        $("#shareWith").select2("data", uris.map(
+            function (uri) {
+                return {id: uri, text: "("+uri+")"};
+            }));
+    });
+
+function setModelFromShares(n) {
+    console.log("from val", $("#shareWith").select2("val"), "new", n)
+    model.linkRecord.shareWith($("#shareWith").select2("val"));
+}
+
+setSharesFromModel();