From 4a7503ac103835d7091bc3f4068d8f0c3c05d3c6 Mon Sep 17 00:00:00 2001 From: Trevor Parscal Date: Tue, 18 Jun 2013 15:09:01 -0700 Subject: [PATCH] Add templates from toolbar Change-Id: I0aa21bed13d1dbc693c1db03eab903af55a1517a --- .../mw/targets/ve.init.mw.ViewPageTarget.js | 4 +- .../ve/ui/dialogs/ve.ui.MWReferenceDialog.js | 2 +- .../ui/dialogs/ve.ui.MWTransclusionDialog.js | 44 +++++++++++-------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js b/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js index 3d6c693bb5..2255695b2e 100644 --- a/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js +++ b/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js @@ -202,7 +202,9 @@ ve.init.mw.ViewPageTarget.compatibility = { ve.init.mw.ViewPageTarget.static.toolbarTools = [ { 'items': ['undo', 'redo'] }, { 'items': ['mwFormat'] }, - { 'items': ['bold', 'italic', 'mwLink', 'clear', 'mwMediaInsert', 'mwReference'] }, + { 'items': [ + 'bold', 'italic', 'mwLink', 'clear', 'mwMediaInsert', 'mwReference', 'mwTransclusion' + ] }, { 'items': ['number', 'bullet', 'outdent', 'indent'] } ]; diff --git a/modules/ve/ui/dialogs/ve.ui.MWReferenceDialog.js b/modules/ve/ui/dialogs/ve.ui.MWReferenceDialog.js index 9d4cc07341..57dee192d0 100644 --- a/modules/ve/ui/dialogs/ve.ui.MWReferenceDialog.js +++ b/modules/ve/ui/dialogs/ve.ui.MWReferenceDialog.js @@ -38,7 +38,7 @@ ve.ui.MWReferenceDialog.static.modelClasses = [ ve.dm.MWReferenceNode ]; ve.ui.MWReferenceDialog.static.toolbarTools = [ { 'items': ['undo', 'redo'] }, { 'items': ['mwFormat'] }, - { 'items': ['bold', 'italic', 'mwLink', 'clear', 'mwMediaInsert'] } + { 'items': ['bold', 'italic', 'mwLink', 'clear', 'mwMediaInsert', 'mwTransclusion'] } ]; ve.ui.MWReferenceDialog.static.surfaceCommands = [ diff --git a/modules/ve/ui/dialogs/ve.ui.MWTransclusionDialog.js b/modules/ve/ui/dialogs/ve.ui.MWTransclusionDialog.js index 53cd56fdbd..bb90b148b4 100644 --- a/modules/ve/ui/dialogs/ve.ui.MWTransclusionDialog.js +++ b/modules/ve/ui/dialogs/ve.ui.MWTransclusionDialog.js @@ -78,8 +78,6 @@ ve.ui.MWTransclusionDialog.prototype.initialize = function () { * Handle frame open events. * * @method - * @throws {Error} If the surface doesn't have a focused node - * @throws {Error} If the focused node is not a transclusion */ ve.ui.MWTransclusionDialog.prototype.onOpen = function () { // Parent method @@ -87,19 +85,18 @@ ve.ui.MWTransclusionDialog.prototype.onOpen = function () { // Sanity check this.node = this.surface.getView().getFocusedNode(); - if ( !this.node ) { - throw new Error( 'Surface does not have a focused node' ); - } - if ( !( this.node instanceof ve.ce.MWTransclusionNode ) ) { - throw new Error( 'Focused node is not a transclusion' ); - } // Properties this.transclusion = new ve.dm.MWTransclusionModel(); // Initialization - this.transclusion.load( ve.copyObject( this.node.getModel().getAttribute( 'mw' ) ) ) - .always( ve.bind( this.setupPages, this ) ); + if ( this.node instanceof ve.ce.MWTransclusionNode ) { + this.transclusion.load( ve.copyObject( this.node.getModel().getAttribute( 'mw' ) ) ) + .always( ve.bind( this.setupPages, this ) ); + } else { + this.transclusion.addPlaceholder(); + this.setupPages(); + } }; /** @@ -108,19 +105,25 @@ ve.ui.MWTransclusionDialog.prototype.onOpen = function () { * @param {string} action Action that caused the window to be closed */ ve.ui.MWTransclusionDialog.prototype.onClose = function ( action ) { - var surfaceModel = this.surface.getModel(); + var surfaceModel = this.surface.getModel(), + obj = this.transclusion.getPlainObject(); // Save changes if ( action === 'apply' ) { - // TODO: Wrap attribute changes in ve.dm.SurfaceFragment - surfaceModel.change( - ve.dm.Transaction.newFromAttributeChanges( - surfaceModel.getDocument(), - this.node.getOffset(), - { 'mw': this.transclusion.getPlainObject() } - ) - ); + if ( this.node instanceof ve.ce.MWTransclusionNode ) { + surfaceModel.getFragment().changeAttributes( { 'mw': obj } ); + } else { + surfaceModel.getFragment().collapseRangeToEnd().insertContent( [ + { + 'type': 'mwTransclusionInline', + 'attributes': { + 'mw': obj + } + }, + { 'type': '/mwTransclusionInline' } + ] ); + } } this.clearPages(); @@ -337,6 +340,9 @@ ve.ui.MWTransclusionDialog.prototype.setupPages = function () { } else if ( part instanceof ve.dm.MWTransclusionContentModel ) { // Add wikitext page this.addPage( part.getId(), this.getContentPage( part ) ); + } else if ( part instanceof ve.dm.MWTemplatePlaceholderModel ) { + // Add template placeholder page + this.addPage( part.getId(), this.getPlaceholderPage( part ) ); } }