Robmoen has uploaded a new change for review.
https://gerrit.wikimedia.org/r/58274
Change subject: Hook in MetaList and MWCategoryMetaItems into category ui widget
......................................................................
Hook in MetaList and MWCategoryMetaItems into category ui widget
ve.dm.MWCategoryMetaItem.js
* add MWcategory group static property
* deleteRemovedItem: return deleted item so that it may be emitted
ve.dm.Surface.js
* Construct MetaList
ve.ui.MetaDialog.js
* Hook in MetaList and add remove and insert listener methods.
** insert / remove methods to be improved upon additional meta nodes
* Add listeners to mwCategoryWidget for new categories and sort key updates
ve.ui.CategoryGroupItemWidget.js
* Integrate Insert, update, remove. Emit on new and category group item
updates.
ve.ui.MWCategoryInputWidget.js
* Add category namespace when getting base item from value
Change-Id: Ia464724a6291a948938fca2fa9cb778afeccfc29
TODO: js duck style documentation.
---
M modules/ve/dm/metaitems/ve.dm.MWCategoryMetaItem.js
M modules/ve/dm/ve.dm.MetaList.js
M modules/ve/dm/ve.dm.Surface.js
M modules/ve/ui/dialogs/ve.ui.MetaDialog.js
M modules/ve/ui/widgets/ve.ui.CategoryGroupItemWidget.js
M modules/ve/ui/widgets/ve.ui.CategoryWidget.js
M modules/ve/ui/widgets/ve.ui.MWCategoryInputWidget.js
7 files changed, 101 insertions(+), 24 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor
refs/changes/74/58274/1
diff --git a/modules/ve/dm/metaitems/ve.dm.MWCategoryMetaItem.js
b/modules/ve/dm/metaitems/ve.dm.MWCategoryMetaItem.js
index 072d2a9..bd0bf1e 100644
--- a/modules/ve/dm/metaitems/ve.dm.MWCategoryMetaItem.js
+++ b/modules/ve/dm/metaitems/ve.dm.MWCategoryMetaItem.js
@@ -26,6 +26,8 @@
ve.dm.MWCategoryMetaItem.static.name = 'MWcategory';
+ve.dm.MWCategoryMetaItem.static.group = 'MWcategory';
+
ve.dm.MWCategoryMetaItem.static.matchTagNames = [ 'link' ];
ve.dm.MWCategoryMetaItem.static.matchRdfaTypes = [ 'mw:WikiLink/Category' ];
diff --git a/modules/ve/dm/ve.dm.MetaList.js b/modules/ve/dm/ve.dm.MetaList.js
index feef3ee..ec0ed1e 100644
--- a/modules/ve/dm/ve.dm.MetaList.js
+++ b/modules/ve/dm/ve.dm.MetaList.js
@@ -102,14 +102,14 @@
ins = reversed ? ops[i].remove : ops[i].insert;
rm = reversed ? ops[i].insert : ops[i].remove;
for ( j = 0, jlen = rm.length; j < jlen; j++ ) {
- this.deleteRemovedItem( offset, index +
j );
+ item = this.deleteRemovedItem( offset,
index + j );
removedItems.push( { 'item': item,
'offset': offset, 'index': index } );
}
for ( j = 0, jlen = ins.length; j < jlen; j++ )
{
item =
ve.dm.metaItemFactory.createFromElement( ins[j] );
// offset and index are
pre-transaction, but we'll fix them later
this.addInsertedItem( offset, index +
j, item );
- insertedItems.push( {'item': item } );
+ insertedItems.push( { 'item': item } );
}
index += rm.length;
break;
@@ -266,7 +266,6 @@
this.groups[group] = [ item ];
}
item.attach( this, offset, index );
- this.emit( 'insert', item );
};
/**
@@ -291,7 +290,7 @@
if ( at !== null ) {
this.groups[group].splice( at, 1 );
}
- this.emit( 'remove', item );
item.detach( this );
+ return item;
};
diff --git a/modules/ve/dm/ve.dm.Surface.js b/modules/ve/dm/ve.dm.Surface.js
index 4ba18e8..2222792 100644
--- a/modules/ve/dm/ve.dm.Surface.js
+++ b/modules/ve/dm/ve.dm.Surface.js
@@ -19,6 +19,7 @@
// Properties
this.documentModel = doc;
+ this.metaList = new ve.dm.MetaList( this );
this.selection = new ve.Range( 0, 0 );
this.selectedNodes = {};
this.smallStack = [];
diff --git a/modules/ve/ui/dialogs/ve.ui.MetaDialog.js
b/modules/ve/ui/dialogs/ve.ui.MetaDialog.js
index d179ed8..86d2c0a 100644
--- a/modules/ve/ui/dialogs/ve.ui.MetaDialog.js
+++ b/modules/ve/ui/dialogs/ve.ui.MetaDialog.js
@@ -18,6 +18,10 @@
ve.ui.MetaDialog = function VeUiMetaDialog( surface ) {
// Parent constructor
ve.ui.Dialog.call( this, surface );
+ this.metaList = surface.getModel().metaList;
+
+ // Events
+ this.metaList.addListenerMethods( this, { 'insert': 'onMetaListInsert',
'remove': 'onMetaListRemove' } );
};
/* Inheritance */
@@ -86,20 +90,88 @@
this.$body.append( this.layout.$ );
this.outlinePanel.$.append( this.outlineWidget.$ );
- // Dummmy categories data
- var categoryWidget = new ve.ui.CategoryWidget( {
+ // MW Categories
+ this.mwCategoryWidget = new ve.ui.CategoryWidget( {
'$$': this.$$,
'$overlay': this.$overlay,
- 'categoryItems': [
- { 'name': 'Living people', 'value': 'Living
people', 'metaItem': {} },
- { 'name': 'Cats', 'value': 'Cats', 'metaItem':
{} }
- ]
+ 'categoryItems': this.getMwCategoryItems()
} );
+ // Append the category widget to the category editor panel
+ this.editorPanels.categories.$.append( this.mwCategoryWidget.$ );
- this.editorPanels.categories.$.append( categoryWidget.$ );
+ this.mwCategoryWidget.addListenerMethods( this, { 'newCategory':
'onNewCategory', 'updateSortkey': 'onUpdateSortKey' } );
+
this.layout.update();
};
+/* MW Categories */
+
+ve.ui.MetaDialog.prototype.getMwCategoryItems = function () {
+ var metaListMwCategories = this.metaList.getItemsInGroup( 'MWcategory'
),
+ MwCategoryItems = [],
+ i;
+
+ // Loop through MwCategories and build out items
+ for ( i = 0; i < metaListMwCategories.length; i++ ) {
+ MwCategoryItems.push( this.getCategoryItemFromMetaListItem(
metaListMwCategories[i] ) );
+ }
+ return MwCategoryItems;
+};
+
+ve.ui.MetaDialog.prototype.getCategoryItemFromMetaListItem = function (
metaItem ) {
+ return {
+ 'name': metaItem.element.attributes.category,
+ 'value': metaItem.element.attributes.category.split( ':' )[1],
+ // TODO: sortkey is lcase, make consistent throughout
CategoryWidget
+ 'sortKey': metaItem.element.attributes.sortkey,
+ 'metaItem': metaItem
+ };
+};
+
+ve.ui.MetaDialog.prototype.getCategoryItemForInsertion = function ( item ) {
+ return {
+ 'attributes': { 'category': item.name, 'sortkey': item.sortKey
|| '' },
+ 'type': 'MWcategory'
+ };
+};
+
+ve.ui.MetaDialog.prototype.onNewCategory = function ( item ) {
+ // Insert new metaList item
+ this.insertMetaListItem( this.getCategoryItemForInsertion( item ) );
+};
+
+ve.ui.MetaDialog.prototype.onUpdateSortKey = function ( item ) {
+ var offset = item.metaItem.offset,
+ index = item.metaItem.index;
+
+ item.metaItem.remove();
+ // It would seem as if insertItem happens before the onRemove event is
sent to CategoryWidget,
+ // Remove the reference there so it doesn't try to get removed again
onMetaListInsert
+ delete this.mwCategoryWidget.categories[item.name];
+ // Insert updated meta item at same offset and index
+ this.metaList.insertMeta( this.getCategoryItemForInsertion( item ),
offset, index );
+};
+
+/* Meta List */
+
+ve.ui.MetaDialog.prototype.onMetaListInsert = function ( metaItem ) {
+ // Responsible for adding UI components.
+ if ( metaItem.element.type === 'MWcategory' ) {
+ this.mwCategoryWidget.addItem(
this.getCategoryItemFromMetaListItem( metaItem ) );
+ }
+};
+// Responsible for removing UI components.
+ve.ui.MetaDialog.prototype.onMetaListRemove = function ( metaItem ) {
+ if ( metaItem.element.type === 'MWcategory' ) {
+ this.mwCategoryWidget.removeItem(
metaItem.element.attributes.category );
+ }
+};
+
+ve.ui.MetaDialog.prototype.insertMetaListItem = function ( metaItem ) {
+ var offset = this.surface.getModel().getDocument().getData().length;
+ this.metaList.insertMeta( metaItem, offset );
+};
+
/* Registration */
ve.ui.dialogFactory.register( 'meta', ve.ui.MetaDialog );
diff --git a/modules/ve/ui/widgets/ve.ui.CategoryGroupItemWidget.js
b/modules/ve/ui/widgets/ve.ui.CategoryGroupItemWidget.js
index 8430bea..d0cb34c 100644
--- a/modules/ve/ui/widgets/ve.ui.CategoryGroupItemWidget.js
+++ b/modules/ve/ui/widgets/ve.ui.CategoryGroupItemWidget.js
@@ -26,10 +26,10 @@
ve.ui.Widget.call( this, config );
// Properties
- this.name = config.item.value;
+ this.name = config.item.name;
this.value = config.item.value;
this.sortKey = config.item.sortKey || '';
- this.metaItem = config.metaItem;
+ this.metaItem = config.item.metaItem;
// Initialization
diff --git a/modules/ve/ui/widgets/ve.ui.CategoryWidget.js
b/modules/ve/ui/widgets/ve.ui.CategoryWidget.js
index 3e05605..e5d001f 100644
--- a/modules/ve/ui/widgets/ve.ui.CategoryWidget.js
+++ b/modules/ve/ui/widgets/ve.ui.CategoryWidget.js
@@ -72,7 +72,7 @@
/* Events */
- this.categoryInput.addListenerMethods( this, { 'newCategory':
'onNewCategory' } );
+ this.categoryInput.addListenerMethods( this, { 'newCategory':
'insertNewCategory' } );
this.removeButton.on( 'click', ve.bind( this.onRemoveCategory, this ) );
this.$removeButtonLabel.on( 'click', ve.bind( this.onRemoveCategory,
this ) );
this.$sortKeyForm.on( 'submit', ve.bind( this.onSortKeySubmit, this ) );
@@ -96,29 +96,31 @@
ve.ui.CategoryWidget.prototype.addItem = function ( item ) {
var categoryGroupItem = new ve.ui.CategoryGroupItemWidget( { '$$':
this.$$, 'item': item } ),
- existingItem = null;
+ existingCategoryGroupItem = null;
// Bind category item events.
categoryGroupItem.addListenerMethods( this, {
'togglePopupMenu': 'onTogglePoupupMenu'
} );
if ( item.name in this.categories ) {
- existingItem = this.categories[item.name];
- this.removeItem( item.name );
+ existingCategoryGroupItem = this.categories[item.name];
+ existingCategoryGroupItem.metaItem.remove();
}
this.categories[item.name] = categoryGroupItem;
- if ( existingItem ) {
- categoryGroupItem.sortKey = existingItem.sortKey;
+ if ( existingCategoryGroupItem ) {
+ categoryGroupItem.sortKey =
existingCategoryGroupItem.sortKey;
}
this.$categories.append( categoryGroupItem.$ );
return this;
};
-ve.ui.CategoryWidget.prototype.onNewCategory = function ( item ) {
- this.addItem( item );
+ve.ui.CategoryWidget.prototype.insertNewCategory = function ( item ) {
+ this.emit( 'newCategory', item );
};
ve.ui.CategoryWidget.prototype.onSortKeySubmit = function () {
- this.categories[this.popupCategory].sortKey =
this.sortKeyInput.$input.val();
+ var item = this.categories[this.popupCategory];
+ item.sortKey = this.sortKeyInput.$input.val();
+ this.emit( 'updateSortkey', item );
this.closePopup();
return false;
};
@@ -170,11 +172,12 @@
};
ve.ui.CategoryWidget.prototype.onRemoveCategory = function () {
- this.removeItem( this.popupCategory );
+ this.categories[this.popupCategory].metaItem.remove();
this.closePopup();
};
ve.ui.CategoryWidget.prototype.removeItem = function ( name ) {
+ // Remove from widget
this.categories[name].$.remove();
delete this.categories[name.name];
};
diff --git a/modules/ve/ui/widgets/ve.ui.MWCategoryInputWidget.js
b/modules/ve/ui/widgets/ve.ui.MWCategoryInputWidget.js
index 684cdd2..55c6e8d 100644
--- a/modules/ve/ui/widgets/ve.ui.MWCategoryInputWidget.js
+++ b/modules/ve/ui/widgets/ve.ui.MWCategoryInputWidget.js
@@ -162,7 +162,7 @@
};
ve.ui.MWCategoryInputWidget.prototype.getCategoryItemFromValue = function (
value ) {
- return { 'name': value, 'value': value, 'metaItem': {} };
+ return { 'name': this.categoryPrefix + value, 'value': value,
'metaItem': {} };
};
/**
--
To view, visit https://gerrit.wikimedia.org/r/58274
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia464724a6291a948938fca2fa9cb778afeccfc29
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Robmoen <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits