Created
April 21, 2014 03:10
-
-
Save ccorcos/11131249 to your computer and use it in GitHub Desktop.
ok cancel event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Session.setDefault("parents", []) | |
Session.setDefault("children", []) | |
Template.main.rendered = function() { | |
// autoupdate the parents and children | |
Deps.autorun(function() { | |
nodeId = Session.get("editNode"); | |
if (nodeId) { | |
parents = parentsOf(nodeId) | |
children = childrenOf(nodeId) | |
Session.set("parents", parents) | |
Session.set("children", children) | |
} | |
}) | |
} | |
Template.editNode.rendered = function() { | |
// autocomplete | |
addparent = completely($(".add.parent.field")[0]) | |
addchild = completely($(".add.child.field")[0]) | |
// addparent.input.placeholder = "add parent" | |
// addchild.input.placeholder = "add child" | |
// console.log(addparent) | |
Deps.autorun(function() { | |
addparent.options = _.pluck(parentOptions(Session.get("editNode")), "name") | |
addparent.repaint() | |
addparent.hideDropDown(); | |
}) | |
Deps.autorun(function() { | |
addchild.options = _.pluck(childOptions(Session.get("editNode")), "name") | |
addchild.repaint() | |
addchild.hideDropDown(); | |
}) | |
} | |
// add parent | |
Template.editNode.events(okCancelEvents( | |
'.add.parent', { | |
ok: function(text, evt) { | |
console.log("ok") | |
parentId = Nodes.findOne({ | |
name: text | |
})._id | |
if (parentId) { | |
console.log("saving new parent link") | |
nodeId = Session.get("editNode") | |
Links.insert({ | |
from: parentId, | |
to: nodeId | |
}) | |
evt.target.value = "" | |
} else { | |
console.log("WARNING: invalid parent") | |
} | |
}, | |
cancel: function(evt) { | |
console.log("cancel") | |
evt.target.value = "" | |
} | |
})); | |
// add children | |
Template.editNode.events(okCancelEvents( | |
'.add.child', { | |
ok: function(text, evt) { | |
console.log("ok") | |
childId = Nodes.findOne({ | |
name: text | |
})._id | |
if (childId) { | |
console.log("saving new child link") | |
nodeId = Session.get("editNode") | |
Links.insert({ | |
from: nodeId, | |
to: childId | |
}) | |
evt.target.value = "" | |
} else { | |
console.log("WARNING: invalid child") | |
} | |
}, | |
cancel: function(evt) { | |
console.log("cancel") | |
evt.target.value = "" | |
} | |
})); | |
nodeEdited = function() { | |
nodeId = Session.get("editNode"); | |
if (nodeId) { | |
node = Nodes.findOne(nodeId) | |
if (node) { | |
return node | |
} else { | |
console.log("WARNING: editing unknown node") | |
return {} | |
} | |
} else { | |
return {} | |
} | |
} | |
Template.editNode.events({ | |
// save node name | |
'focusout .edit.node.name': function(e, t) { | |
node = nodeEdited(); | |
editedName = e.target.value | |
if (editedName == node.name) { | |
console.log("name unchanged") | |
} else { | |
if (editedName.length > 1) { | |
console.log("saving node name") | |
Nodes.update(node._id, { | |
$set: { | |
name: editedName | |
} | |
}) | |
} else { | |
console.log("invalid name") | |
e.target.value = node.name | |
} | |
} | |
}, | |
// save node decription | |
'focusout .edit.node.description': function(e, t) { | |
node = nodeEdited(); | |
editedDescription = e.target.value | |
if (editedDescription == node.description) { | |
console.log("description unchanged") | |
} else { | |
console.log("saving node description") | |
Nodes.update(node._id, { | |
$set: { | |
description: editedDescription | |
} | |
}) | |
} | |
}, | |
'click .node.name': function(e, t) { | |
editNode(this._id) | |
}, | |
'click .remove.parent': function(e, t) { | |
console.log("remove parent") | |
nodeId = Session.get("editNode"); | |
linkId = Links.findOne({ | |
to: nodeId, | |
from: this._id | |
})._id | |
Links.remove(linkId) | |
}, | |
'click .remove.child': function(e, t) { | |
console.log("remove child") | |
nodeId = Session.get("editNode"); | |
linkId = Links.dinsOne({ | |
to: this._id, | |
from: nodeId | |
})._id | |
Links.remove(linkId) | |
} | |
}) | |
// save node, delete node, delete link | |
Template.editNode.events({ | |
'click .delete.button': function(e, t) { | |
console.log("delete node") | |
nodeId = Session.get("editNode"); | |
Nodes.remove(nodeId); | |
Session.set("editNode", false) | |
} | |
}) | |
errorMsg = function($elem, message) { | |
if (message.length > 0) { | |
$elem.after('<div class="ui red pointing above ui label error msg">' + message + '</div>') | |
} | |
$elem.parent().addClass("error") | |
} | |
clearErrors = function() { | |
$('.error.msg').remove() | |
$('.error').removeClass("error") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment