|
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | if (!angular) |
5 | | - throw new Error('angular.js required!'); |
6 | | - |
| 5 | + throw new Error('Angular.js required!'); |
| 6 | + |
7 | 7 | var NProgressExist = NProgress && NProgress.start && NProgress.done; |
8 | 8 |
|
9 | 9 | angular |
10 | 10 | .module('duoshuo', []) |
| 11 | + // API set |
11 | 12 | .provider('duoshuo', duoshuoProvider) |
12 | | - .directive('duoshuo', ['duoshuo', duoshuoDirective]); |
| 13 | + // Directives |
| 14 | + .directive('dsThread', createDirective('ds-thread')) |
| 15 | + .directive('dsRecentComments', createDirective('ds-recent-comments')) |
| 16 | + .directive('dsRecentVisitors', createDirective('ds-recent-visitors')) |
| 17 | + .directive('dsThreadCount', createDirective('ds-thread-count')) |
| 18 | + .directive('dsTopThreads', createDirective('ds-top-threads')) |
| 19 | + .directive('dsLogin', createDirective('ds-login')); |
13 | 20 |
|
14 | 21 | function duoshuoProvider() { |
15 | 22 | this.config = config; |
|
80 | 87 | }; |
81 | 88 |
|
82 | 89 | // Comments renderer |
83 | | - duoshuo.render = function(attrs) { |
84 | | - if (!window.DUOSHUO) |
85 | | - throw new Error('duoshuo embed.js required!'); |
86 | | - |
87 | | - var data = {}; |
88 | | - if (attrs.threadId) data['thread-id'] = attrs.threadId; |
89 | | - if (attrs.threadKey) data['thread-key'] = attrs.threadKey; |
| 90 | + duoshuo.render = function(options) { |
| 91 | + if (!window.DUOSHUO || !window.DUOSHUO.initSelector) |
| 92 | + throw new Error('createDirective(); duoshuo embed.js is required!'); |
90 | 93 |
|
91 | | - return window.DUOSHUO.createEmbedThread('div', data); |
| 94 | + return window.DUOSHUO.initSelector( |
| 95 | + window.DUOSHUO.selectors['.ds-thread'] |
| 96 | + ) |
92 | 97 | }; |
93 | 98 |
|
94 | 99 | return duoshuo; |
95 | 100 | } |
96 | 101 | ]; |
97 | 102 | } |
98 | 103 |
|
99 | | - function duoshuoDirective(duoshuo) { |
100 | | - return { |
101 | | - restrict: 'AE', |
102 | | - replace: true, |
103 | | - template: '<div class="ds-thread-wrapper"></div>', |
104 | | - link: function(scope, element, attrs) { |
105 | | - // Render comments when DOM has been injected. |
106 | | - angular.element(document).ready(function() { |
107 | | - // Fired after DOM ready |
108 | | - angular |
109 | | - .element(element[0]) |
110 | | - .append(duoshuo.render(attrs)); |
111 | | - }); |
112 | | - } |
113 | | - }; |
| 104 | + function createDirective(type) { |
| 105 | + return function directive() { |
| 106 | + return { |
| 107 | + restrict: 'AE', |
| 108 | + replace: true, |
| 109 | + template: '<div class="' + type + '">', |
| 110 | + link: function(scope, element, attrs) { |
| 111 | + if (!window.DUOSHUO || !window.DUOSHUO.initSelector) |
| 112 | + return; |
| 113 | + |
| 114 | + // Trigger init selector function |
| 115 | + window.DUOSHUO |
| 116 | + .initSelector(window.DUOSHUO.selectors['.' + type]) |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
114 | 120 | } |
115 | 121 |
|
116 | 122 | })(window.angular, window.NProgress); |
0 commit comments