[c_selection.js] If safari_mobile, get current selection from a previously created global variable
authorSimon Descarpentries <sid@sopinspace.com>
Wed, 30 Oct 2013 18:08:42 +0100
changeset 556 69503659fe8f
parent 554 8e116b8ddbc3
child 557 6005880ca8c7
[c_selection.js] If safari_mobile, get current selection from a previously created global variable [c_sync.js] ref where the safari_mobile global is used [c_text_view_comments.js] if safari_mobile update selection also on selectionChange event [text_view_comments.html] if safari_mobile store a clone of the current selection on each selectionChange set layout width to 99% to improve display factorize safari mobile detection code
src/cm/media/js/client/c_selection.js
src/cm/media/js/client/c_sync.js
src/cm/media/js/site/c_text_view_comments.js
src/cm/templates/site/text_view_comments.html
--- a/src/cm/media/js/client/c_selection.js	Mon Oct 21 16:50:41 2013 +0200
+++ b/src/cm/media/js/client/c_selection.js	Wed Oct 30 18:08:42 2013 +0100
@@ -26,11 +26,12 @@
 
 // when selection starts/ends in/on a non textual element (<hr/> for example) we very often have anchorNode/focusNode == body elt
 // TODO adapt this body case by considering offset ( cf. http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html)
+
 getSelectionInfo  = function () {
   var startNode = null, endNode = null, startOffset = 0, endOffset = 0, text = '' ;
   
   if (window.getSelection) { // everything else than IE
-    var userSelection = window.getSelection();
+    var userSelection = safari_mobile ? storedSelection : window.getSelection ();
 
     if (userSelection.rangeCount > 0) {
       var range = userSelection.getRangeAt(0) ;
@@ -166,7 +167,7 @@
       else 
         return null ;
     }
-    else 
+    else
       return null ;
     
   }
--- a/src/cm/media/js/client/c_sync.js	Mon Oct 21 16:50:41 2013 +0200
+++ b/src/cm/media/js/client/c_sync.js	Wed Oct 30 18:08:42 2013 +0100
@@ -17,7 +17,9 @@
 }
 
 // Are we on Safari mobile ?
-var safari_mobile = /iPhone|iPod|iPad/.test(navigator.userAgent);
+// made global to be used in templates/site/text_view_comments.html
+// and in media/js/site/c_text_view_comments.js
+safari_mobile = /iPhone|iPod|iPad/.test(navigator.userAgent);
 // If so, we must scroll the jQuery UI pane created for Safari mobile instead of the whole document
 var the_scrolling_part = safari_mobile ? '#maincontainer' : 'document' ;
 
--- a/src/cm/media/js/site/c_text_view_comments.js	Mon Oct 21 16:50:41 2013 +0200
+++ b/src/cm/media/js/site/c_text_view_comments.js	Wed Oct 30 18:08:42 2013 +0100
@@ -164,6 +164,10 @@
   }
 } ;
 
+// safari_mobile defined in media/js/client/c_sync.js
+if (safari_mobile)
+	onSelectionChange = onTextMouseUp;
+
 gLastScrollTime = null ;
 checkForAlignement = function () {
   var now = (new Date()).getTime() ;
--- a/src/cm/templates/site/text_view_comments.html	Mon Oct 21 16:50:41 2013 +0200
+++ b/src/cm/templates/site/text_view_comments.html	Wed Oct 30 18:08:42 2013 +0100
@@ -47,8 +47,9 @@
 <!--
 
 // Using jQuery UI-Layout to allow scrolling in Safari mobile
-if (/iPhone|iPod|iPad/.test(navigator.userAgent)) {
-	jQuery(document).ready (function () {
+jQuery(document).ready (function () {
+	// safari_mobile is defined in media/js/client/c_sync.js
+	if (safari_mobile) {
 		jQuery('body').layout ({
 			center: {
 			    paneSelector : '#maincontainer',
@@ -57,9 +58,44 @@
 		});
 		jQuery('#maincontainer').css ({
 			height: '100%',
+			width: '99%'
 		});
-	});
-}
+
+		/**
+		 * cloneSelection
+		 * s		: Selection object
+		 * return	: a deep copy of a given Selection, co-ment usefull functions are recreated
+		 */
+		function cloneSelection (s) {
+			var c = jQuery.extend (true, {}, s);
+			// var c = JSON.parse(JSON.stringify(s)); // alternative
+			c.ranges = [];
+
+			for (var j = 0; j < s.rangeCount; j++) {
+				c.ranges [j] = s.getRangeAt (j);
+			}
+
+			c.getRangeAt = function (j) { return this.ranges [j]; };
+			c.toString = function () { return this.ranges[0].toString (); };
+			return c;
+		}
+
+		var emptySelection = { rangeCount: 0 };
+		// intended global variable used in media/js/client/c_selection.js
+		storedSelection = emptySelection;
+
+		document.addEventListener("selectionchange", function() {
+			var c = cloneSelection(window.getSelection());
+			var c_txt = c.toString ();
+
+			if (c_txt && c_txt != '') {
+				storedSelection = c;
+			} else {
+				setTimeout (function () { storedSelection = emptySelection; }, 500);
+			}
+		}, false);
+	}
+});
 
 // GLOBALS from server
 sv_user_permissions = [];