var transformed = transformations.fromXML.getDocumentDescription(xml);\r
view.setBody(transformed.HTMLTree);\r
view.selectFirstNode();\r
- //isDirty = false;\r
+ sandbox.publish('documentSet');\r
+ },\r
+ getDocument: function() {\r
+ return transformations.toXML.getXML(view.getBody());\r
},\r
modifyCurrentNode: function(attr, value) {\r
- if(view.currentNode)\r
+ if(view.currentNode) {\r
view.currentNode.attr('wlxml-'+attr, value);\r
+ sandbox.publish('contentEdited');\r
+ }\r
},\r
highlightNode: function(wlxmlNode) {\r
view.highlightNode(wlxmlNode);\r
}\r
\r
transformations.toXML = {\r
- getXML: function(documentDescription) {\r
+ getXML: function(body) {\r
\r
- var inner = $(documentDescription.HTMLTree);\r
+ var inner = $(body);\r
var toret = $('<div></div>');\r
toret.append(inner);\r
\r
toret.append(div.contents());\r
return toret;\r
});\r
- \r
- var meta = $('<metadata></metadata>\n');\r
- _.each(_.keys(documentDescription.metadata), function(key) {\r
- meta.append('\n\t<dc:'+key+'>' + documentDescription.metadata[key] + '</dc:'+key+'>');\r
- });\r
- meta.append('\n');\r
- \r
- var metadata = toret.find('metadata');\r
- if(metadata.length === 0) {\r
- var section = toret.find('section');\r
- section = section.length ? $(section[0]) : null;\r
- if(section) {\r
- section.prepend(meta)\r
- }\r
- } else {\r
- metadata.replaceWith(meta);\r
- }\r
- \r
- \r
+\r
return vkbeautify.xml(toret.html());\r
}\r
}\r
var view = {\r
node: $(_.template(mainTemplate)()),\r
setup: function() {\r
+ var view = this;\r
var metaTable = this.metaTable = this.node.find('table');\r
\r
this.node.find('.rng-module-metadataEditor-addBtn').click(function() {\r
var newRow = view._addMetaRow('', '');\r
$(newRow.find('td div')[0]).focus();\r
- //isDirty = true;\r
+ sandbox.publish('metadataChanged', view.getMetadata());\r
});\r
\r
this.metaTable.on('click', '.rng-visualEditor-metaRemoveBtn', function(e) {\r
$(e.target).closest('tr').remove();\r
- //isDirty = true;\r
+ sandbox.publish('metadataChanged', view.getMetadata());\r
});\r
\r
this.metaTable.on('keydown', '[contenteditable]', function(e) {\r
}\r
e.preventDefault();\r
}\r
- \r
});\r
+ \r
+ \r
+ var onKeyUp = function(e) {\r
+ if(e.which !== 13)\r
+ sandbox.publish('metadataChanged', view.getMetadata());\r
+ }\r
+ this.metaTable.on('keyup', '[contenteditable]', _.throttle(onKeyUp, 500));\r
},\r
- getMetaData: function() {\r
+ getMetadata: function() {\r
var toret = {};\r
this.node.find('tr').each(function() {\r
var tr = $(this);\r
start: function() {\r
sandbox.publish('ready');\r
},\r
- setMetadata: function(xml) {\r
+ setDocument: function(xml) {\r
view.setMetadata(transformations.getMetadata(xml));\r
+ sandbox.publish('metadataSet');\r
},\r
getMetadata: function() {\r
- return view.getMetadata();\r
+ return transformations.getXML(view.getMetadata());\r
},\r
getView: function() {\r
return view.node;\r
+ },\r
+ attachMetadata: function(document) {\r
+ var toret = $('<div>');\r
+ toret.append($(document));\r
+ var meta = $('<metadata></metadata>\n').append(transformations.getXML(view.getMetadata()));\r
+ \r
+ var metadata = toret.find('metadata');\r
+ if(metadata.length === 0) {\r
+ var section = toret.find('section');\r
+ section = section.length ? $(section[0]) : null;\r
+ if(section) {\r
+ section.prepend(meta)\r
+ }\r
+ } else {\r
+ metadata.replaceWith(meta);\r
+ }\r
+ return toret.html();\r
}\r
\r
};\r
views.mainTabs.addTab(title, slug, view);\r
}\r
\r
+ var dirty = {\r
+ sourceEditor: false,\r
+ documentCanvas: false,\r
+ metadataEditor: false,\r
+ };\r
+ \r
var commands = {\r
highlightDocumentNode: function(wlxmlNode, origin) {\r
['documentCanvas', 'nodeBreadCrumbs', 'nodeFamilyTree'].forEach(function(moduleName) {\r
\r
views.visualEditingSidebar.addTab({icon: 'pencil'}, 'edit', views.currentNodePaneLayout.getAsView());\r
\r
+ views.mainTabs.on('tabSelected', function(event) {\r
+ if(event.prevSlug) {\r
+ if(event.prevSlug === 'sourceEditor' && dirty.sourceEditor) {\r
+ sandbox.getModule('data').commitDocument(sandbox.getModule('sourceEditor').getDocument(), 'source_edit');\r
+ }\r
+ if(event.prevSlug === 'editor' && (dirty.documentCanvas || dirty.metadataEditor)) {\r
+ var doc = dirty.documentCanvas ? sandbox.getModule('documentCanvas').getDocument() : sandbox.getModule('data').getDocument();\r
+ if(dirty.metadataEditor) {\r
+ doc = sandbox.getModule('metadataEditor').attachMetadata(doc);\r
+ }\r
+ sandbox.getModule('data').commitDocument(doc, 'edit');\r
+ }\r
+ \r
+ }\r
+ });\r
\r
/* Events handling */\r
\r
\r
eventHandlers.sourceEditor = {\r
ready: function() {\r
- addMainTab(gettext('Source'), 'source', sandbox.getModule('sourceEditor').getView());\r
+ addMainTab(gettext('Source'), 'sourceEditor', sandbox.getModule('sourceEditor').getView());\r
sandbox.getModule('sourceEditor').setDocument(sandbox.getModule('data').getDocument());\r
+ },\r
+ xmlChanged: function() {\r
+ dirty.sourceEditor = true;\r
+ },\r
+ documentSet: function() {\r
+ dirty.sourceEditor = false;\r
}\r
};\r
\r
});\r
},\r
documentChanged: function(document, reason) {\r
- var slug = (reason === 'visual_edit' ? 'source' : 'visual');\r
- sandbox.getModule(slug+'Editor').setDocument(document);\r
+ var modules = [];\r
+ if(reason === 'source_edit')\r
+ modules = ['documentCanvas', 'metadataEditor'];\r
+ else if (reason === 'edit')\r
+ modules = ['sourceEditor'];\r
+ \r
+ modules.forEach(function(moduleName) {\r
+ sandbox.getModule(moduleName).setDocument(document);\r
+ });\r
},\r
savingStarted: function() {\r
sandbox.getModule('mainBar').setCommandEnabled('save', false);\r
sandbox.getModule('documentCanvas').setDocument(sandbox.getModule('data').getDocument());\r
views.visualEditing.setView('leftColumn', sandbox.getModule('documentCanvas').getView());\r
},\r
+ documentSet: function() {\r
+ dirty.documentCanvas = false;\r
+ },\r
\r
nodeSelected: function(wlxmlNode) {\r
commands.selectNode(wlxmlNode);\r
},\r
\r
contentChanged: function() {\r
- \r
+ dirty.documentCanvas = true;\r
},\r
\r
nodeHovered: function(wlxmlNode) {\r
\r
eventHandlers.metadataEditor = {\r
ready: function() {\r
- sandbox.getModule('metadataEditor').setMetadata(sandbox.getModule('data').getDocument());\r
+ sandbox.getModule('metadataEditor').setDocument(sandbox.getModule('data').getDocument());\r
views.visualEditingSidebar.addTab({icon: 'info-sign'}, 'metadataEditor', sandbox.getModule('metadataEditor').getView());\r
- }\r
+ },\r
+ metadataChanged: function(metadata) {\r
+ dirty.metadataEditor = true;\r
+ },\r
+ metadataSet: function() {\r
+ dirty.metadataEditor = false;\r
+ },\r
};\r
\r
eventHandlers.nodeFamilyTree = {\r
return function(sandbox) {\r
\r
var view = $(sandbox.getTemplate('main')());\r
- var isDirty = false;\r
\r
var editor = ace.edit(view.find('#rng-sourceEditor-editor')[0]);\r
editor.setTheme("ace/theme/chrome");\r
editor.getSession().setMode("ace/mode/xml");\r
$('textarea', view).on('keyup', function() {\r
- isDirty = true;\r
+ sandbox.publish('xmlChanged');\r
});\r
\r
editor.getSession().on('change', function() {\r
- isDirty = true;\r
+ sandbox.publish('xmlChanged');\r
})\r
return {\r
start: function() {\r
setDocument: function(document) {\r
editor.setValue(document);\r
editor.gotoLine(0)\r
- isDirty = false;\r
+ sandbox.publish('documentSet');\r
},\r
getDocument: function() {\r
return editor.getValue();\r
- },\r
- isDirty: function() {\r
- return isDirty;\r
- },\r
- setDirty: function(dirty) {\r
- isDirty = dirty;\r
}\r
- \r
}\r
};\r
\r
this.nodes.tabBar.find('.active').removeClass('active');\r
this.nodes.tabBar.find('a[href="#'+slug+'"]').parent().addClass('active');\r
\r
+ var prevSlug = this.selectedTab;\r
this.selectedTab = slug;\r
- this.trigger('tabSelected', slug);\r
+ this.trigger('tabSelected', {slug: slug, prevSlug: prevSlug});\r
}\r
},\r
\r