browser/metro/base/content/bookmarks.js

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 'use strict';
     7 /**
     8  * Utility singleton for manipulating bookmarks.
     9  */
    10 var Bookmarks = {
    11   get metroRoot() {
    12     return PlacesUtils.annotations.getItemsWithAnnotation('metro/bookmarksRoot', {})[0];
    13   },
    15   logging: false,
    16   log: function(msg) {
    17     if (this.logging) {
    18       Services.console.logStringMessage(msg);
    19     }
    20   },
    22   addForURI: function bh_addForURI(aURI, aTitle, callback) {
    23     this.isURIBookmarked(aURI, function (isBookmarked) {
    24       if (isBookmarked)
    25         return;
    27       let bookmarkTitle = aTitle || aURI.spec;
    28       let bookmarkService = PlacesUtils.bookmarks;
    29       let bookmarkId = bookmarkService.insertBookmark(Bookmarks.metroRoot,
    30                                                       aURI,
    31                                                       bookmarkService.DEFAULT_INDEX,
    32                                                       bookmarkTitle);
    34       // XXX Used for browser-chrome tests
    35       let event = document.createEvent("Events");
    36       event.initEvent("BookmarkCreated", true, false);
    37       window.dispatchEvent(event);
    39       if (callback)
    40         callback(bookmarkId);
    41     });
    42   },
    44   _isMetroBookmark: function(aItemId) {
    45     return PlacesUtils.bookmarks.getFolderIdForItem(aItemId) == Bookmarks.metroRoot;
    46   },
    48   isURIBookmarked: function bh_isURIBookmarked(aURI, callback) {
    49     if (!callback)
    50       return;
    51     PlacesUtils.asyncGetBookmarkIds(aURI, aItemIds => {
    52       callback(aItemIds && aItemIds.length > 0 && aItemIds.some(this._isMetroBookmark));
    53     });
    54   },
    56   removeForURI: function bh_removeForURI(aURI, callback) {
    57     // XXX blargle xpconnect! might not matter, but a method on
    58     // nsINavBookmarksService that takes an array of items to
    59     // delete would be faster. better yet, a method that takes a URI!
    60     PlacesUtils.asyncGetBookmarkIds(aURI, (aItemIds) => {
    61       aItemIds.forEach((aItemId) => {
    62         if (this._isMetroBookmark(aItemId)) {
    63           PlacesUtils.bookmarks.removeItem(aItemId);
    64         }
    65       });
    67       if (callback)
    68         callback();
    70       // XXX Used for browser-chrome tests
    71       let event = document.createEvent("Events");
    72       event.initEvent("BookmarkRemoved", true, false);
    73       window.dispatchEvent(event);
    74     });
    75   }
    76 };

mercurial