PageSourceSearch

https://ag.zeromobile.co.jp/js/TreeModel.js?4

js zeromobile.co.jp collected 2026-10-05 01:05:47 UTC 11,351 bytes, 371 lines download raw bytes

1(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.TreeModel = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
2var mergeSort, findInsertIndex;
3mergeSort = require('mergesort');
4findInsertIndex = require('find-insert-index');
5
6module.exports = (function () {
7  'use strict';
8
9  var walkStrategies;
10
11  walkStrategies = {};
12
13  function k(result) {
14    return function () {
15      return result;
16    };
17  }
18
19  function TreeModel(config) {
20    config = config || {};
21    this.config = config;
22    this.config.childrenPropertyName = config.childrenPropertyName || 'children';
23    this.config.modelComparatorFn = config.modelComparatorFn;
24  }
25
26  function addChildToNode(node, child) {
27    child.parent = node;
28    node.children.push(child);
29    return child;
30  }
31
32  function Node(config, model) {
33    this.config = config;
34    this.model = model;
35    this.children = [];
36  }
37
38  TreeModel.prototype.parse = function (model) {
39    var i, childCount, node;
40
41    if (!(model instanceof Object)) {
42      throw new TypeError('Model must be of type object.');
43    }
44
45    node = new Node(this.config, model);
46    if (model[this.config.childrenPropertyName] instanceof Array) {
47      if (this.config.modelComparatorFn) {
48        model[this.config.childrenPropertyName] = mergeSort(
49          this.config.modelComparatorFn,
50          model[this.config.childrenPropertyName]);
51      }
52      for (i = 0, childCount = model[this.config.childrenPropertyName].length; i < childCount; i++) {
53        addChildToNode(node, this.parse(model[this.config.childrenPropertyName][i]));
54      }
55    }
56    return node;
57  };
58
59  function hasComparatorFunction(node) {
60    return typeof node.config.modelComparatorFn === 'function';
61  }
62
63  Node.prototype.isRoot = function () {
64    return this.parent === undefined;
65  };
66
67  Node.prototype.hasChildren = function () {
68    return this.children.length > 0;
69  };
70
71  function addChild(self, child, insertIndex) {
72    var index;
73
74    if (!(child instanceof Node)) {
75      throw new TypeError('Child must be of type Node.');
76    }
77
78    child.parent = self;
79    if (!(self.model[self.config.childrenPropertyName] instanceof Array)) {
80      self.model[self.config.childrenPropertyName] = [];
81    }
82
83    if (hasComparatorFunction(self)) {
84      // Find the index to insert the child
85      index = findInsertIndex(
86        self.config.modelComparatorFn,
87        self.model[self.config.childrenPropertyName],
88        child.model);
89
90      // Add to the model children
91      self.model[self.config.childrenPropertyName].splice(index, 0, child.model);
92
93      // Add to the node children
94      self.children.splice(index, 0, child);
95    } else {
96      if (insertIndex === undefined) {
97        self.model[self.config.childrenPropertyName].push(child.model);
98        self.children.push(child);
99      } else {
100        if (insertIndex < 0 || insertIndex > self.children.length) {
101          throw new Error('Invalid index.');
102        }
103        self.model[self.config.childrenPropertyName].splice(insertIndex, 0, child.model);
104        self.children.splice(insertIndex, 0, child);
105      }
106    }
107    return child;
108  }
109
110  Node.prototype.addChild = function (child) {
111    return addChild(this, child);
112  };
113
114  Node.prototype.addChildAtIndex = function (child, index) {
115    if (hasComparatorFunction(this)) {
116      throw new Error('Cannot add child at index when using a comparator function.');
117    }
118
119    return addChild(this, child, index);
120  };
121
122  Node.prototype.setIndex = function (index) {
123    if (hasComparatorFunction(this)) {
124      throw new Error('Cannot set node index when using a comparator function.');
125    }
126
127    if (this.isRoot()) {
128      if (index === 0) {
129        return this;
130      }
131      throw new Error('Invalid index.');
132    }
133
134    if (index < 0 || index >= this.parent.children.length) {
135      throw new Error('Invalid index.');
136    }
137
138    var oldIndex = this.parent.children.indexOf(this);
139
140    this.parent.children.splice(index, 0, this.parent.children.splice(oldIndex, 1)[0]);
141
142    this.parent.model[this.parent.config.childrenPropertyName]
143      .splice(index, 0, this.parent.model[this.parent.config.childrenPropertyName].splice(oldIndex, 1)[0]);
144
145    return this;
146  };
147
148  Node.prototype.getPath = function () {
149    var path = [];
150    (function addToPath(node) {
151      path.unshift(node);
152      if (!node.isRoot()) {
153        addToPath(node.parent);
154      }
155    })(this);
156    return path;
157  };
158
159  Node.prototype.getIndex = function () {
160    if (this.isRoot()) {
161      return 0;
162    }
163    return this.parent.children.indexOf(this);
164  };
165
166  /**
167   * Parse the arguments of traversal functions. These functions can take one optional
168   * first argument which is an options object. If present, this object will be stored
169   * in args.options. The only mandatory argument is the callback function which can
170   * appear in the first or second position (if an options object is given). This
171   * function will be saved to args.fn. The last optional argument is the context on
172   * which the callback function will be called. It will be available in args.ctx.
173   *
174   * @returns Parsed arguments.
175   */
176  function parseArgs() {
177    var args = {};
178    if (arguments.length === 1) {
179      if (typeof arguments[0] === 'function') {
180        args.fn = arguments[0];
181      } else {
182        args.options = arguments[0];
183      }
184    } else if (arguments.length === 2) {
185      if (typeof arguments[0] === 'function') {
186        args.fn = arguments[0];
187        args.ctx = arguments[1];
188      } else {
189        args.options = arguments[0];
190        args.fn = arguments[1];
191      }
192    } else {
193      args.options = arguments[0];
194      args.fn = arguments[1];
195      args.ctx = arguments[2];
196    }
197    args.options = args.options || {};
198    if (!args.options.strategy) {
199      args.options.strategy = 'pre';
200    }
201    if (!walkStrategies[args.options.strategy]) {
202      throw new Error('Unknown tree walk strategy. Valid strategies are \'pre\' [default], \'post\' and \'breadth\'.');
203    }
204    return args;
205  }
206
207  Node.prototype.walk = function () {
208    var args;
209    args = parseArgs.apply(this, arguments);
210    walkStrategies[args.options.strategy].call(this, args.fn, args.ctx);
211  };
212
213  walkStrategies.pre = function depthFirstPreOrder(callback, context) {
214    var i, childCount, keepGoing;
215    keepGoing = callback.call(context, this);
216    for (i = 0, childCount = this.children.length; i < childCount; i++) {
217      if (keepGoing === false) {
218        return false;
219      }
220      keepGoing = depthFirstPreOrder.call(this.children[i], callback, context);
221    }
222    return keepGoing;
223  };
224
225  walkStrategies.post = function depthFirstPostOrder(callback, context) {
226    var i, childCount, keepGoing;
227    for (i = 0, childCount = this.children.length; i < childCount; i++) {
228      keepGoing = depthFirstPostOrder.call(this.children[i], callback, context);
229      if (keepGoing === false) {
230        return false;
231      }
232    }
233    keepGoing = callback.call(context, this);
234    return keepGoing;
235  };
236
237  walkStrategies.breadth = function breadthFirst(callback, context) {
238    var queue = [this];
239    (function processQueue() {
240      var i, childCount, node;
241      if (queue.length === 0) {
242        return;
243      }
244      node = queue.shift();
245      for (i = 0, childCount = node.children.length; i < childCount; i++) {
246        queue.push(node.children[i]);
247      }
248      if (callback.call(context, node) !== false) {
249        processQueue();
250      }
251    })();
252  };
253
254  Node.prototype.all = function () {
255    var args, all = [];
256    args = parseArgs.apply(this, arguments);
257    args.fn = args.fn || k(true);
258    walkStrategies[args.options.strategy].call(this, function (node) {
259      if (args.fn.call(args.ctx, node)) {
260        all.push(node);
261      }
262    }, args.ctx);
263    return all;
264  };
265
266  Node.prototype.first = function () {
267    var args, first;
268    args = parseArgs.apply(this, arguments);
269    args.fn = args.fn || k(true);
270    walkStrategies[args.options.strategy].call(this, function (node) {
271      if (args.fn.call(args.ctx, node)) {
272        first = node;
273        return false;
274      }
275    }, args.ctx);
276    return first;
277  };
278
279  Node.prototype.drop = function () {
280    var indexOfChild;
281    if (!this.isRoot()) {
282      indexOfChild = this.parent.children.indexOf(this);
283      this.parent.children.splice(indexOfChild, 1);
284      this.parent.model[this.config.childrenPropertyName].splice(indexOfChild, 1);
285      this.parent = undefined;
286      delete this.parent;
287    }
288    return this;
289  };
290
291  return TreeModel;
292})();
293
294},{"find-insert-index":2,"mergesort":3}],2:[function(require,module,exports){
295module.exports = (function () {
296  'use strict';
297
298  /**
299   * Find the index to insert an element in array keeping the sort order.
300   *
301   * @param {function} comparatorFn The comparator function which sorted the array.
302   * @param {array} arr The sorted array.
303   * @param {object} el The element to insert.
304   */
305  function findInsertIndex(comparatorFn, arr, el) {
306    var i, len;
307    for (i = 0, len = arr.length; i < len; i++) {
308      if (comparatorFn(arr[i], el) > 0) {
309        break;
310      }
311    }
312    return i;
313  }
314
315  return findInsertIndex;
316})();
317
318},{}],3:[function(require,module,exports){
319module.exports = (function () {
320  'use strict';
321
322  /**
323   * Sort an array using the merge sort algorithm.
324   *
325   * @param {function} comparatorFn The comparator function.
326   * @param {array} arr The array to sort.
327   * @returns {array} The sorted array.
328   */
329  function mergeSort(comparatorFn, arr) {
330    var len = arr.length, firstHalf, secondHalf;
331    if (len >= 2) {
332      firstHalf = arr.slice(0, len / 2);
333      secondHalf = arr.slice(len / 2, len);
334      return merge(comparatorFn, mergeSort(comparatorFn, firstHalf), mergeSort(comparatorFn, secondHalf));
335    } else {
336      return arr.slice();
337    }
338  }
339
340  /**
341   * The merge part of the merge sort algorithm.
342   *
343   * @param {function} comparatorFn The comparator function.
344   * @param {array} arr1 The first sorted array.
345   * @param {array} arr2 The second sorted array.
346   * @returns {array} The merged and sorted array.
347   */
348  function merge(comparatorFn, arr1, arr2) {
349    var result = [], left1 = arr1.length, left2 = arr2.length;
350    while (left1 > 0 && left2 > 0) {
351      if (comparatorFn(arr1[0], arr2[0]) <= 0) {
352        result.push(arr1.shift());
353        left1--;
354      } else {
355        result.push(arr2.shift());
356        left2--;
357      }
358    }
359    if (left1 > 0) {
360      result.push.apply(result, arr1);
361    } else {
362      result.push.apply(result, arr2);
363    }
364    return result;
365  }
366
367  return mergeSort;
368})();
369
370},{}]},{},[1])(1)
371});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.