browser/metro/base/content/bookmarks.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/metro/base/content/bookmarks.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +'use strict';
     1.9 +
    1.10 +/**
    1.11 + * Utility singleton for manipulating bookmarks.
    1.12 + */
    1.13 +var Bookmarks = {
    1.14 +  get metroRoot() {
    1.15 +    return PlacesUtils.annotations.getItemsWithAnnotation('metro/bookmarksRoot', {})[0];
    1.16 +  },
    1.17 +
    1.18 +  logging: false,
    1.19 +  log: function(msg) {
    1.20 +    if (this.logging) {
    1.21 +      Services.console.logStringMessage(msg);
    1.22 +    }
    1.23 +  },
    1.24 +
    1.25 +  addForURI: function bh_addForURI(aURI, aTitle, callback) {
    1.26 +    this.isURIBookmarked(aURI, function (isBookmarked) {
    1.27 +      if (isBookmarked)
    1.28 +        return;
    1.29 +
    1.30 +      let bookmarkTitle = aTitle || aURI.spec;
    1.31 +      let bookmarkService = PlacesUtils.bookmarks;
    1.32 +      let bookmarkId = bookmarkService.insertBookmark(Bookmarks.metroRoot,
    1.33 +                                                      aURI,
    1.34 +                                                      bookmarkService.DEFAULT_INDEX,
    1.35 +                                                      bookmarkTitle);
    1.36 +
    1.37 +      // XXX Used for browser-chrome tests
    1.38 +      let event = document.createEvent("Events");
    1.39 +      event.initEvent("BookmarkCreated", true, false);
    1.40 +      window.dispatchEvent(event);
    1.41 +
    1.42 +      if (callback)
    1.43 +        callback(bookmarkId);
    1.44 +    });
    1.45 +  },
    1.46 +
    1.47 +  _isMetroBookmark: function(aItemId) {
    1.48 +    return PlacesUtils.bookmarks.getFolderIdForItem(aItemId) == Bookmarks.metroRoot;
    1.49 +  },
    1.50 +
    1.51 +  isURIBookmarked: function bh_isURIBookmarked(aURI, callback) {
    1.52 +    if (!callback)
    1.53 +      return;
    1.54 +    PlacesUtils.asyncGetBookmarkIds(aURI, aItemIds => {
    1.55 +      callback(aItemIds && aItemIds.length > 0 && aItemIds.some(this._isMetroBookmark));
    1.56 +    });
    1.57 +  },
    1.58 +
    1.59 +  removeForURI: function bh_removeForURI(aURI, callback) {
    1.60 +    // XXX blargle xpconnect! might not matter, but a method on
    1.61 +    // nsINavBookmarksService that takes an array of items to
    1.62 +    // delete would be faster. better yet, a method that takes a URI!
    1.63 +    PlacesUtils.asyncGetBookmarkIds(aURI, (aItemIds) => {
    1.64 +      aItemIds.forEach((aItemId) => {
    1.65 +        if (this._isMetroBookmark(aItemId)) {
    1.66 +          PlacesUtils.bookmarks.removeItem(aItemId);
    1.67 +        }
    1.68 +      });
    1.69 +
    1.70 +      if (callback)
    1.71 +        callback();
    1.72 +
    1.73 +      // XXX Used for browser-chrome tests
    1.74 +      let event = document.createEvent("Events");
    1.75 +      event.initEvent("BookmarkRemoved", true, false);
    1.76 +      window.dispatchEvent(event);
    1.77 +    });
    1.78 +  }
    1.79 +};

mercurial