1 /*! 2 * This file is part of Aloha Editor Project http://aloha-editor.org 3 * Copyright (c) 2010-2011 Gentics Software GmbH, aloha@gentics.com 4 * Contributors http://aloha-editor.org/contribution.php 5 * Licensed unter the terms of http://www.aloha-editor.org/license.html 6 * 7 * Aloha Editor is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Affero General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version.* 11 * 12 * Aloha Editor is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Affero General Public License for more details. 16 * 17 * You should have received a copy of the GNU Affero General Public License 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 define( 22 [ 'aloha/core', 'util/class', 'aloha/repositorymanager' ], 23 function( Aloha, Class, RepositoryManager ) { 24 "use strict"; 25 26 // var 27 // $ = jQuery, 28 // GENTICS = window.GENTICS, 29 // Aloha = window.Aloha, 30 // Class = window.Class; 31 32 /** 33 * Abstract Repository Class. Implement that class for your own repository. 34 * @namespace Aloha.Repository 35 * @class Repository 36 * @constructor 37 * @param {String} repositoryId unique repository identifier 38 * @param {String} repositoryName (optional) is the displyed name for this Repository instance 39 */ 40 var AbstractRepository = Class.extend({ 41 _constructor: function(repositoryId, repositoryName) { 42 /** 43 * @property repositoryId is the unique Id for this Repository instance 44 */ 45 this.repositoryId = repositoryId; 46 47 /** 48 * contains the repository's settings object 49 * @property settings {Object} the repository's settings stored in an object 50 */ 51 this.settings = {}; 52 53 /** 54 * @property repositoryName is the name for this Repository instance 55 */ 56 this.repositoryName = (repositoryName) ? repositoryName : repositoryId; 57 58 RepositoryManager.register(this); 59 }, 60 61 /** 62 * Init method of the repository. Called from Aloha Core to initialize this repository 63 * @return void 64 * @hide 65 */ 66 init: function() {}, 67 68 /** 69 * Searches a repository for object items matching queryString if none found returns null. 70 * The returned object items must be an array of Aloha.Repository.Object 71 * 72 <pre><code> 73 // simple delicious implementation 74 Aloha.Repositories.myRepository.query = function (params, callback) { 75 76 // make local var of this to use in ajax function 77 var that = this; 78 79 // handle each word as tag 80 var tags = p.queryString.split(' '); 81 82 // if we have a query and no tag matching return 83 if ( p.queryString && tags.length == 0 ) { 84 callback.call( that, []); 85 return; 86 } 87 88 // no handling of objectTypeFilter, filter, inFolderId, etc... 89 // in real implementation you should handle all parameters 90 91 jQuery.ajax({ type: "GET", 92 dataType: "jsonp", 93 url: 'http://feeds.delicious.com/v2/json/' + tags.join('+'), 94 success: function(data) { 95 var items = []; 96 // convert data to Aloha objects 97 for (var i = 0; i < data.length; i++) { 98 if (typeof data[i] != 'function' ) { 99 items.push(new Aloha.Repository.Document ({ 100 id: data[i].u, 101 name: data[i].d, 102 repositoryId: that.repositoryId, 103 type: 'website', 104 url: data[i].u 105 })); 106 } 107 } 108 callback.call( that, items); 109 } 110 }); 111 }; 112 </code></pre> 113 * 114 * @param {object} params object with properties 115 * <div class="mdetail-params"><ul> 116 * <li><code> queryString</code> : String <div class="sub-desc">The query string for full text search</div></li> 117 * <li><code> objectTypeFilter</code> : array (optional) <div class="sub-desc">Object types that will be returned.</div></li> 118 * <li><code> filter</code> : array (optional) <div class="sub-desc">Attributes that will be returned.</div></li> 119 * <li><code> inFolderId</code> : boolean (optional) <div class="sub-desc">This is indicates whether or not a candidate object is a child-object of the folder object identified by the given inFolderId (objectId).</div></li> 120 * <li><code> inTreeId</code> : boolean (optional) <div class="sub-desc">This indicates whether or not a candidate object is a descendant-object of the folder object identified by the given inTreeId (objectId).</div></li> 121 * <li><code> orderBy</code> : array (optional) <div class="sub-desc">ex. [{lastModificationDate:’DESC’, name:’ASC’}]</div></li> 122 * <li><code> maxItems</code> : Integer (optional) <div class="sub-desc">number items to return as result</div></li> 123 * <li><code> skipCount</code> : Integer (optional) <div class="sub-desc">This is tricky in a merged multi repository scenario</div></li> 124 * <li><code> renditionFilter</code> : array (optional) <div class="sub-desc">Instead of termlist an array of kind or mimetype is expected. If null or array.length == 0 all renditions are returned. See http://docs.oasis-open.org/cmis/CMIS/v1.0/cd04/cmis-spec-v1.0.html#_Ref237323310 for renditionFilter</div></li> 125 * </ul></div> 126 * @param {function} callback this method must be called with all result items</div></li> 127 */ 128 query: null, 129 /* 130 query: function( params, callback ) { 131 if (typeof callback === 'function') { 132 callback([]); 133 } 134 }, 135 */ 136 137 /** 138 * Returns all children of a given motherId. 139 * 140 * @param {object} params object with properties 141 * <div class="mdetail-params"><ul> 142 * <li><code> objectTypeFilter</code> : array (optional) <div class="sub-desc">Object types that will be returned.</div></li> 143 * <li><code> filter</code> : array (optional) <div class="sub-desc">Attributes that will be returned.</div></li> 144 * <li><code> inFolderId</code> : boolean (optional) <div class="sub-desc">This indicates whether or not a candidate object is a child-object of the folder object identified by the given inFolderId (objectId).</div></li> 145 * <li><code> orderBy</code> : array (optional) <div class="sub-desc">ex. [{lastModificationDate:’DESC’, name:’ASC’}]</div></li> 146 * <li><code> maxItems</code> : Integer (optional) <div class="sub-desc">number items to return as result</div></li> 147 * <li><code> skipCount</code> : Integer (optional) <div class="sub-desc">This is tricky in a merged multi repository scenario</div></li> 148 * <li><code> renditionFilter</code> : array (optional) <div class="sub-desc">Instead of termlist an array of kind or mimetype is expected. If null or array.length == 0 all renditions are returned. See http://docs.oasis-open.org/cmis/CMIS/v1.0/cd04/cmis-spec-v1.0.html#_Ref237323310 for renditionFilter</div></li> 149 * </ul></div> 150 * @param {function} callback this method must be called with all result items 151 */ 152 getChildren: null, 153 /* 154 getChildren: function( params, callback ) { 155 if (typeof callback === 'function') { 156 callback([]); 157 } 158 }, 159 */ 160 161 /** 162 * Make the given jQuery object (representing an object marked as object of this type) 163 * clean. All attributes needed for handling should be removed. 164 * 165 <pre><code> 166 Aloha.Repositories.myRepository.makeClean = function (obj) { 167 obj.removeAttr('data-myRepository-name'); 168 }; 169 </code></pre> 170 * @param {jQuery} obj jQuery object to make clean 171 * @return void 172 */ 173 makeClean: function (obj) {}, 174 175 /** 176 * This method will be called when a user chooses an item from a repository and wants 177 * to insert this item in his content. 178 * Mark or modify an object as needed by that repository for handling, processing or identification. 179 * Objects can be any DOM object as A, SPAN, ABBR, etc. or 180 * special objects such as aloha-aloha_block elements. 181 * (see http://dev.w3.org/html5/spec/elements.html#embedding-custom-non-visible-data) 182 * 183 <pre><code> 184 Aloha.Repositories.myRepository.markObject = function (obj, resourceItem) { 185 186 obj.attr('data-myRepository-name').text(resourceItem.name); 187 }; 188 </code></pre> 189 * 190 * 191 * @param obj jQuery target object to which the repositoryItem will be applied 192 * @param repositoryItem The selected item. A class constructed from Document or Folder. 193 * @return void 194 */ 195 markObject: function (obj, repositoryItem) {}, 196 197 /** 198 * Set a template for rendering objects of this repository 199 * @param {String} template 200 * @return void 201 * @method 202 */ 203 setTemplate: function (template) { 204 if (template) { 205 this.template = template; 206 } else { 207 this.template = null; 208 } 209 }, 210 211 /** 212 * Checks whether the repository has a template 213 * @return {boolean} true when the repository has a template, false if not 214 * @method 215 */ 216 hasTemplate: function () { 217 return this.template ? true : false; 218 }, 219 220 /** 221 * Get the parsed template 222 * @return {Object} parsed template 223 * @method 224 */ 225 getTemplate: function () { 226 return this.template; 227 }, 228 229 /** 230 * Get the repositoryItem with given id 231 * @param itemId {String} id of the repository item to fetch 232 * @param callback {function} callback function 233 * @return {Aloha.Repository.Object} item with given id 234 */ 235 getObjectById: function ( itemId, callback ) { return true; } 236 }); 237 238 239 // expose the AbstractRepository 240 Aloha.AbstractRepository = AbstractRepository; 241 242 return AbstractRepository; 243 }); 244