record edit state in hash, add children

This commit is contained in:
Anton Dyudin 2015-12-09 16:22:21 -08:00
parent 473e0e08cf
commit ee6deae576
4 changed files with 117 additions and 23 deletions

View File

@ -20,4 +20,4 @@ module.exports =
saveFile: (spur, mime, cb) -> saveFile: (spur, mime, cb) ->
TreePersistence.put spur, mime, (err,res)-> TreePersistence.put spur, mime, (err,res)->
if err? then throw err if err? then throw err
cb res if cb? then cb res

View File

@ -144,7 +144,8 @@ module.exports = query {
if @state.url isnt window.location.pathname if @state.url isnt window.location.pathname
@reset() @reset()
@setPath (window.tree.fragpath window.location.pathname),false @setPath (window.tree.fragpath window.location.pathname),false
@setState url: window.location.pathname if @isMounted() # XX interval should have been cleared
@setState url: window.location.pathname
render: -> render: ->
if @props.meta.anchor is 'none' if @props.meta.anchor is 'none'

View File

@ -8,7 +8,7 @@ TreeActions = require '../actions/TreeActions.coffee'
recl = React.createClass recl = React.createClass
rele = React.createElement rele = React.createElement
{div,pre,p,img,a,button} = React.DOM {input,div,pre,p,img,a,button} = React.DOM
extras = extras =
spam: recl spam: recl
@ -66,7 +66,28 @@ Edit = query {mime:'m'}, recl
displayName: "Edit" displayName: "Edit"
render: -> render: ->
{mite,octs} = @props.mime {mite,octs} = @props.mime
rele codemirror, {value:octs, readOnly:false, mode:mite} rele codemirror, {value:octs, mode:mite, readOnly:false, autofocus:true}
Add = recl
displayName: "Add"
getInitialState: -> edit:false
onClick: -> @setState edit:true
componentDidUpdate: ->
if @state.edit
$(@getDOMNode()).focus()
render: ->
unless @state.edit
button {@onClick}, "Add"
else
input {type:"text",onKeyDown:(e)=>
if 13 is e.keyCode
neu = @getDOMNode().value
newPath = @props.path+"/"+neu
history.pushState {}, "", window.tree.basepath newPath + "#edit"
TreeActions.saveFile "/"+neu+@props.spur, '', ->
# TreeActions.setCurr newPath
@setState edit:false
}
module.exports = query { module.exports = query {
body:'r' body:'r'
@ -77,7 +98,16 @@ module.exports = query {
spur:'t' spur:'t'
}, recl }, recl
displayName: "Body" displayName: "Body"
getInitialState: -> edit:false getInitialState: -> edit: document.location.hash is "#edit"
setEdit: ->
@hash = document.location.hash
document.location.hash = "#edit" # XX generic state<->hash binding
@setState edit:true
unsetEdit: ->
document.location.hash = @hash ? ""
@setState edit:false
document.location.reload() # XX sigh
render: -> render: ->
className = clas (@props.meta.layout?.split ',') className = clas (@props.meta.layout?.split ',')
own = urb.user and urb.user is urb.ship own = urb.user and urb.user is urb.ship
@ -86,14 +116,14 @@ module.exports = query {
unless @state.edit unless @state.edit
body = reactify @props.body body = reactify @props.body
editButton = button {onClick: => @setState edit:true}, "Edit" editButton = button {onClick: => @setEdit()}, "Edit"
else else
body = rele Edit, {} body = rele Edit, {}
onClick = => onClick = =>
txt = $(@getDOMNode()).find('.CodeMirror')[0].CodeMirror.getValue() # XX refs txt = $(@getDOMNode()).find('.CodeMirror')[0].CodeMirror.getValue() # XX refs
TreeActions.saveFile @props.spur, txt, => @setState edit:false TreeActions.saveFile @props.spur, txt, => @unsetEdit()
editButton = button {onClick}, "Done" editButton = button {onClick}, "Done"
(div { (div {
@ -106,6 +136,6 @@ module.exports = query {
if own then editButton if own then editButton
body body
extra 'next', {dataPath:@props.sein,curr:@props.name} extra 'next', {dataPath:@props.sein,curr:@props.name}
if own then button {}, "Add" if own then rele Add,{spur:@props.spur, path:@props.path}
extra 'footer' extra 'footer'
) )

View File

@ -40,7 +40,9 @@ module.exports = {
if (err != null) { if (err != null) {
throw err; throw err;
} }
return cb(res); if (cb != null) {
return cb(res);
}
}); });
} }
}; };
@ -295,9 +297,11 @@ module.exports = query({
if (this.state.url !== window.location.pathname) { if (this.state.url !== window.location.pathname) {
this.reset(); this.reset();
this.setPath(window.tree.fragpath(window.location.pathname), false); this.setPath(window.tree.fragpath(window.location.pathname), false);
return this.setState({ if (this.isMounted()) {
url: window.location.pathname return this.setState({
}); url: window.location.pathname
});
}
} }
}, },
render: function() { render: function() {
@ -439,7 +443,7 @@ module.exports = function(queries, Child, load) {
},{"../actions/TreeActions.coffee":1,"../stores/TreeStore.coffee":21,"./LoadComponent.coffee":10}],4:[function(require,module,exports){ },{"../actions/TreeActions.coffee":1,"../stores/TreeStore.coffee":21,"./LoadComponent.coffee":10}],4:[function(require,module,exports){
var Edit, TreeActions, a, button, clas, codemirror, div, extras, img, p, pre, query, reactify, recl, ref, rele; var Add, Edit, TreeActions, a, button, clas, codemirror, div, extras, img, input, p, pre, query, reactify, recl, ref, rele;
clas = require('classnames'); clas = require('classnames');
@ -455,7 +459,7 @@ recl = React.createClass;
rele = React.createElement; rele = React.createElement;
ref = React.DOM, div = ref.div, pre = ref.pre, p = ref.p, img = ref.img, a = ref.a, button = ref.button; ref = React.DOM, input = ref.input, div = ref.div, pre = ref.pre, p = ref.p, img = ref.img, a = ref.a, button = ref.button;
extras = { extras = {
spam: recl({ spam: recl({
@ -545,12 +549,57 @@ Edit = query({
ref1 = this.props.mime, mite = ref1.mite, octs = ref1.octs; ref1 = this.props.mime, mite = ref1.mite, octs = ref1.octs;
return rele(codemirror, { return rele(codemirror, {
value: octs, value: octs,
mode: mite,
readOnly: false, readOnly: false,
mode: mite autofocus: true
}); });
} }
})); }));
Add = recl({
displayName: "Add",
getInitialState: function() {
return {
edit: false
};
},
onClick: function() {
return this.setState({
edit: true
});
},
componentDidUpdate: function() {
if (this.state.edit) {
return $(this.getDOMNode()).focus();
}
},
render: function() {
if (!this.state.edit) {
return button({
onClick: this.onClick
}, "Add");
} else {
return input({
type: "text",
onKeyDown: (function(_this) {
return function(e) {
var neu, newPath;
if (13 === e.keyCode) {
neu = _this.getDOMNode().value;
newPath = _this.props.path + "/" + neu;
history.pushState({}, "", window.tree.basepath(newPath + "#edit"));
TreeActions.saveFile("/" + neu + _this.props.spur, '', function() {});
return _this.setState({
edit: false
});
}
};
})(this)
});
}
}
});
module.exports = query({ module.exports = query({
body: 'r', body: 'r',
name: 't', name: 't',
@ -562,9 +611,24 @@ module.exports = query({
displayName: "Body", displayName: "Body",
getInitialState: function() { getInitialState: function() {
return { return {
edit: false edit: document.location.hash === "#edit"
}; };
}, },
setEdit: function() {
this.hash = document.location.hash;
document.location.hash = "#edit";
return this.setState({
edit: true
});
},
unsetEdit: function() {
var ref1;
document.location.hash = (ref1 = this.hash) != null ? ref1 : "";
this.setState({
edit: false
});
return document.location.reload();
},
render: function() { render: function() {
var body, className, editButton, extra, onClick, own, ref1; var body, className, editButton, extra, onClick, own, ref1;
className = clas((ref1 = this.props.meta.layout) != null ? ref1.split(',') : void 0); className = clas((ref1 = this.props.meta.layout) != null ? ref1.split(',') : void 0);
@ -584,9 +648,7 @@ module.exports = query({
editButton = button({ editButton = button({
onClick: (function(_this) { onClick: (function(_this) {
return function() { return function() {
return _this.setState({ return _this.setEdit();
edit: true
});
}; };
})(this) })(this)
}, "Edit"); }, "Edit");
@ -597,9 +659,7 @@ module.exports = query({
var txt; var txt;
txt = $(_this.getDOMNode()).find('.CodeMirror')[0].CodeMirror.getValue(); txt = $(_this.getDOMNode()).find('.CodeMirror')[0].CodeMirror.getValue();
return TreeActions.saveFile(_this.props.spur, txt, function() { return TreeActions.saveFile(_this.props.spur, txt, function() {
return _this.setState({ return _this.unsetEdit();
edit: false
});
}); });
}; };
})(this); })(this);
@ -616,7 +676,10 @@ module.exports = query({
}), own ? editButton : void 0, body, extra('next', { }), own ? editButton : void 0, body, extra('next', {
dataPath: this.props.sein, dataPath: this.props.sein,
curr: this.props.name curr: this.props.name
}), own ? button({}, "Add") : void 0, extra('footer')); }), own ? rele(Add, {
spur: this.props.spur,
path: this.props.path
}) : void 0, extra('footer'));
} }
})); }));