1 2 (function(window, $){ 3 var CMIS; 4 5 /** 6 Holds all our CMIS code 7 @class 8 */ 9 CMIS = {}; 10 11 /** 12 Wrap a Node of type Feed or Entry 13 @class 14 @param node XMLNode of an entry or feed 15 */ 16 CMIS.Node = function(node) { 17 var that = this; 18 19 /** The XML node wrapped. */ 20 this.xmlNode = node; 21 22 /** The title of the feed or Entry. */ 23 this.title = this.xmlNode.children("title").eq(0).text(); 24 25 /** The properties of the feed or Entry. */ 26 this.properties = {}; 27 28 this._entry_cache_by_id = null; 29 this._entries_cache = null; 30 31 _getPropertiesNode(this.xmlNode).children().each(function(i, node) { 32 that.properties[$(node).attr("propertyDefinitionId")] = $(node).find("[nodeName=cmis:value]").text(); 33 }); 34 35 if (this.getProperty("cmis:objectId")) { 36 this.id = this.getProperty("cmis:objectId"); 37 } else { 38 this.id = this.xmlNode.children("id").text(); 39 } 40 } 41 42 /** Get the links for the current Node 43 @param {String} [rel] the relation kind of link is optional 44 @param {String} [type] the type of the link is optional 45 */ 46 CMIS.Node.prototype.getLinks = function(rel, type) { 47 48 var selector = "link", res = []; 49 if (rel !== undefined) { 50 selector += "[rel=" + rel + "]"; 51 } 52 53 if (type !== undefined) { 54 selector += "[type=" + type + "]"; 55 } 56 this.xmlNode.children(selector).each(function(i, n) { 57 res.push($(n).attr("href")); 58 }); 59 return res; 60 } 61 62 /** 63 Is the current Node is a feed? 64 */ 65 CMIS.Node.prototype.isFeed = function() { 66 return this.xmlNode.is("feed"); 67 } 68 69 /** 70 Is the current Node is an entry? 71 */ 72 CMIS.Node.prototype.isEntry = function() { 73 return this.xmlNode.is("entry"); 74 } 75 76 /** 77 Is the base type of the current Node is a Folder? 78 */ 79 CMIS.Node.prototype.isFolder = function() { 80 return this.properties["cmis:baseTypeId"] === "cmis:folder"; 81 } 82 83 /** 84 Return a property from the current Node 85 if the property does not exist, return null 86 @param {String} key The propertyDefinitionId of the property 87 */ 88 CMIS.Node.prototype.getProperty = function(key) { 89 return (key in this.properties ? this.properties[key] : null); 90 } 91 92 /** 93 Return the parent ID from the current Node 94 if the Node has no parent, return null 95 */ 96 CMIS.Node.prototype.getParentId = function() { 97 return this.getProperty("cmis:parentId"); 98 } 99 100 /** 101 Return all the entries in a feed 102 */ 103 CMIS.Node.prototype.getEntries = function() { 104 var that = this; 105 if (!this.isFeed()) { 106 return []; 107 } 108 109 if (this._entries_cache === null) { 110 this._entries_cache = []; 111 this.xmlNode.children("entry").each(function(i, node) { 112 that._entries_cache.push(new CMIS.Node($(node))); 113 }); 114 this._entries_cache = $(this._entries_cache); 115 } 116 117 return this._entries_cache; 118 } 119 120 121 /** 122 Return the URL of the content 123 this is only useful if the node is an entry 124 */ 125 CMIS.Node.prototype.getContentUrl = function() { 126 return this.xmlNode.children("content").attr("src"); 127 } 128 129 /** 130 Return the entry of a given ID from a feed 131 @param {String} id The id of a document (cmis:objectId if available) 132 */ 133 CMIS.Node.prototype.getEntry = function(id) { 134 if (!this.isFeed()) { 135 return null; 136 } 137 if (this._entry_cache_by_id === null) { 138 var that = this; 139 this._entry_cache_by_id = {}; 140 this.getEntries().each(function(i, entry) { 141 that._entry_cache_by_id[entry.id] = entry; 142 }); 143 } 144 return id in this._entry_cache_by_id ? this._entry_cache_by_id[id] : null; 145 } 146 147 /** 148 Wrap a Workspace node 149 @class 150 @param node XMLNode of a Workspace 151 */ 152 CMIS.Workspace = function(node) { 153 var that = this; 154 155 /** The XML node wrapped. */ 156 this.xmlNode = node; 157 158 /** The title of the workspace. */ 159 this.title = this.xmlNode.children("title").eq(0).text(); 160 161 /** The link to collections indexed by collection type. */ 162 this.collections = {}; 163 this.xmlNode.children("collection").each(function(i, col) { 164 that.collections[$(col).find("[nodeName=cmisra:collectionType]").eq(0).text()] = $(col).attr("href"); 165 }); 166 }; 167 168 /** The name of the vendor of this workspace */ 169 CMIS.Node.prototype.getVendorName = function() { 170 return this.xmlNode.find("[nodeName=cmis:vendorName]").text() 171 } 172 173 /** Get the links for the Workspace 174 @function 175 @param {String} [rel] the relation kind of link is optional. 176 @param {String} [type] the type of the link is optional. 177 */ 178 CMIS.Workspace.prototype.getLinks = CMIS.Node.prototype.getLinks; 179 180 /** 181 Is this is a workspace? 182 */ 183 CMIS.Workspace.prototype.isWorkspace = function(){return true}; 184 185 186 187 /** 188 Parse a feed or an entry 189 @param {Document} xml the relation kind of link is optional. 190 @returns {CMIS.Node|CMIS.Workspace[]} CMIS.Node or an array of CMIS.Workspace . 191 */ 192 CMIS.parse = function(xml) { 193 if ($(xml).children("feed").length > 0) { 194 return new CMIS.Node($(xml).children("feed").eq(0)); 195 } else if ($(xml).children("service").length > 0) { 196 var res = []; 197 $(xml).children("service").children("workspace").each(function(i, workspace) { 198 res.push(new CMIS.Workspace($(workspace))); 199 }); 200 201 return res; 202 } 203 return new CMIS.Node($(xml).children("entry").eq(0)); 204 } 205 206 window.CMIS = CMIS; 207 208 209 210 /* 211 * Helper function 212 */ 213 var _getPropertiesNode = function(node) { 214 // this doesn't work due to the namespace, so we use another way arround 215 // var props = node.children("cmisra\\:object > cmis\\:properties"); 216 var res = null; 217 propsNodes = $(node).find("[nodeName=cmis:properties]").each(function(i, propsNode) { 218 if ($(propsNode).parent().parent()[0] === $(node)[0]) { 219 res = $(propsNode); 220 return false; 221 } 222 }); 223 return res || $(); 224 }; 225 })(window, jQuery); 226