src/cm/media/js/client/c_util.js
changeset 341 053551f213fb
parent 0 40c8f766c9b8
equal deleted inserted replaced
340:9e2b9e568e42 341:053551f213fb
     1 _changeIds = function(elt, suffix) {
     1 _changeIds = function(elt, suffix) {
     2 	if (elt.id)
     2   if (elt.id)
     3 		elt.id =  elt.id + suffix ;
     3     elt.id =  elt.id + suffix ;
     4 	var c = elt.firstChild ;
     4   var c = elt.firstChild ;
     5 	while (c != null) {
     5   while (c != null) {
     6 		_changeIds(c, suffix) ;
     6     _changeIds(c, suffix) ;
     7 		c = c.nextSibling ; 
     7     c = c.nextSibling ; 
     8 	}
     8   }
     9 } ;
     9 } ;
    10 
    10 
    11 suffix = 0 ;
    11 suffix = 0 ;
    12 domDuplicate = function(elt) {
    12 domDuplicate = function(elt) {
    13 	var newElt = elt.cloneNode(true) ;
    13   var newElt = elt.cloneNode(true) ;
    14 	suffix++ ;
    14   suffix++ ;
    15 	_changeIds(newElt, '-'+suffix) ;
    15   _changeIds(newElt, '-'+suffix) ;
    16 	return newElt ;
    16   return newElt ;
    17 } ;
    17 } ;
    18 
    18 
    19 getDuplicated = function(elt) {
    19 getDuplicated = function(elt) {
    20 	return document.getElementById(elt.id + '-' + suffix) ;
    20   return document.getElementById(elt.id + '-' + suffix) ;
    21 } ;
    21 } ;
    22 
    22 
    23 logSel = function(selection) {
    23 logSel = function(selection) {
    24 	log('text :' + selection['text'] + ', start id : ' + selection['start']['elt'].id + ' , start offset : ' + selection['start']['offset'] + ' , end id : ' + selection['end']['elt'].id + 'end offset : ' + selection['end']['offset']) ;
    24   log('text :' + selection['text'] + ', start id : ' + selection['start']['elt'].id + ' , start offset : ' + selection['start']['offset'] + ' , end id : ' + selection['end']['elt'].id + 'end offset : ' + selection['end']['offset']) ;
    25 } ;
    25 } ;
    26 
    26 
    27 log = function (msg) {
    27 log = function (msg) {
    28 	var log = document.getElementById("log") ;
    28   var log = document.getElementById("log") ;
    29 	log.innerHTML = log.innerHTML + "<li>" + msg + "</li>" ; 
    29   log.innerHTML = log.innerHTML + "<li>" + msg + "</li>" ; 
    30 } ;
    30 } ;
    31 
    31 
    32 // from ext
    32 // from ext
    33 urlEncode = function(o){
    33 urlEncode = function(o){
    34     if(!o){
    34     if(!o){
    90     return obj;
    90     return obj;
    91 };
    91 };
    92 
    92 
    93 
    93 
    94 areSortedArraysEqual = function (a1, a2) {
    94 areSortedArraysEqual = function (a1, a2) {
    95 	if (a1.length != a2.length) 
    95   if (a1.length != a2.length) 
    96 		return false ;
    96     return false ;
    97 	for (var i = 0, ilen = a1.length ; i < ilen ; i++) {
    97   for (var i = 0, ilen = a1.length ; i < ilen ; i++) {
    98 		if (a1[i] != a2[i])
    98     if (a1[i] != a2[i])
    99 			return false ;
    99       return false ;
   100 	}
   100   }
   101 	return true
   101   return true
   102 } ;
   102 } ;
   103 
   103 
   104 // in place ordering!
   104 // in place ordering!
   105 quicksort = function (vec) {
   105 quicksort = function (vec) {
   106 	_quicksort(vec, 0, vec.length-1) ;
   106   _quicksort(vec, 0, vec.length-1) ;
   107 };
   107 };
   108 
   108 
   109 // cf. http://www.4guysfromrolla.com/webtech/012799-1.shtml
   109 // cf. http://www.4guysfromrolla.com/webtech/012799-1.shtml
   110 _quicksort = function (vec, loBound, hiBound)
   110 _quicksort = function (vec, loBound, hiBound)
   111 /**************************************************************
   111 /**************************************************************
   112 	This function adapted from the algorithm given in:
   112   This function adapted from the algorithm given in:
   113 		Data Abstractions & Structures Using C++, by
   113     Data Abstractions & Structures Using C++, by
   114 		Mark Headington and David Riley, pg. 586.
   114     Mark Headington and David Riley, pg. 586.
   115 
   115 
   116 	quicksort is the fastest array sorting routine for
   116   quicksort is the fastest array sorting routine for
   117 	unordered arrays.  Its big O is n log n.
   117   unordered arrays.  Its big O is n log n.
   118  **************************************************************/
   118  **************************************************************/
   119 {
   119 {
   120 
   120 
   121 	var pivot, loSwap, hiSwap, temp;
   121   var pivot, loSwap, hiSwap, temp;
   122 
   122 
   123 	// Two items to sort
   123   // Two items to sort
   124 	if (hiBound - loBound == 1)
   124   if (hiBound - loBound == 1)
   125 	{
   125   {
   126 		if (vec[loBound] > vec[hiBound])
   126     if (vec[loBound] > vec[hiBound])
   127 		{
   127     {
   128 			temp = vec[loBound];
   128       temp = vec[loBound];
   129 			vec[loBound] = vec[hiBound];
   129       vec[loBound] = vec[hiBound];
   130 			vec[hiBound] = temp;
   130       vec[hiBound] = temp;
   131 		}
   131     }
   132 		return;
   132     return;
   133 	}
   133   }
   134 
   134 
   135 	// Three or more items to sort
   135   // Three or more items to sort
   136 	pivot = vec[parseInt((loBound + hiBound) / 2)];
   136   pivot = vec[parseInt((loBound + hiBound) / 2)];
   137 	vec[parseInt((loBound + hiBound) / 2)] = vec[loBound];
   137   vec[parseInt((loBound + hiBound) / 2)] = vec[loBound];
   138 	vec[loBound] = pivot;
   138   vec[loBound] = pivot;
   139 	loSwap = loBound + 1;
   139   loSwap = loBound + 1;
   140 	hiSwap = hiBound;
   140   hiSwap = hiBound;
   141 
   141 
   142 	do {
   142   do {
   143 		// Find the right loSwap
   143     // Find the right loSwap
   144 		while (loSwap <= hiSwap && vec[loSwap] <= pivot)
   144     while (loSwap <= hiSwap && vec[loSwap] <= pivot)
   145 			loSwap++;
   145       loSwap++;
   146 
   146 
   147 		// Find the right hiSwap
   147     // Find the right hiSwap
   148 		while (vec[hiSwap] > pivot)
   148     while (vec[hiSwap] > pivot)
   149 			hiSwap--;
   149       hiSwap--;
   150 
   150 
   151 		// Swap values if loSwap is less than hiSwap
   151     // Swap values if loSwap is less than hiSwap
   152 		if (loSwap < hiSwap)
   152     if (loSwap < hiSwap)
   153 		{
   153     {
   154 			temp = vec[loSwap];
   154       temp = vec[loSwap];
   155 			vec[loSwap] = vec[hiSwap];
   155       vec[loSwap] = vec[hiSwap];
   156 			vec[hiSwap] = temp;
   156       vec[hiSwap] = temp;
   157 		}
   157     }
   158 	} while (loSwap < hiSwap);
   158   } while (loSwap < hiSwap);
   159 
   159 
   160 	vec[loBound] = vec[hiSwap];
   160   vec[loBound] = vec[hiSwap];
   161 	vec[hiSwap] = pivot;
   161   vec[hiSwap] = pivot;
   162 
   162 
   163 
   163 
   164 	// Recursively call function...  the beauty of quicksort
   164   // Recursively call function...  the beauty of quicksort
   165 
   165 
   166 	// 2 or more items in first section		
   166   // 2 or more items in first section   
   167 	if (loBound < hiSwap - 1)
   167   if (loBound < hiSwap - 1)
   168 		_quicksort(vec, loBound, hiSwap - 1);
   168     _quicksort(vec, loBound, hiSwap - 1);
   169 
   169 
   170 
   170 
   171 	// 2 or more items in second section
   171   // 2 or more items in second section
   172 	if (hiSwap + 1 < hiBound)
   172   if (hiSwap + 1 < hiBound)
   173 		_quicksort(vec, hiSwap + 1, hiBound);
   173     _quicksort(vec, hiSwap + 1, hiBound);
   174 } ;
   174 } ;