| author | ymh <ymh.work@gmail.com> |
| Tue, 10 Sep 2013 02:01:31 +0200 | |
| changeset 54 | 9f6e5abc5e74 |
| parent 52 | 99801979086c |
| child 55 | a1182b90cbd0 |
| permissions | -rw-r--r-- |
| 26 | 1 |
|
| 54 | 2 |
"use strict"; |
| 26 | 3 |
// initialize the app |
4 |
||
| 47 | 5 |
var app = angular.module("recordApp", ['ngResource', 'ngRoute', 'pascalprecht.translate']) |
| 26 | 6 |
|
|
33
945b144d68c1
put context variables in a single object
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
7 |
app.service("Api", function($resource, context) { |
| 47 | 8 |
this.record = $resource(context.urls.record_api, |
9 |
{}, |
|
10 |
{ |
|
11 |
get: { |
|
12 |
method: "GET", |
|
13 |
isArray: false |
|
14 |
}, |
|
15 |
save:{ |
|
16 |
method:"PUT", |
|
17 |
isArray:false, |
|
18 |
headers:{'X-CSRFToken':context.csrf_token} |
|
19 |
} |
|
20 |
}); |
|
| 26 | 21 |
}); |
22 |
||
| 49 | 23 |
app.service("RecordModel", function(Api, context) { |
24 |
this.record = Api.record.get({recordId: context.record_id}); |
|
25 |
this.uriLabels = context.uri_labels; |
|
26 |
}); |
|
27 |
||
| 54 | 28 |
|
29 |
app.directive('objectDisp', ['$compile', '$http', '$templateCache', 'context', function($compile, $http, $templateCache, context) { |
|
30 |
||
31 |
var getTemplate = function(templateName) { |
|
32 |
var templateLoader, |
|
33 |
templateUrl = context.urls.base_static+'p4l/templates/'+templateName+'.html'; |
|
34 |
templateLoader = $http.get(templateUrl, {cache: $templateCache}); |
|
35 |
||
36 |
return templateLoader; |
|
37 |
} |
|
38 |
||
39 |
var linker = function(scope, element, attrs) { |
|
40 |
||
41 |
var loader = getTemplate(scope.dispTemplate); |
|
42 |
||
43 |
var promise = loader.success(function(html) { |
|
44 |
element.html(html); |
|
45 |
}).then(function (response) { |
|
46 |
element.replaceWith($compile(element.html())(scope)); |
|
47 |
}); |
|
48 |
} |
|
49 |
||
50 |
return { |
|
51 |
restrict: 'E', |
|
52 |
scope: { |
|
53 |
dispTemplate: "=", |
|
54 |
obj: "=" |
|
55 |
}, |
|
56 |
link: linker |
|
57 |
}; |
|
58 |
}]); |
|
59 |
||
60 |
||
61 |
app.directive('objectList', function(RecordModel, context) { |
|
| 50 | 62 |
return { |
63 |
restrict: 'E', |
|
64 |
replace: true, |
|
65 |
transclude: true, |
|
66 |
scope: { |
|
| 54 | 67 |
list:"=objectList", |
68 |
dispTemplate: "@dispTemplate", |
|
69 |
formTemplate: "@formTemplate", |
|
70 |
objectFields: "@objectFields" |
|
71 |
}, |
|
72 |
controller: function($scope, $element, $attrs, $transclude) { |
|
73 |
$scope.getStaticTemplateUrl = function(templateName) { |
|
74 |
return context.urls.base_static+'p4l/templates/'+templateName+".html"; |
|
75 |
} |
|
76 |
$scope.getEmptyObjectFromList = function(fieldList) { |
|
77 |
var res = {}; |
|
78 |
for ( var field in fieldList) { |
|
79 |
res[field] = ""; |
|
80 |
} |
|
81 |
return res; |
|
82 |
} |
|
| 50 | 83 |
}, |
| 54 | 84 |
templateUrl: context.urls.base_static+'p4l/templates/objectList.html', |
85 |
link: function($scope, $element, $attrs) { |
|
86 |
$scope.editedObj = null; |
|
87 |
|
|
88 |
$scope.getEmptyObject = function() { |
|
89 |
return $scope.getEmptyObjectFromList(angular.fromJson($scope.objectFields)); |
|
90 |
}; |
|
91 |
||
92 |
$scope.setEditedObject = function(obj) { |
|
93 |
$scope.editedObj = obj; |
|
94 |
} |
|
95 |
|
|
96 |
$scope.newEditedObject = function() { |
|
97 |
var newObj = $scope.getEmptyObject(); |
|
98 |
$scope.list.push(newObj); |
|
99 |
$scope.setEditedObject(newObj); |
|
100 |
}; |
|
101 |
|
|
102 |
$scope.removeFromList = function(obj) { |
|
103 |
var i = $scope.list.indexOf(obj); |
|
104 |
if(i>=0){ |
|
105 |
$scope.list.splice(i, 1); |
|
106 |
} |
|
107 |
$scope.setEditedObject(null); |
|
108 |
} |
|
109 |
|
|
110 |
$scope.onOk = function() { |
|
111 |
$scope.setEditedObject(null); |
|
112 |
} |
|
| 50 | 113 |
|
114 |
} |
|
115 |
}; |
|
116 |
}); |
|
117 |
||
| 54 | 118 |
|
119 |
||
| 49 | 120 |
app.controller("RecordCtrl", function($scope, RecordModel, context){ |
| 35 | 121 |
|
| 49 | 122 |
$scope.record = RecordModel.record; |
| 54 | 123 |
$scope.uriLabels = RecordModel.uriLabels; |
| 47 | 124 |
|
125 |
$scope.submitRecord = function() { |
|
126 |
$scope.record.$save({recordId: context.record_id}); |
|
127 |
} |
|
| 38 | 128 |
|
| 50 | 129 |
|
| 26 | 130 |
}); |
131 |
||
| 51 | 132 |
app.directive('addSemUri', function(RecordModel, context){ |
133 |
return { |
|
134 |
restrict: 'E', |
|
135 |
replace: true, |
|
136 |
transclude: true, |
|
137 |
scope: { |
|
138 |
localid:"@", |
|
| 52 | 139 |
listname:"@" |
| 51 | 140 |
}, |
141 |
templateUrl: function(tElement, tAttrs) { |
|
142 |
return context.urls.base_static+'p4l/templates/addSemanticUriForm.html'; |
|
143 |
}, |
|
| 52 | 144 |
link: function($scope, $element, $attrs) { |
145 |
// Get queries attributes from $scope listname and context query dict |
|
146 |
attr_dict = context[$scope.listname + "_query_dict"]; |
|
147 |
for (var k in attr_dict){ |
|
| 51 | 148 |
if (attr_dict.hasOwnProperty(k)) { |
| 52 | 149 |
$scope[k] = attr_dict[k]; |
| 51 | 150 |
} |
| 52 | 151 |
} |
152 |
// initalize autocomplete and browse thesaurus events |
|
153 |
init_autocomplete(); |
|
154 |
// We have to apply because init_browse needs the real ids and not {{ localid }} |
|
155 |
$scope.$apply(); |
|
156 |
init_browse(); |
|
157 |
}, |
|
158 |
controller: function($scope, $element, $attrs, $transclude, RecordModel){ |
|
159 |
$scope.record = RecordModel.record; |
|
160 |
$scope.uriLabels = RecordModel.uriLabels; |
|
161 |
$scope.addUriText = ''; |
|
162 |
|
|
163 |
$scope.addUriToList = function() { |
|
164 |
$scope.record[$scope.listname].push($scope.addUriText); |
|
165 |
$scope.addUriText = ''; |
|
166 |
}; |
|
167 |
|
|
168 |
$scope.updateUriLabelDict = function(k,v) { |
|
169 |
$scope.uriLabels[k] = v; |
|
170 |
}; |
|
| 51 | 171 |
} |
172 |
} |
|
173 |
}); |
|
174 |
||
| 26 | 175 |
app.config(['$routeProvider', function($routeProvider) { |
176 |
// $routeProvider.when('/', {controller: 'RecordCtrl', templateUrl: 'partials/record.html'}); |
|
177 |
// $routeProvider.otherwise({redirectTo: '/'}); |
|
178 |
}]); |
|
|
28
f26426e9360b
update view simplification. Baby step towards complete form.
ymh <ymh.work@gmail.com>
parents:
26
diff
changeset
|
179 |