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