Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor collection handling to its own widgets

  • Loading branch information...
commit be69145f2ea295e064248c37372b15c4083ebdb6 1 parent 688ac2d
@bergie authored
View
1  Cakefile
@@ -21,6 +21,7 @@ mergeDirs = (k) ->
fs.mkdirSync "merged"
series [
(sh "cp src/*.js merged/")
+ (sh "cp src/collectionWidgets/*.js merged/")
(sh "cp src/editingWidgets/*.js merged/")
], k
View
63 src/collectionWidgets/jquery.Midgard.midgardCollectionAdd.js
@@ -0,0 +1,63 @@
+// Create.js - On-site web editing interface
+// (c) 2011-2012 Henri Bergius, IKS Consortium
+// Create may be freely distributed under the MIT license.
+// For all details and documentation:
+// http://createjs.org/
+(function (jQuery, undefined) {
+ // # Create editing widget
+ jQuery.widget('Midgard.midgardCollectionAdd', {
+ addButton: null,
+
+ options: {
+ editingWidgets: null,
+ collection: null,
+ model: null,
+ view: null,
+ disabled: false,
+ vie: null,
+ editableOptions: null
+ },
+
+ _create: function () {
+ var widget = this;
+ widget.options.collection.url = widget.options.model.url();
+
+ widget.options.view.bind('add', function (itemView) {
+ //itemView.el.effect('slide');
+ widget.options.editableOptions.disabled = widget.options.disabled;
+ widget.options.editableOptions.model = itemView.model;
+ jQuery(itemView.el).midgardEditable(widget.options.editableOptions);
+ });
+
+ widget.options.view.collection.bind('add', function (model) {
+ model.primaryCollection = widget.options.collection;
+ widget.options.vie.entities.add(model);
+ model.collection = widget.options.collection;
+ });
+ },
+
+ _init: function () {
+ if (this.options.disabled) {
+ this.disable();
+ return;
+ }
+ this.enable();
+ },
+
+ enable: function () {
+ var widget = this;
+ widget.addButton = jQuery('<button class="btn"><i class="icon-plus"></i> Add</button>').button();
+ widget.addButton.addClass('midgard-create-add');
+ widget.addButton.click(function () {
+ widget.options.collection.add({});
+ });
+
+ jQuery(widget.options.view.el).after(widget.addButton);
+ },
+
+ disable: function () {
+ this.addButton.remove();
+ delete this.addButton;
+ }
+ });
+})(jQuery);
View
104 src/jquery.Midgard.midgardEditable.js
@@ -8,6 +8,7 @@
jQuery.widget('Midgard.midgardEditable', {
options: {
editables: [],
+ collections: [],
model: null,
editorOptions: {},
// the available widgets by data type
@@ -16,6 +17,9 @@
'Text': 'halloWidget',
'default': 'halloWidget'
},
+ collectionWidgets: {
+ 'default': 'midgardCollectionAdd'
+ },
toolbarState: 'full',
// returns the name of the widget to use for the given property
widgetName: function (data) {
@@ -57,6 +61,29 @@
jQuery(data.element).removeClass('ui-state-disabled');
}
},
+ collectionWidgetName: function (data) {
+ // TODO: Actual selection mechanism
+ return this.collectionWidgets['default'];
+ },
+ enableCollection: function (data) {
+ var widgetName = this.collectionWidgetName(data);
+ data.disabled = false;
+ if (typeof jQuery(data.element)[widgetName] !== 'function') {
+ throw new Error(widgetName + ' widget is not available');
+ }
+ jQuery(data.element)[widgetName](data);
+ jQuery(data.element).data('createCollectionWidgetName', widgetName);
+ return jQuery(data.element);
+ },
+ disableCollection: function (data) {
+ var widgetName = jQuery(data.element).data('createCollectionWidgetName');
+ data.disabled = true;
+ if (widgetName) {
+ // only if there has been an editing widget registered
+ jQuery(data.element)[widgetName](data);
+ jQuery(data.element).removeClass('ui-state-disabled');
+ }
+ },
addButton: null,
enable: function () {},
enableproperty: function () {},
@@ -65,7 +92,6 @@
deactivated: function () {},
changed: function () {},
vie: null,
- enableCollectionAdd: true
},
_create: function () {
@@ -98,12 +124,18 @@
instance: this.options.model,
entityElement: this.element
});
- if (!this.options.enableCollectionAdd) {
- return;
- }
+
_.forEach(this.vie.service('rdfa').views, function (view) {
if (view instanceof widget.vie.view.Collection && widget.options.model === view.owner) {
- widget._enableCollection(view);
+ var collection = widget.options.enableCollection({
+ model: widget.options.model,
+ collection: view.collection,
+ view: view,
+ element: view.el,
+ vie: widget.vie,
+ editableOptions: widget.options
+ });
+ widget.options.collections.push(collection);
}
});
},
@@ -119,11 +151,16 @@
});
});
this.options.editables = [];
-
- if (this.options.addButton) {
- this.options.addButton.remove();
- delete this.options.addButton;
- }
+ jQuery.each(this.options.collections, function (index, collectionWidget) {
+ widget.options.disableCollection({
+ widget: widget,
+ model: widget.options.model,
+ element: collectionWidget,
+ vie: widget.vie,
+ editableOptions: widget.options
+ });
+ });
+ this.options.collections = [];
this._trigger('disable', null, {
instance: this.options.model,
@@ -190,53 +227,6 @@
});
this.options.editables.push(editable);
- },
-
- _enableCollection: function (collectionView) {
- var widget = this;
-
- if (!collectionView.owner || collectionView.owner.getSubject() !== widget.options.model.getSubject()) {
- return;
- }
-
- if (widget.options.addButton) {
- return;
- }
-
- if (collectionView.template.length === 0) {
- // Collection view has no template and so can't add
- return;
- }
-
- collectionView.collection.url = widget.options.model.url();
-
- collectionView.bind('add', function (itemView) {
- //itemView.el.effect('slide');
- jQuery(itemView.el).midgardEditable({
- disabled: widget.options.disabled,
- model: itemView.model,
- vie: widget.vie,
- widgets: widget.options.widgets
- });
- });
-
- collectionView.collection.bind('add', function (model) {
- model.primaryCollection = collectionView.collection;
- widget.vie.entities.add(model);
- model.collection = collectionView.collection;
- });
-
- collectionView.bind('remove', function (itemView) {
- //itemView.el.hide('drop');
- });
-
- widget.options.addButton = jQuery('<button class="btn"><i class="icon-plus"></i> Add</button>').button();
- widget.options.addButton.addClass('midgard-create-add');
- widget.options.addButton.click(function () {
- collectionView.collection.add({});
- });
-
- jQuery(collectionView.el).after(widget.options.addButton);
}
});
})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.