Permalink
Browse files

Switch container management to the generic VIE library

  • Loading branch information...
1 parent 65a739b commit 3ada9480dc977b7117b79586e302053af82d538d @bergie bergie committed Feb 23, 2011
Showing with 222 additions and 202 deletions.
  1. +3 −3 static/js/collections.js
  2. +158 −0 static/js/deps/vie-containermanager.js
  3. +3 −4 static/js/editable.js
  4. +58 −195 static/js/objectmanager.js
View
@@ -54,7 +54,7 @@ midgardCreate.Collections = {
var collectionElement = jQuery(collectionElement);
var firstChild = collectionElement.children(':first-child');
- if (midgardCreate.objectManager.getIdentifierForContainer(firstChild) === 'mgd:containerPlaceholder') {
+ if (VIE.ContainerManager._getContainerValue(firstChild, 'about') === 'mgd:containerPlaceholder') {
firstChild.hide();
}
@@ -69,7 +69,7 @@ midgardCreate.Collections = {
}
var collectionCollection = Backbone.Collection.extend({
- model: midgardCreate.objectManager.getModelForContainer(firstChild),
+ model: VIE.ContainerManager.getModelForContainer(firstChild),
order: orderFromElement,
urlpattern: urlPattern
});
@@ -83,7 +83,7 @@ midgardCreate.Collections = {
});
}
- var itemView = midgardCreate.objectManager.getViewForContainer(midgardCreate.objectManager.getCleanContainer(firstChild));
+ var itemView = VIE.ContainerManager._getViewForContainer(VIE.ContainerManager.cloneContainer(firstChild));
var collectionView = Backbone.View.extend({
collection: collectionInstance,
el: collectionElement,
@@ -0,0 +1,158 @@
+if (typeof VIE === 'undefined') {
+ VIE = {};
+}
+
+VIE.ContainerManager = {
+ models: {},
+ views: {},
+
+ /**
+ * @private
+ */
+ _getContainerProperties: function(element, emptyValues) {
+ var containerProperties = {};
+
+ jQuery.each(jQuery('[property]', element), function(index, objectProperty) {
+ var objectProperty = jQuery(objectProperty);
+ var propertyName = objectProperty.attr('property');
+
+ if (emptyValues) {
+ containerProperties[propertyName] = '';
+ return true;
+ }
+
+ containerProperties[propertyName] = objectProperty.html();
+ });
+
+ return containerProperties;
+ },
+
+ /**
+ * @private
+ */
+ _getContainerValue: function(element, propertyName) {
+ var element = jQuery(element);
+
+ if (typeof element.attr(propertyName) !== 'undefined')
+ {
+ // Direct match with container
+ return element.attr(propertyName);
+ }
+ return element.find('[' + propertyName + ']').attr(propertyName);
+ },
+
+ cloneContainer: function(element) {
+ var element = jQuery(element).clone(false);
+
+ if (typeof element.attr('about') !== 'undefined')
+ {
+ // Direct match with container
+ element.attr('about', '');
+ }
+ element.find('[about]').attr('about', '');
+ element.find('[property]').html('');
+
+ return element;
+ },
+
+ /**
+ * @access private
+ */
+ _getViewForContainer: function(element) {
+ var element = jQuery(element);
+ var type = VIE.ContainerManager._getContainerValue(element, 'typeof');
+
+ if (typeof VIE.ContainerManager.views[type] !== 'undefined') {
+ // We already have a view for this type
+ return VIE.ContainerManager.views[type];
+ }
+
+ var viewProperties = {};
+ viewProperties.initialize = function() {
+ _.bindAll(this, 'render');
+ this.model.bind('change', this.render);
+ this.model.view = this;
+ };
+ viewProperties.tagName = element.get(0).nodeName;
+ viewProperties.make = function(tagName, attributes, content) {
+ return VIE.ContainerManager.cloneContainer(element);
+ };
+ viewProperties.render = function() {
+ var model = this.model;
+ jQuery('[property]', this.el).each(function(index, propertyElement) {
+ var propertyElement = jQuery(propertyElement);
+ var property = propertyElement.attr('property');
+ propertyElement.html(model.get(property));
+ });
+ return this;
+ };
+
+ VIE.ContainerManager.views[type] = Backbone.View.extend(viewProperties);
+
+ return VIE.ContainerManager.views[type];
+ },
+
+ getModelForContainer: function(element) {
+ var type = VIE.ContainerManager._getContainerValue(element, 'typeof');
+
+ if (typeof VIE.ContainerManager.models[type] !== 'undefined') {
+ // We already have a model for this type
+ return VIE.ContainerManager.models[type];
+ }
+
+ // Parse the relevant properties from DOM
+ var modelPropertiesFromRdf = VIE.ContainerManager._getContainerProperties(element, true);
+ var modelProperties = jQuery.extend({}, modelPropertiesFromRdf);
+
+ modelProperties.type = type;
+
+ modelProperties.initialize = function() {
+ var modelInstance = this;
+ var populateProperties = {};
+ jQuery.each(modelPropertiesFromRdf, function(propName, propValue) {
+
+ if (!modelInstance.get(propName)) {
+ populateProperties[propName] = modelInstance.getPlaceholder(propName);
+ }
+
+ });
+
+ if (!jQuery.isEmptyObject(populateProperties)) {
+ modelInstance.set(populateProperties);
+ }
+ };
+
+ VIE.ContainerManager.findAdditionalModelProperties(element, modelProperties);
+
+ VIE.ContainerManager.models[type] = Backbone.Model.extend(modelProperties);
+
+ return VIE.ContainerManager.models[type];
+ },
+
+ /**
+ * Override this to seek additional properties from the element to include to the model
+ */
+ findAdditionalModelProperties: function(element, properties) {
+ },
+
+ /**
+ * Override this to seek additional properties from the element to include to the instance
+ */
+ findAdditionalInstanceProperties: function(element, properties) {
+ },
+
+ getInstanceForContainer: function(element) {
+ var model = VIE.ContainerManager.getModelForContainer(element);
+ var properties = VIE.ContainerManager._getContainerProperties(element, false);
+ var view = VIE.ContainerManager._getViewForContainer(element);
+
+ properties.id = VIE.ContainerManager._getContainerValue(element, 'about');
+
+ VIE.ContainerManager.findAdditionalInstanceProperties(element, properties);
+
+ var modelInstance = new model(properties);
+ modelInstance.view = new view({model: modelInstance, el: element});
+
+ return modelInstance;
+ }
+};
View
@@ -94,7 +94,6 @@ midgardCreate.Editable = {
if (midgardCreate.Editable.currentObject === null) {
return;
}
-
midgardCreate.Editable.currentObject.getWorkflowState(function(stateData) {
midgardCreate.Editable.objectActions.empty();
@@ -190,7 +189,7 @@ midgardCreate.Editable = {
// Seek editable properties from RDFa
objectInstance.editables = {};
- jQuery.each(jQuery('[property]', objectInstance.view.el), function(index, objectProperty)
+ jQuery('[property]', objectInstance.view.el).each(function(index, objectProperty)
{
var objectProperty = jQuery(objectProperty);
@@ -204,7 +203,7 @@ midgardCreate.Editable = {
midgardCreate.Editable.activateEditable(objectInstance, propertyName);
});
GENTICS.Aloha.EventRegistry.subscribe(objectInstance.editables[propertyName], 'editableDeactivated', function() {
- midgardCreate.Editable.activateEditable(objectInstance, propertyName);
+ midgardCreate.Editable.deactivateEditable(objectInstance, propertyName);
});
objectProperty.effect('highlight', { color: midgardCreate.highlightcolor }, 3000);
@@ -225,7 +224,7 @@ midgardCreate.Editable = {
// No identifier set, therefore not editable
return true;
}
- var objectInstance = midgardCreate.objectManager.getInstanceForContainer(objectContainer);
+ var objectInstance = VIE.ContainerManager.getInstanceForContainer(objectContainer);
midgardCreate.Editable.enableEditable(objectInstance);
});
Oops, something went wrong.

0 comments on commit 3ada948

Please sign in to comment.