PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/frontend/alohaeditor/aloha/plugins/extra/vie/src/vie-collectionmanager.js

https://gitlab.com/klausmig/CloudSemanticWeb
JavaScript | 70 lines | 51 code | 16 blank | 3 comment | 3 complexity | cef0eb508de2c2c279881bac4298f9c1 MD5 | raw file
  1. if (typeof VIE === 'undefined') {
  2. VIE = {};
  3. }
  4. VIE.CollectionManager = {
  5. collections: [],
  6. loadCollections: function() {
  7. jQuery('[typeof="http://purl.org/dc/dcmitype/Collection"]').each(function() {
  8. VIE.CollectionManager.getCollectionForContainer(this);
  9. });
  10. return VIE.CollectionManager.collections;
  11. },
  12. getCollectionForContainer: function(element) {
  13. element = jQuery(element);
  14. var firstChild = element.children(':first-child');
  15. if (firstChild === undefined) {
  16. return null;
  17. }
  18. var preparedNewElement = VIE.ContainerManager.cloneContainer(firstChild);
  19. var Collection = Backbone.Collection.extend({
  20. model: VIE.ContainerManager.getModelForContainer(preparedNewElement)
  21. });
  22. var collectionInstance = new Collection({});
  23. collectionInstance.view = VIE.CollectionManager._getViewForCollection(preparedNewElement, element, collectionInstance);
  24. VIE.CollectionManager.collections.push(collectionInstance);
  25. return collectionInstance;
  26. },
  27. /**
  28. * @private
  29. */
  30. _getViewForCollection: function(element, collectionElement, collectionInstance) {
  31. var collectionView = Backbone.View.extend({
  32. collection: collectionInstance,
  33. el: collectionElement,
  34. initialize: function() {
  35. _.bindAll(this, 'addItem', 'removeItem');
  36. this.collection.bind('add', this.addItem);
  37. this.collection.bind('remove', this.removeItem);
  38. },
  39. addItem: function(itemInstance) {
  40. itemInstance = VIE.ContainerManager.registerInstance(itemInstance, VIE.ContainerManager.cloneContainer(element));
  41. var itemViewElement = itemInstance.views[0].render().el;
  42. this.el.append(itemViewElement);
  43. itemViewElement.show();
  44. },
  45. removeItem: function(itemInstance) {
  46. if (typeof itemInstance.view === 'undefined') {
  47. return;
  48. }
  49. itemInstance.view.el.hide();
  50. }
  51. });
  52. return new collectionView({});
  53. }
  54. };