-
Notifications
You must be signed in to change notification settings - Fork 2
/
ngVk.js
68 lines (67 loc) · 2.57 KB
/
ngVk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
angular
.module('ngVk', [
'angularLoad'
])
.factory('vkSimplekHash', function () {
return function (str) {
var hash = 0;
if (str.length == 0) return hash;
for (var i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
})
.run(function (vkAppId, angularLoad, vkApi) {
angularLoad.loadScript('//vk.com/js/api/openapi.js').then(function () {
VK.init({apiId: vkAppId, onlyWidgets: true});
vkApi.set(VK);
}).catch(function () {
console.log('can\'t load vk api, please report this here https://github.com/shekspir55/ngVk')
});
})
.factory('vkApi', function () {
var factoryCallbacksArray = [];
return {
set: function (vkApi) {
angular.forEach(factoryCallbacksArray, function (factoryCallback) {
factoryCallback(vkApi);
});
},
onSet: function (callback) {
factoryCallbacksArray.push(callback);
}
};
})
.directive('vkComments',
function ($window, $timeout, $location, vkApi, vkSimplekHash) {
return {
restrict: 'E',
scope: {
readyToBind: '@',
url: '@'
},
replace: !0,
transclude: !0,
link: function (scope, $element, $attr) {
vkApi.onSet(function (vkApi) {
scope.$watch('readyToBind', function () {
$timeout(function () {
scope.idRand = Math.floor(Math.random() * (9999999 - 1 + 1) + 1).toString(10);
$element.html('');
$window.vkComment = vkApi.Widgets.Comments('vk-comments-' + scope.idRand, {
limit: 10,
attach: '*',
autoPublish: 1,
mini: 1
}, vkSimplekHash(scope.url || $location.path()));
}, 10);
});
});
},
template: '<div class="ngVk ngVkComments" id="vk-comments-{{idRand}}" ng-transclude post-url="{{url}}"></div>'
}
});