123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 'use strict';
- module.metadata = {
- "stability": "experimental",
- "engines": {
- "Firefox": "*"
- }
- };
- const { Cc, Ci } = require('chrome');
- const { Class } = require('../core/heritage');
- const { method } = require('../lang/functional');
- const { defer, promised, all } = require('../core/promise');
- const { send } = require('../addon/events');
- const { EventTarget } = require('../event/target');
- const { merge } = require('../util/object');
- const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
- getService(Ci.nsINavBookmarksService);
- let TreeNode = Class({
- initialize: function (value) {
- this.value = value;
- this.children = [];
- },
- add: function (values) {
- [].concat(values).forEach(value => {
- this.children.push(value instanceof TreeNode ? value : TreeNode(value));
- });
- },
- get length () {
- let count = 0;
- this.walk(() => count++);
-
- return --count;
- },
- get: method(get),
- walk: method(walk),
- toString: function () '[object TreeNode]'
- });
- exports.TreeNode = TreeNode;
- /*
- * Descends down from `node` applying `fn` to each in order.
- * Can be asynchronous if `fn` returns a promise. `fn` is passed
- * one argument, the current node, `curr`
- */
- function walk (curr, fn) {
- return promised(fn)(curr).then(val => {
- return all(curr.children.map(child => walk(child, fn)));
- });
- }
- function get (node, value) {
- if (node.value === value) return node;
- for (let child of node.children) {
- let found = get(child, value);
- if (found) return found;
- }
- return null;
- }
- /*
- * Constructs a tree of bookmark nodes
- * returning the root (value: null);
- */
- function constructTree (items) {
- let root = TreeNode(null);
- items.forEach(treeify.bind(null, root));
- function treeify (root, item) {
-
- let node = root.get(item);
- if (node) return node;
- node = TreeNode(item);
- let parentNode = item.group ? treeify(root, item.group) : root;
- parentNode.add(node);
- return node;
- }
- return root;
- }
- exports.constructTree = constructTree;
- function fetchItem (item)
- send('sdk-places-bookmarks-get', { id: item.id || item })
- exports.fetchItem = fetchItem;
- /*
- * Takes an ID or an object with ID and checks it against
- * the root bookmark folders
- */
- function isRootGroup (id) {
- id = id && id.id;
- return ~[bmsrv.bookmarksMenuFolder, bmsrv.toolbarFolder,
- bmsrv.unfiledBookmarksFolder
- ].indexOf(id);
- }
- exports.isRootGroup = isRootGroup;
- function urlQueryParser (query, url) {
- if (!url) return;
- if (/^https?:\/\//.test(url)) {
- query.uri = url.charAt(url.length - 1) === '/' ? url : url + '/';
- if (/\*$/.test(url)) {
- query.uri = url.replace(/\*$/, '');
- query.uriIsPrefix = true;
- }
- } else {
- if (/^\*/.test(url)) {
- query.domain = url.replace(/^\*\./, '');
- query.domainIsHost = false;
- } else {
- query.domain = url;
- query.domainIsHost = true;
- }
- }
- }
- exports.urlQueryParser = urlQueryParser;
- function promisedEmitter (emitter) {
- let { promise, resolve, reject } = defer();
- let errors = [];
- emitter.on('error', error => errors.push(error));
- emitter.on('end', (items) => {
- if (errors.length) reject(errors[0]);
- else resolve(items);
- });
- return promise;
- }
- exports.promisedEmitter = promisedEmitter;
- function createQuery (type, query) {
- query = query || {};
- let qObj = {
- searchTerms: query.query
- };
-
- urlQueryParser(qObj, query.url);
-
-
- if (type === 0) {
-
- qObj.beginTime = (query.from || 0) * 1000;
- qObj.endTime = (query.to || new Date()) * 1000;
-
- qObj.beginTimeReference = 0;
- qObj.endTimeReference = 0;
- }
-
- else if (type === 1) {
- qObj.tags = query.tags;
- qObj.folder = query.group && query.group.id;
- }
-
- else if (type === 2) {
- }
- return qObj;
- }
- exports.createQuery = createQuery;
- const SORT_MAP = {
- title: 1,
- date: 3,
- url: 5,
- visitCount: 7,
-
-
- dateAdded: 11,
- lastModified: 13
- };
- function createQueryOptions (type, options) {
- options = options || {};
- let oObj = {};
- oObj.sortingMode = SORT_MAP[options.sort] || 0;
- if (options.descending && options.sort)
- oObj.sortingMode++;
-
- if (type === 0 &&
- (options.sort === 'dateAdded' || options.sort === 'lastModified'))
- oObj.sortingMode = 0;
- oObj.maxResults = typeof options.count === 'number' ? options.count : 0;
- oObj.queryType = type;
- return oObj;
- }
- exports.createQueryOptions = createQueryOptions;
- function mapBookmarkItemType (type) {
- if (typeof type === 'number') {
- if (bmsrv.TYPE_BOOKMARK === type) return 'bookmark';
- if (bmsrv.TYPE_FOLDER === type) return 'group';
- if (bmsrv.TYPE_SEPARATOR === type) return 'separator';
- } else {
- if ('bookmark' === type) return bmsrv.TYPE_BOOKMARK;
- if ('group' === type) return bmsrv.TYPE_FOLDER;
- if ('separator' === type) return bmsrv.TYPE_SEPARATOR;
- }
- }
- exports.mapBookmarkItemType = mapBookmarkItemType;
|