PageSourceSearch

https://www.fancyhands.com/angular/services.js

js fancyhands.com collected 2026-10-02 02:25:04 UTC 8,558 bytes, 264 lines download raw bytes

1/////////////////////////////
2// Custom Services
3/////////////////////////////
4var servicesMod = angular.module('services', [])
5
6// AngularJS timeAgo
7.factory('nowTime', ['$timeout',
8    function($timeout) {
9        var nowTime = Date.now();
10        var updateTime = function() {
11            $timeout(function() {
12                nowTime = Date.now();
13                updateTime();
14            }, 1000);
15        };
16        updateTime();
17        return function() {
18            return nowTime;
19        };
20    }
21])
22
23.factory('timeAgo', function($timeout) {
24    var service = {
25        settings: {
26            refreshMillis: 60000,
27            allowFuture: false,
28            strings: {
29                prefixAgo: null,
30                prefixFromNow: null,
31                suffixAgo: "ago",
32                suffixFromNow: "from now",
33                seconds: "less than a minute",
34                minute: "a minute",
35                minutes: "%d minutes",
36                hour: "an hour",
37                hours: "%d hours",
38                day: "a day",
39                days: "%d days",
40                month: "a month",
41                months: "%d months",
42                year: "a year",
43                years: "%d years",
44                numbers: []
45            }
46        },
47        inWords: function(distanceMillis) {
48            var $l = service.settings.strings;
49            var prefix = $l.prefixAgo;
50            var suffix = $l.suffixAgo;
51            if (service.settings.allowFuture) {
52                if (distanceMillis < 0) {
53                    prefix = $l.prefixFromNow;
54                    suffix = $l.suffixFromNow;
55                }
56            }
57
58            var seconds = Math.abs(distanceMillis) / 1000;
59            var minutes = seconds / 60;
60            var hours = minutes / 60;
61            var days = hours / 24;
62            var years = days / 365;
63
64            function substitute(stringOrFunction, number) {
65                var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
66                var value = ($l.numbers && $l.numbers[number]) || number;
67                return string.replace(/%d/i, value);
68            }
69
70            var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
71                seconds < 90 && substitute($l.minute, 1) ||
72                minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
73                minutes < 90 && substitute($l.hour, 1) ||
74                hours < 24 && substitute($l.hours, Math.round(hours)) ||
75                hours < 42 && substitute($l.day, 1) ||
76                days < 30 && substitute($l.days, Math.round(days)) ||
77                days < 45 && substitute($l.month, 1) ||
78                days < 365 && substitute($l.months, Math.round(days / 30)) ||
79                years < 1.5 && substitute($l.year, 1) ||
80                substitute($l.years, Math.round(years));
81
82            var separator = $l.wordSeparator === undefined ? " " : $l.wordSeparator;
83            return $.trim([prefix, words, suffix].join(separator));
84        },
85        parse: function(iso8601) {
86            if (angular.isNumber(iso8601)) return parseInt(iso8601, 10);
87
88            var s = $.trim(iso8601);
89            s = s.replace(/\.\d+/, ""); // remove milliseconds
90            s = s.replace(/-/, "/").replace(/-/, "/");
91            s = s.replace(/T/, " ").replace(/Z/, " UTC");
92            s = s.replace(/([\+\-]\d\d)\:?(\d\d)/, " $1$2"); // -04:00 -> -0400
93
94            return new Date(s);
95        }
96    };
97    return service;
98})
99
100/* OAuth Service
101---------------------------------------------------------- */
102.factory('$OAuth', function($rootScope, $http) {
103
104    var request = function(app_context, url, method, params, callback) {
105
106        // SIGN IT!
107        if (typeof(params) != 'object') {
108            params = {
109                'z': true,
110                'b': true,
111                'c': true
112            };
113        }
114
115        for (var k in params) {
116            if (k.indexOf('oauth_') === 0) {
117                delete params[k];
118            }
119        }
120
121        params['oauth_consumer_key'] = app_context.consumer_key;
122        params['oauth_timestamp'] = Math.round(new Date().getTime() / 1000);
123        params['oauth_nonce'] = Math.random() * 10000;
124        params['oauth_signature_method'] = 'HMAC-SHA1';
125        params['oauth_signature_method'] = 'HMAC-SHA1';
126
127        _sig_base = sig_base(method, url, params);
128        var key = encodeURIComponent(app_context.secret) + "&";
129        _signature = CryptoJS.HmacSHA1(_sig_base, key).toString(CryptoJS.enc.Base64);
130
131        params['oauth_signature'] = _signature;
132
133        _oauth_results = {
134            'oauth_signature_base': _sig_base,
135            'oauth_signature': _signature
136        };
137
138        var vals = {
139            url: url,
140            method: method,
141            headers: {
142                'Content-Type': 'application/x-www-form-urlencoded'
143            }
144        };
145
146        // we need to set the params as data if it's post, otherwise params. THANKS OBAMA!
147        if (method === 'POST'|| method === 'PUT' || method === 'DELETE') {
148            vals['data'] = $.param(params);
149        } else {
150            vals['params'] = params;
151        }
152
153        $http(vals)
154            .success(function(data) {
155                if (typeof(callback) != 'undefined') {
156                    callback({'data':data, 'oauth_results': _oauth_results });
157                }
158            })
159            .error(function(data) {
160                if (typeof(callback) != 'undefined') {
161                    callback({'error':data, 'oauth_results': _oauth_results });
162                }
163            });
164    }
165
166    return {
167        request: request
168    }
169})
170
171/* Smooth Scroll
172---------------------------------------------------------- */
173.service('anchorSmoothScroll', function ($document, $window) {
174
175    var document = $document[0];
176    var window = $window;
177
178    function getCurrentPagePosition(window, document) {
179        // Firefox, Chrome, Opera, Safari
180        if (window.pageYOffset) return window.pageYOffset;
181        // Internet Explorer 6 - standards mode
182        if (document.documentElement && document.documentElement.scrollTop)
183            return document.documentElement.scrollTop;
184        // Internet Explorer 6, 7 and 8
185        if (document.body.scrollTop) return document.body.scrollTop;
186        return 0;
187    }
188
189    function getElementY(document, element) {
190        var y = element.offsetTop;
191        var node = element;
192        while (node.offsetParent && node.offsetParent != document.body) {
193            node = node.offsetParent;
194            y += node.offsetTop;
195        }
196        return y;
197    }
198
199    this.scrollDown = function (startY, stopY, speed, distance) {
200
201        var timer = 0;
202
203        var step = Math.round(distance / 25);
204        var leapY = startY + step;
205
206        for (var i = startY; i < stopY; i += step) {
207            setTimeout("window.scrollTo(0, " + leapY + ")", timer * speed);
208            leapY += step;
209            if (leapY > stopY) leapY = stopY;
210            timer++;
211        }
212    };
213
214    this.scrollUp = function (startY, stopY, speed, distance) {
215
216        var timer = 0;
217
218        var step = Math.round(distance / 25);
219        var leapY = startY - step;
220
221        for (var i = startY; i > stopY; i -= step) {
222            setTimeout("window.scrollTo(0, " + leapY + ")", timer * speed);
223            leapY -= step;
224            if (leapY < stopY) leapY = stopY;
225            timer++;
226        }
227    };
228
229    this.scrollToTop = function (stopY) {
230        scrollTo(0, stopY);
231    };
232
233    this.scrollTo = function (elementId, speed) {
234        // This scrolling function
235        // is from http://www.itnewb.com/tutorial/Creating-the-Smooth-Scroll-Effect-with-JavaScript
236
237        var element = document.getElementById(elementId);
238
239        if (element) {
240            var startY = getCurrentPagePosition(window, document);
241            var stopY = getElementY(document, element);
242
243            var distance = stopY > startY ? stopY - startY : startY - stopY;
244
245            if (distance < 100) {
246                this.scrollToTop(stopY);
247
248            } else {
249
250                var defaultSpeed = Math.round(distance / 100);
251                speed = speed || (defaultSpeed > 20 ? 20 : defaultSpeed);
252
253                if (stopY > startY) {
254                    this.scrollDown(startY, stopY, speed, distance);
255                } else {
256                    this.scrollUp(startY, stopY, speed, distance);
257                }
258            }
259
260        }
261
262    };
263
264})

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.