annotate static/add.js @ 19:9fc1f5c8aa4e

working on the 'share' selectbox ui Ignore-this: 5e6f80568920eb3aab9e56bdfb509d19
author Drew Perttula <drewp@bigasterisk.com>
date Sun, 17 Mar 2013 00:49:27 -0700
parents e054949143e9
children 8008ec2fd763
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
1 var model = {
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
2 linkRecord: {
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
3 href: ko.observable(""),
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
4 description: ko.observable(""),
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
5 tag: ko.observable(""),
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
6 extended: ko.observable(""),
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
7 private: ko.observable(false),
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
8 shareWith: ko.observableArray([]), // foaf uris
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
9 },
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
10 submitLabel: ko.observable("Add"),
4
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
11 };
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
12
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
13 ko.computed(function() {
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
14 if (model.linkRecord.href() == "") {
4
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
15 return;
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
16 }
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
17
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
18 $.getJSON("addLink/proposedUri", {uri: model.linkRecord.href()}, function (data) {
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
19 // these could arrive after the user has started typing in the fields!
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
20
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
21 model.linkRecord.description(data.description);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
22 model.linkRecord.tag(data.tag);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
23 model.linkRecord.extended(data.extended);
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
24 model.linkRecord.shareWith(data.shareWith);
5
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
25 model.submitLabel(data.existed ? "Update existing" : "Add");
f8c4c7ce5f4a lots of href additions: add/edit, nav fixes
Drew Perttula <drewp@bigasterisk.com>
parents: 4
diff changeset
26
4
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
27 });
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
28
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
29 });
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
30
409da49c148d partway though add
drewp@bigasterisk.com
parents:
diff changeset
31 ko.applyBindings(model);
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
32
19
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
33 (function (inputElem, model) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
34 inputElem.select2({
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
35 tokenSeparators: [",", " "],
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
36 ajax: {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
37 url: "/foaf/findPerson",
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
38 data: function (term, page) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
39 return {q: term};
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
40 },
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
41 results: function (data, page) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
42 var ret = {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
43 results: data.people.map(
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
44 function (row) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
45 return {id: row.uri,
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
46 text: row.label + " ("+row.uri+")"};
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
47 }),
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
48 more: false,
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
49 context: {}
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
50 };
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
51 //ret.results.push({id: "new1", text: this.context});
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
52 return ret;
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
53 }
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
54 },
19
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
55 tags: [],
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
56 });
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
57 inputElem.on('change', function (e) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
58 console.log("onchange", inputElem.select2('val'));
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
59 setModelFromShares(inputElem.select2('val'));
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
60 });
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
61
19
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
62 var enableModel = true;
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
63
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
64 var setSharesFromModel = ko.computed(
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
65 function () {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
66 var uris = ko.utils.arrayGetDistinctValues(model.linkRecord.shareWith());
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
67 if (!enableModel) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
68 return;
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
69 }
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
70 console.log("from model", uris)
10
e054949143e9 reworking addlink and shareWith support
drewp@bigasterisk.com
parents: 5
diff changeset
71
19
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
72 inputElem.select2("data", uris.map(
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
73 function (uri) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
74 return {id: uri, text: "("+uri+")"};
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
75 }));
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
76 });
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
77
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
78 function setModelFromShares(n) {
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
79 console.log("from val", inputElem.select2("val"), "new", n)
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
80 enableModel = false;
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
81 model.linkRecord.shareWith(inputElem.select2("val"));
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
82 enableModel = true;
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
83 }
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
84
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
85 // setSharesFromModel();
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
86 })($("#shareWith"), model);
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
87
9fc1f5c8aa4e working on the 'share' selectbox ui
Drew Perttula <drewp@bigasterisk.com>
parents: 10
diff changeset
88