ginebra2/chrome/bedrockchrome/bookmarkview.superpage/bookmarkviewmaemo.js
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 var __prevOpenedBookmarkControl = null;
       
     2 var __previousSortEle = null;
       
     3 var __bookmarkCount= 0;
       
     4 var __dragendFlag = false;
       
     5 var __dogearSelected = false;
       
     6 var __timerValueLeft = 0;
       
     7 var __timerId = "";
       
     8 var __longPressEvent = false;
       
     9 // Set to the LongPress object that is currently waiting for activation,
       
    10 // ie. it has gotten a mouse-down event and has its timer running.
       
    11 var __currentLongPress;
       
    12 var __cancelLinkOpening = false;
       
    13 
       
    14 
       
    15 function _displayError(text) {
       
    16     var el = document.getElementById("errorText");
       
    17     el.style.visibility = "visible";
       
    18     el.style.width = "100%";
       
    19     el.style.height = "100%";
       
    20     el.innerHTML = "<p>" + text;
       
    21 }
       
    22 
       
    23 function _clearError(text) {
       
    24     var el = document.getElementById("errorText");
       
    25     el.style.visibility = "hidden";
       
    26     el.style.width = "0";
       
    27     el.style.height = "0";
       
    28     el.innerHTML = "";
       
    29 }
       
    30 
       
    31 function _enableSorting(ele) {
       
    32     // This is messy, why do we even need to do this sort/no-sort stuff???
       
    33     $(ele).removeClass('no-sort');
       
    34     $(ele).addClass('sort');
       
    35 		
       
    36 	var evt = document.createEvent("MouseEvents");
       
    37 	evt.initMouseEvent("mousedown", true, true, window,
       
    38 	  0, 0, 0, gInitialX, gInitialY, false, false, false, false, 0, null);
       
    39 	var canceled = !ele.dispatchEvent(evt);
       
    40     
       
    41   	views.WebView.gesturesEnabled = false;
       
    42 
       
    43     $(ele).find(".dogEarBox").addClass('selected');
       
    44     $(ele).find(".dogEar").addClass('selected');
       
    45 
       
    46   __sortingEnabled = true;
       
    47   __sortEleOffsetTop = ele.offsetTop;
       
    48   __previousSortEle = ele;
       
    49     $(ele).toggleClass('highlight');
       
    50 
       
    51   }
       
    52 
       
    53 function _disableSorting(ele) {
       
    54 //    app.debug('disablesorting:'); printProp(ele);
       
    55 
       
    56     views.WebView.gesturesEnabled = true;
       
    57     
       
    58     $(ele).removeClass('sort');
       
    59     $(ele).addClass('no-sort');
       
    60     $(ele).find('.dogEarBox').removeClass('selected').addClass('closed');
       
    61     $(ele).find('.dogEar').removeClass('selected').addClass('closed');
       
    62 
       
    63     __sortingEnabled = false;
       
    64     __sortEleOffsetTop = 0;
       
    65     __previousSortEle = null;
       
    66 }
       
    67 
       
    68 function _dragInit()
       
    69 {
       
    70     $("#bookmarkListTree").sortable({
       
    71         placeholder: 'ui-state-highlight',
       
    72         axis:'y',
       
    73       cancel : 'li.no-sort',
       
    74       scrollSensitivity : 40,
       
    75       scrollSpeed : 40,
       
    76         start: function(event, ui) {
       
    77             __dragendFlag = true;
       
    78             },
       
    79       update: function(event, ui) {
       
    80                 var sortedBookmark = ui.item[0];
       
    81                 _disableSorting(sortedBookmark); // Unselect it
       
    82                 // If there's only one bookmark in the list, do nothing
       
    83                 if ($('#bookmarkListTree').children().length == 0)
       
    84                     return;
       
    85                 var nextBookmark = $(sortedBookmark).next()[0];
       
    86                 var bm = window.bookmarksController.findBookmark(sortedBookmark.id);
       
    87                 // If it wasn't moved to the end, change the sortIndex to the next bookmark's sortIndex and the rest will propagate down
       
    88                 if (nextBookmark) {
       
    89                     var nextbm = window.bookmarksController.findBookmark(nextBookmark.id);
       
    90                     window.bookmarksController.reorderBookmark(sortedBookmark.id, nextbm.sortIndex);
       
    91                 }
       
    92                 // It was moved to the end, so change the sortIndex to the prev bookmark's sortIndex + 1
       
    93                 else {
       
    94                     var prevBookmark = $(sortedBookmark).prev()[0];
       
    95                     var prevbm = window.bookmarksController.findBookmark(prevBookmark.id);
       
    96                     window.bookmarksController.reorderBookmark(sortedBookmark.id, prevbm.sortIndex+1);
       
    97                 }
       
    98             }
       
    99     });
       
   100 
       
   101     $("#bookmarkListTree").sortable('enable');
       
   102     $("#bookmarkListTree").disableSelection();
       
   103 }
       
   104 
       
   105 function _longPress(ele)
       
   106 {
       
   107    __longPressEvent = true;
       
   108    if(__timerId != "")
       
   109    {
       
   110        window.clearTimeout(__timerId);
       
   111        __timerId = "";
       
   112    }
       
   113   if (__sortingEnabled)
       
   114   {
       
   115             _disableSorting(__previousSortEle)
       
   116             return false;
       
   117   }
       
   118 
       
   119   if(__prevOpenedBookmarkControl)
       
   120   	_bookmarkHideControl(__prevOpenedBookmarkControl);
       
   121   	
       
   122     if(!__dogearSelected)
       
   123      _enableSorting(ele);
       
   124 }
       
   125 
       
   126 function _timeLeft()
       
   127 {
       
   128    __timerValueLeft = 1;
       
   129 }
       
   130 function _longPressStarted(lp)
       
   131 {
       
   132     __timerValueLeft = 0;
       
   133     __longPressEvent = false;
       
   134     if(!window.views.WebView.bedrockTiledBackingStoreEnabled())
       
   135     {
       
   136         __timerId = window.setTimeout("_timeLeft()",250);
       
   137     }
       
   138     else
       
   139     {
       
   140         __timerId = window.setTimeout("_timeLeft()",550);
       
   141     }
       
   142     // Remember the current LongPress object so we can cancel it if scrolling
       
   143     // starts.
       
   144     __currentLongPress = lp;
       
   145     __cancelLinkOpening = false;
       
   146 }
       
   147 
       
   148 function _handlePanStarted(type)
       
   149 {
       
   150    if(app.ui() != "maemo5_ui") // workaround for Maemo Issue
       
   151    {
       
   152 			__cancelLinkOpening = true;
       
   153 	 }
       
   154 		
       
   155     // Scrolling started, cancel the LongPress.
       
   156       if (__currentLongPress != undefined) {
       
   157         __currentLongPress.cancel();
       
   158         __currentLongPress = undefined;
       
   159     }
       
   160 }
       
   161 
       
   162 
       
   163 function _bookmarkHideControl(ele){
       
   164     $(ele).find('.dogEarBox').addClass('closed');
       
   165     $(ele).find('.dogEar').addClass('closed');
       
   166     $(ele).find('.bookmarkItem').css('width', (parseInt(window.innerWidth)-70)+"px");
       
   167 }
       
   168 
       
   169 function _bookmarkToggleControls(event){
       
   170     var ele = event.target;
       
   171     try {
       
   172         if(__timerId != "")
       
   173         {
       
   174         	window.clearTimeout(__timerId);
       
   175         	__timerId = "";
       
   176         }
       
   177         if (__sortingEnabled && ele.parentNode.parentNode!=__previousSortEle)
       
   178         {
       
   179             _disableSorting(__previousSortEle)
       
   180             return false;
       
   181         }
       
   182         else if (__sortingEnabled && (ele.parentNode.parentNode==__previousSortEle))
       
   183         {
       
   184             return false;
       
   185         }
       
   186         if(__timerValueLeft == 1 || __longPressEvent == true)
       
   187         {
       
   188             __timerValueLeft = 0;
       
   189             __longPressEvent = false;
       
   190             return false;
       
   191         }
       
   192         __timerValueLeft = 0;
       
   193 
       
   194         var li = $(ele).parents('li');
       
   195         var dogEarBox = li.find('.dogEarBox');
       
   196         dogEarBox.toggleClass('closed');
       
   197         dogEarBox.find('.dogEar').toggleClass('closed');
       
   198         // It's too bad we can't do this via margin-right because then we could just do it in the .css file coz it would be a static value
       
   199         if (dogEarBox.hasClass('closed')) {
       
   200             li.find('.bookmarkItem').css('width', (parseInt(window.innerWidth)-70)+"px");
       
   201         }
       
   202         else {
       
   203             li.find('.bookmarkItem').css('width', (parseInt(window.innerWidth)-220)+"px");
       
   204         }
       
   205         if (__prevOpenedBookmarkControl != null && __prevOpenedBookmarkControl != ele.parentNode.parentNode)
       
   206             _bookmarkHideControl(__prevOpenedBookmarkControl);
       
   207 
       
   208     }catch(e) { alert(e); }
       
   209 
       
   210     __prevOpenedBookmarkControl = ele.parentNode.parentNode;
       
   211 }
       
   212 
       
   213 function _addNewBookmark(bmtitle,bmurl,bmid)
       
   214 {
       
   215     // bookmark title/url may have been altered by the controller's add method (i.e. to include http://) so reload it
       
   216     var bm = window.bookmarksController.findBookmark(bmid);
       
   217     bmtitle = bm.title;
       
   218     bmurl = bm.url;
       
   219     if(__timerId != "")
       
   220     {
       
   221         window.clearTimeout(__timerId);
       
   222         __timerId = "";
       
   223     }
       
   224 	if(__prevOpenedBookmarkControl)
       
   225   		_bookmarkHideControl(__prevOpenedBookmarkControl);
       
   226 
       
   227     if(__previousSortEle    != null)
       
   228         _disableSorting(__previousSortEle)
       
   229 
       
   230     __bookmarkCount += 1;
       
   231     //create element and add it to bookmark view
       
   232     var ul=document.getElementById('bookmarkListTree');
       
   233     var dbgTitle = bmtitle.replace(/'/g, "&#39");
       
   234     dbgTitle = dbgTitle.replace(/"/g, "&#34");
       
   235     var li = _createBookmarkElement(dbgTitle,bmurl,bmid);
       
   236 	$(li).addClass('no-sort');
       
   237 	ul.appendChild(li);
       
   238     ul.childNodes[ul.childNodes.length-1].focus();
       
   239     
       
   240     if (!window.views.WebView.bedrockTiledBackingStoreEnabled())
       
   241         new LongPress(li.id, _longPress, _longPressStarted, 250);
       
   242     else
       
   243         new LongPress(li.id, _longPress, _longPressStarted, 550);
       
   244 
       
   245     $('html, body').animate({
       
   246                         scrollTop: 0}, 1000);
       
   247 }
       
   248 
       
   249 function _editBookmark(bmtitle,bmurl,bmid)
       
   250 {
       
   251     if(__prevOpenedBookmarkControl)
       
   252         _bookmarkHideControl(__prevOpenedBookmarkControl);
       
   253 
       
   254     // bookmark title/url may have been altered by the controller's edit method (i.e. to include http://) so reload it
       
   255     var bm = window.bookmarksController.findBookmark(bmid);
       
   256 
       
   257     $('#'+bmid).find('.aTitle').text(bm.title);
       
   258     $('#'+bmid).find('.aUrl').text(bm.url);
       
   259 }
       
   260 
       
   261 function _launchEditBookmark(r,bmtitle,bmurl,id)
       
   262 {
       
   263     window.bookmarksController.showBookmarkEditDialog(bmtitle,bmurl,id);
       
   264 }
       
   265 
       
   266 function _deleteBookmark(r,bmid)
       
   267 { 
       
   268   window.bookmarksController.deleteBookmark(bmid);
       
   269   $('#'+bmid).remove();
       
   270 }
       
   271 
       
   272 
       
   273 function _openUrl(ele, newUrl) {
       
   274     if(__timerId != "")
       
   275     {
       
   276        window.clearTimeout(__timerId);
       
   277         __timerId = "";
       
   278     }
       
   279     // DragStart & DragEnd listeners are defined at bottom
       
   280     if (__sortingEnabled && (ele.parentNode!=__previousSortEle))
       
   281     {
       
   282         _disableSorting(__previousSortEle)
       
   283         return false;
       
   284     }
       
   285     else if (__sortingEnabled && (ele.parentNode==__previousSortEle))
       
   286         {
       
   287             return false;
       
   288         }
       
   289     else if (__dragendFlag)
       
   290     {
       
   291         __dragendFlag = false;
       
   292         return false;
       
   293     }
       
   294     else if(__cancelLinkOpening)
       
   295     {
       
   296     	__cancelLinkOpening = false;
       
   297     	return false;
       
   298     }
       
   299 
       
   300     window.views.WebView.showNormalPage();
       
   301     window.ViewStack.switchView( "WebView","BookmarkTreeView");
       
   302 
       
   303     // Laod a page to chrome view
       
   304     window.views.WebView.loadUrlToCurrentPage(newUrl);
       
   305     views.WebView.gesturesEnabled = true;
       
   306 }
       
   307 
       
   308 function _updateBookmarkViewGoemetry(displayMode)
       
   309 {
       
   310     // It's too bad we couldn't use margin-right instead of width coz then we could just put a static value in the .css file and not even need to do anything
       
   311     $('.dogEarBox.closed').next('.bookmarkItem').css('width', (parseInt(window.innerWidth)-70)+"px");
       
   312     $('.dogEarBox').not('.closed').next('.bookmarkItem').css('width', (parseInt(window.innerWidth)-220)+"px");
       
   313 }
       
   314 
       
   315 function _setDogear()
       
   316 {
       
   317 	__dogearSelected = true;
       
   318 }
       
   319 
       
   320 function _unsetDogear()
       
   321 {
       
   322 	__dogearSelected = false;
       
   323 }
       
   324 
       
   325 function _createBookmarkElement(bmtitle,bmfullurl,idValue)
       
   326 {
       
   327         var dogEar;
       
   328         var _width = parseInt(window.innerWidth)-70;
       
   329         var escapedTitle = bmtitle.replace(/&#39/g, "\\'"); 
       
   330         var li=document.createElement('li');
       
   331         li.id = idValue;
       
   332         if (app.ui() == "maemo5_ui"){
       
   333         dogEar = 
       
   334         '<div class="dogEarBox closed">'+ // bookmarkBox
       
   335           '<div class="dogEar closed"></div>'+
       
   336           '<img class="bookmarkEditBtn" src="iconsMaemo/edit_btn.png" width="56" height="56" vspace="7" hspace="5">'+
       
   337           '<img class="bookmarkDeleteBtn" src="iconsMaemo/delete_btn.png" width="56" height="56" vspace="7" hspace="5" >'+
       
   338         '</div>';
       
   339         } else {
       
   340         dogEar = 
       
   341         '<div class="dogEarBox closed">'+ // bookmarkBox
       
   342           '<div class="dogEar closed"></div>'+
       
   343           '<img class="bookmarkEditBtn" src="icons/edit_btn.png" width="56" height="56" vspace="7" hspace="5">'+
       
   344           '<img class="bookmarkDeleteBtn" src="icons/delete_btn.png" width="56" height="56" vspace="7" hspace="5" >'+
       
   345         '</div>';
       
   346         }
       
   347         var bookmark = 
       
   348         '<div class="bookmarkItem" style="width:'+_width+'px;">'+
       
   349           '<span class="aTitle">'+bmtitle+'</span>'+
       
   350           '<br/>'+
       
   351           '<span class="aUrl">'+bmfullurl+'</span>'+
       
   352         '</div>';
       
   353         li.innerHTML = dogEar + bookmark;
       
   354         $(li).find(".dogEar")
       
   355                .click(_bookmarkToggleControls)
       
   356                .mouseover(_setDogear)
       
   357                .mouseout(_unsetDogear);
       
   358         $(li).find(".bookmarkItem").
       
   359                click(function (event) {_openUrl(event.target, bmfullurl);});
       
   360         $(li).find(".bookmarkEditBtn").
       
   361                click(function (event) {_launchEditBookmark(event.target, bmtitle, bmfullurl, idValue);});
       
   362         $(li).find(".bookmarkDeleteBtn").
       
   363                click(function (event) {_deleteBookmark(event.target, idValue);});
       
   364 
       
   365         return li;
       
   366 }
       
   367 
       
   368 function _createBookmarkView()
       
   369 {
       
   370         try
       
   371         {
       
   372             __currentLongPress = undefined;
       
   373             //Get bookmarks data from database
       
   374             window.bookmarksController.findAllBookmarks();
       
   375             var ul=document.getElementById('bookmarkListTree');
       
   376             while (window.bookmarksController.hasMoreBookmarks())
       
   377             {
       
   378                 var bm = window.bookmarksController.nextBookmark();
       
   379                 var bmurl = bm.url;
       
   380                 var bmtitle = bm.title;
       
   381                 var id = bm.id;
       
   382 
       
   383                 //create element and add it to bookmark view
       
   384                 var li = _createBookmarkElement(bmtitle,bmurl,id);
       
   385                 $(li).addClass('no-sort');
       
   386                 ul.appendChild(li);
       
   387                 ul.childNodes[0].focus();
       
   388                 if (!window.views.WebView.bedrockTiledBackingStoreEnabled())
       
   389                     new LongPress(li.id, _longPress, _longPressStarted, 250);
       
   390                 else
       
   391                     new LongPress(li.id, _longPress, _longPressStarted, 550);
       
   392                 		
       
   393                 __bookmarkCount++;
       
   394             }
       
   395         } catch(E) {
       
   396         	  _displayError("Bookmarks unavailable");  // Localize!
       
   397             app.debug("bookmarkView.js:_createBookmarkView: " + E); 
       
   398         }
       
   399 
       
   400         _dragInit();
       
   401 }
       
   402 
       
   403 function launchBookmarkView()
       
   404 {
       
   405     try{
       
   406         window.chrome.aspectChanged.connect(_updateBookmarkViewGoemetry);
       
   407         window.bookmarksController.bookmarkAdded.connect(_addNewBookmark);
       
   408         window.bookmarksController.bookmarkModified.connect(_editBookmark);
       
   409 
       
   410         // Get Bookmarks from the database
       
   411         _createBookmarkView();
       
   412 
       
   413         // Watch for the start of scrolling in the view so we can cancel the
       
   414         // long presses to prevent them from interfere with scrolling.  Otherwise
       
   415         // the long press callback will fire and trigger sortable mode.
       
   416         views.WebView.startingPanGesture.connect(_handlePanStarted);
       
   417 
       
   418     }catch(e){ alert(e); }
       
   419 }