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