1 /* copypaste.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) 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 /**
 28  * @overview:
 29  * Various utility functions that are useful when working with selections, and
 30  * ranges for copy/paste functionality.
 31  */
 32 define('aloha/copypaste', [
 33 	'jquery',
 34 	'aloha/core'
 35 ], function (
 36 	$,
 37 	Aloha
 38 ) {
 39 	'use strict';
 40 
 41 	/**
 42 	 * Retrieve the editable host in which the given range is contained.
 43 	 *
 44 	 * @param {WrappedRange} range
 45 	 * @return {jQuery.<HTMLElement>|null} The editable host element, null if
 46 	 *                                     non can be determinded from the given
 47 	 *                                     range.
 48 	 */
 49 	function getEditableAt(range) {
 50 		if (!range || !range.commonAncestorContainer) {
 51 			return null;
 52 		}
 53 		var $container = $(range.commonAncestorContainer);
 54 		return $container.length ? Aloha.getEditableHost($container) : null;
 55 	}
 56 
 57 	/**
 58 	 * Retrieves the current range.
 59 	 *
 60 	 * @return {WrappedRange|null} Range at current selection or null of non
 61 	 *                             exists.
 62 	 */
 63 	function getRange() {
 64 		var selection = Aloha.getSelection();
 65 		return selection.getRangeCount() ? selection.getRangeAt(0) : null;
 66 	}
 67 
 68 	/**
 69 	 * Set the selection to the given range
 70 	 *
 71 	 * @param {object} range An object that must container the following
 72 	 *                       essential range properties: ~ startContainer
 73 	 *                                                   ~ endContainer
 74 	 *                                                   ~ startOffset
 75 	 *                                                   ~ endOffset
 76 	 */
 77 	function setSelectionAt(range) {
 78 		var newRange = Aloha.createRange();
 79 		var selection = Aloha.getSelection();
 80 		newRange.setStart(range.startContainer, range.startOffset);
 81 		newRange.setEnd(range.endContainer, range.endOffset);
 82 		selection.removeAllRanges();
 83 		selection.addRange(newRange);
 84 	}
 85 
 86 	/**
 87 	 * Creates a selection that encompasses the contents of the given element.
 88 	 *
 89 	 * @param {HTMLElement} element Editable DOM element.
 90 	 */
 91 	function selectAllOf(element) {
 92 		setSelectionAt({
 93 			startContainer: element,
 94 			endContainer: element,
 95 			startOffset: 0,
 96 			endOffset: element.childNodes ? element.childNodes.length
 97 		                                  : element.length
 98 		});
 99 		$(element).focus();
100 	}
101 
102 	return {
103 		getEditableAt: getEditableAt,
104 		getRange: getRange,
105 		selectAllOf: selectAllOf,
106 		setSelectionAt: setSelectionAt
107 	};
108 });
109