1+ 'use strict' ;
2+
3+ var module = angular . module ( 'supportAdminApp' ) ;
4+
5+ module . controller ( 'IdeasDetailController' , [ '$scope' , '$log' , 'IdeaService' , 'Alert' , '$timeout' , 'OAuth2Service' , '$uibModalInstance' , 'ideaId' , 'domain' , 'username' , 'password' , 'clientId' , 'clientSecret' , '$sce' , 'SPIGIT_API_URL' ,
6+ function ( $scope , $log , IdeaService , $alert , $timeout , OAuth2Service , $modalInstance , ideaId , domain , username , password , clientId , clientSecret , $sce , SPIGIT_API_URL ) {
7+
8+ $scope . ideaId = ideaId ;
9+ $scope . domain = domain ;
10+ $scope . username = username ;
11+ $scope . password = password ;
12+ $scope . clientId = clientId ;
13+ $scope . clientSecret = clientSecret ;
14+
15+ $scope . form = { } ;
16+ $scope . isLoading = false ;
17+
18+ $scope . load = function ( ) {
19+
20+ $scope . isLoading = true ;
21+
22+ // If token expired should get new token first
23+ if ( OAuth2Service . checkTokenExpireTime ( ) ) {
24+ OAuth2Service . refreshToken ( $scope . username , $scope . password , $scope . domain , $scope . clientId , $scope . clientSecret ) . then ( function ( data ) {
25+ $scope . loadDetail ( ) ;
26+ } ) . catch ( function ( error ) {
27+ $alert . error ( error . message , $scope ) ;
28+ $scope . isLoading = false ;
29+ } ) ;
30+ } else {
31+ $scope . loadDetail ( ) ;
32+ }
33+ }
34+
35+ // get idea detail
36+ $scope . loadDetail = function ( ) {
37+ IdeaService . getIdeaDetail ( domain , $scope . ideaId ) . then ( function ( data ) {
38+ $scope . form . title = data . title ;
39+ $scope . form . content = $sce . trustAsHtml ( $scope . convertContent ( data . content ) ) ;
40+ // get creator detail
41+ IdeaService . getUserDetail ( domain , data . creator_id ) . then ( function ( user ) {
42+ $scope . form . firstName = user . first_name ;
43+ $scope . form . lastName = user . last_name ;
44+ $scope . form . email = user . primary_email ;
45+ $scope . isLoading = false ;
46+ } ) ;
47+ } ) . catch ( function ( error ) {
48+ $alert . error ( error . error , $scope ) ;
49+ $scope . isLoading = false ;
50+ } ) ;
51+ }
52+
53+ /**
54+ * Convert detail content without html tags
55+ * @param content
56+ * @returns {string }
57+ */
58+ $scope . convertContent = function ( content ) {
59+ var newContent = content . replace ( / < i m g [ ^ > ] * s r c = [ ' " ] ( [ ^ ' " ] + ) [ ^ > ] * > / gi, function ( match , capture ) {
60+ var newStr = '<img src="http://' + domain + "." + SPIGIT_API_URL + capture + '" />' ;
61+ return newStr ;
62+ } ) ;
63+ return newContent ;
64+ }
65+
66+ $scope . cancel = function ( ) {
67+ $modalInstance . close ( ) ;
68+ } ;
69+
70+
71+ $scope . load ( ) ;
72+ }
73+ ] ) ;
0 commit comments