Advertisement
Guest User

controller.js

a guest
May 5th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.21 KB | None | 0 0
  1. var rest_url='http://mobiloutlet.id/panel/Api/';
  2. angular.module('starter.controllers', ['ionic','ngCordova'])
  3. .factory('formDataObject', function() {
  4. return function(data) {
  5. var fd = new FormData();
  6. angular.forEach(data, function(value, key) {
  7. if(key == "file"){
  8. fd.append(key, new Blob([ value ], { type: "image/jpeg" }),"image.jpeg");
  9. }else{
  10. fd.append(key, value);
  11. }
  12.  
  13. });
  14. return fd;
  15. };
  16. }).factory('fileReader', function($q) {
  17.  
  18. return {
  19. readAsDataUrl: function(filePath) {
  20.  
  21. var deferred = $q.defer();
  22.  
  23. function gotFileEntry(fileEntry) {
  24. fileEntry.file(gotFile, fail);
  25. }
  26.  
  27. function gotFile(file){
  28. readDataUrl(file);
  29. }
  30.  
  31. function readDataUrl(file) {
  32. var reader = new FileReader();
  33. reader.onloadend = function(evt) {
  34. console.log("Read as data URL");
  35. fileContent = evt.target.result;
  36. deferred.resolve(fileContent);
  37.  
  38. };
  39. reader.readAsArrayBuffer(file);
  40. }
  41.  
  42. function fail(evt) {
  43. console.log(evt.target.error.code);
  44. }
  45.  
  46. // window.resolveLocalFileSystemURI(filePath, gotFileEntry, fail);
  47. window.resolveLocalFileSystemURL(filePath, gotFileEntry, fail);
  48.  
  49. return deferred.promise;
  50. }
  51. };
  52. })
  53. .controller('AppCtrl', function($scope, $ionicModal, $timeout, $ionicLoading, $http) {
  54.  
  55. // With the new view caching in Ionic, Controllers are only called
  56. // when they are recreated or on app start, instead of every page change.
  57. // To listen for when this page is active (for example, to refresh data),
  58. // listen for the $ionicView.enter event:
  59. //$scope.$on('$ionicView.enter', function(e) {
  60. //});
  61.  
  62. // Form data for the login modal
  63. $scope.loginData = {};
  64.  
  65. // Create the login modal that we will use later
  66. $ionicModal.fromTemplateUrl('templates/login.html', {
  67. scope: $scope
  68. }).then(function(modal) {
  69. $scope.modal = modal;
  70. });
  71.  
  72. // Triggered in the login modal to close it
  73. $scope.closeLogin = function() {
  74. $scope.modal.hide();
  75. };
  76.  
  77. // Open the login modal
  78. $scope.login = function() {
  79. $scope.modal.show();
  80. };
  81.  
  82. // Perform the login action when the user submits the login form
  83. $scope.doLogin = function() {
  84. console.log('Doing login', $scope.loginData);
  85.  
  86. // Simulate a login delay. Remove this and replace with your login
  87. // code if using a login system
  88. $timeout(function() {
  89. $scope.closeLogin();
  90. }, 1000);
  91. };
  92.  
  93. $scope.saveConn = function() {
  94. // $scope.config = {};
  95. // $scope.username = this.config.username;
  96. // $scope.password = this.config.password;
  97.  
  98. $ionicLoading.show({
  99. template: 'Checking server connection...',
  100. showBackdrop: true
  101. });
  102.  
  103. // var somplak = {
  104. // method: 'POST',
  105. // url: 'http://mobiloutlet.id/panel/Api/Login',
  106. // headers: {
  107. // 'Content-Type': 'application/json'
  108. // },
  109. // timeout: 5000,
  110. // data: $scope.config,
  111. // };
  112. $http({
  113. url: 'http://mobiloutlet.id/panel/Api/Login',
  114. method: 'POST',
  115. headers: { 'Content-Type': 'application/json' },
  116. data: {username:this.username,password:this.password}
  117. }).
  118. then(function(response) {
  119. // $ionicLoading.hide();
  120. if (response.Success == 'true') {
  121. $location.path("/home");
  122. } else {
  123. $ionicLoading.show({
  124. template: 'Username and Password Missmatch',
  125. showBackdrop: true
  126. });
  127. }
  128. });
  129. }
  130. })
  131.  
  132. .controller('HomeCtrl', function($scope, $state, $ionicSideMenuDelegate, $http, $ionicLoading, $location, $timeout, $cordovaFileTransfer, $cordovaCamera, $cordovaFile, $cordovaDevice, $ionicPopup, $cordovaActionSheet, formDataObject, $cordovaCamera, fileReader, fileUpload) {
  133. $scope.gambar=[];
  134. $scope.baseimage=[];
  135. $scope.uploadFile = function () {
  136. var files = $scope.myFile,
  137. uploadUrl = "http://httpbin.org/post";
  138.  
  139. function callback(data, status, headers, config) {
  140. if (status == 200) console.log('Success!');
  141. else console.log('Error!');
  142. }
  143.  
  144. if (files !==undefined) {
  145. for (var i = 0; i < files.length; i++) {
  146. var file = files[i];
  147. console.log('file is ' + JSON.stringify(file.name));
  148. fileUpload.uploadFileToUrl(file, uploadUrl, callback);
  149. }
  150. }
  151. };
  152.  
  153. $scope.upload=function(files){
  154. $scope.files = files;
  155. if (files && files.length) {
  156. Upload.upload({
  157. url: '/upload_image',
  158. method: 'POST',
  159. data: {
  160. files: files
  161. }
  162. }).then(function (response) {
  163. $timeout(function () {
  164. $scope.result = response.data;
  165. });
  166. }, function (response) {
  167. if (response.status > 0) {
  168. $scope.errorMsg = response.status + ': ' + response.data;
  169. }
  170. }, function (evt) {
  171. $scope.progress =
  172. Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
  173. });
  174. }
  175.  
  176. };
  177. $ionicSideMenuDelegate.canDragContent(false);
  178. $http.get('http://mobiloutlet.id/panel/Api/getVehicleBrands').then(function(result){
  179. $scope.items = result.data.content;
  180. console.log(result.data.content);
  181. });
  182.  
  183. $http.get('http://mobiloutlet.id/panel/Api/getVehicleCategory').then(function(result){
  184. $scope.category = result.data.content;
  185. console.log(result.data.content);
  186. });
  187.  
  188. $scope.saveProspect = function(config) {
  189.  
  190. $scope.config = {};
  191. $scope.config.VehicleProspectBrandId = config.VehicleProspectBrandId;
  192. $scope.config.VehicleProspectCategoryId = config.VehicleProspectCategoryId;
  193. $scope.config.VehicleProspectName = config.VehicleProspectName;
  194. $scope.config.VehicleProspectModels = config.VehicleProspectModels;
  195. $scope.config.VehicleProspectColor = config.VehicleProspectColor;
  196. $scope.config.VehicleProspectTransmision = config.VehicleProspectTransmision;
  197. $scope.config.VehicleProspectBuildYear = config.VehicleProspectBuildYear;
  198. $scope.config.VehicleProspectLastKm = config.VehicleProspectLastKm;
  199. $scope.config.VehicleProspectPrice = config.VehicleProspectPrice;
  200. $scope.config.VehicleProspectOfferPrice = config.VehicleProspectOfferPrice;
  201. $scope.config.VehicleProspectSalesName = config.VehicleProspectSalesName;
  202. $scope.config.VehicleProspectSalesPhone = config.VehicleProspectSalesPhone;
  203. $scope.config.VehicleProspectSalesDealer = config.VehicleProspectSalesDealer;
  204. $scope.config.VehicleProspectCreateDate = config.VehicleProspectCreateDate;
  205. $scope.config.VehicleProspectUserId = '30';
  206. $scope.config.VehiclePhotoProspectPhotoPath = config.file;
  207.  
  208. $ionicLoading.show({
  209. template: 'Menyimpan...',
  210. showBackdrop: true
  211. });
  212.  
  213. $http({
  214. method: 'POST',
  215. url: 'http://mobiloutlet.id/panel/Api/saveProspect',
  216. headers: {
  217. 'Content-Type': 'application/json'
  218. },
  219. data: {
  220. data:$scope.config
  221. },
  222. //transformRequest: formDataObject
  223. }).
  224. then(function(response) {
  225. if (response.data.Success == true) {
  226. $ionicLoading.show({
  227. template: 'Data Tersimpan',
  228. showBackdrop: true
  229. });
  230. if($scope.gambar!=""){
  231. $scope.simpan_photo(response.data.id_prospect);
  232. }
  233. $location.path("app/data");
  234. $ionicLoading.hide();
  235.  
  236. } else {
  237. console.log(response.data.Success);
  238. $ionicLoading.show({
  239. template: 'Error Menyimpan Data',
  240. showBackdrop: true
  241. });
  242. $ionicLoading.hide();
  243. $location.path("app/home");
  244. }
  245. });
  246. }
  247.  
  248. $scope.test=function(){
  249. fd=new FormData();
  250. fd.append('sabar', 'sabar');
  251. $http({
  252. method: 'POST',
  253. url: 'http://mobiloutlet.id/panel/Api/simpanPhoto',
  254. headers: {
  255. 'Content-Type':'application/json'
  256. },
  257. data: fd,
  258. //transformRequest: formDataObject
  259. })
  260. .success(function(data, status, headers, config) {
  261. console.log(data);
  262. })
  263. .error(function(data, status, headers, config) {
  264. $ionicLoading.hide();
  265. });
  266. }
  267.  
  268. $scope.simpan_photo=function($id){
  269. fd=new FormData();
  270. fd.append('id', $id);
  271. angular.forEach($scope.baseimage, function(value, i){
  272. fd.append(i, new Blob([ value ], { type: "image/jpeg" }),"image.jpeg");
  273. });
  274. $http({
  275. method: 'POST',
  276. url: 'http://mobiloutlet.id/panel/Api/simpanPhoto',
  277. headers: {
  278. 'Content-Type':'application/json'
  279. },
  280. data: fd
  281. })
  282. .success(function(data, status, headers, config) {
  283. $scope.baseimage=[];
  284. $scope.gambar=[];
  285. })
  286. .error(function(data, status, headers, config) {
  287. $ionicLoading.hide();
  288. });
  289. $ionicLoading.hide();
  290. }
  291. $scope.openPhotoLibrary = function() {
  292. $scope.imgtmp="";
  293. $scope.imgtmp1="";
  294. var options = {
  295. quality: 75,
  296. destinationType: Camera.DestinationType.FILE_URI,
  297. sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
  298. allowEdit: true,
  299. encodingType: Camera.EncodingType.JPEG,
  300. targetWidth: 300,
  301. targetHeight: 300,
  302. popoverOptions: CameraPopoverOptions,
  303. saveToPhotoAlbum: false
  304. };
  305. $cordovaCamera.getPicture(options).then(function (imageData) {
  306. fileReader.readAsDataUrl(imageData).then(function(fileInfo) {
  307. console.log(fileInfo);
  308. $scope.imgtmp1=fileInfo;
  309. $scope.baseimage.push($scope.imgtmp1);
  310. }, function(err) {
  311. });
  312. $scope.imgtmp=imageData;
  313. $scope.gambar.push($scope.imgtmp);
  314. console.log($scope.gambar);
  315. console.log($scope.baseimage);
  316. }, function (err) {
  317. });
  318. }
  319. })
  320.  
  321. .controller('DataCtrl', function($scope, $http, $ionicNavBarDelegate, $timeout, $location) {
  322. $scope.doRefresh = function() {
  323.  
  324. console.log('Refreshing!');
  325. $timeout( function() {
  326. //simulate async response
  327. $location.path("app/data");
  328.  
  329. //Stop the ion-refresher from spinning
  330. $scope.$broadcast('scroll.refreshComplete');
  331.  
  332. }, 1000);
  333.  
  334. };
  335. $ionicNavBarDelegate.showBackButton(false);
  336. $http.get("http://mobiloutlet.id/panel/Api/getProspect")
  337. .success(function (response)
  338. {
  339. $scope.names = response.content;
  340. });
  341. })
  342.  
  343. .controller('ViewCtrl', function($scope, $http, $stateParams, $ionicNavBarDelegate) {
  344. $ionicNavBarDelegate.showBackButton(false);
  345. $http.get("http://mobiloutlet.id/panel/Api/getSelected_Prospect/"+$stateParams.VehicleProspectId)
  346. .success(function (response)
  347. {
  348. $scope.names = response.content;
  349. });
  350.  
  351. $http.get("http://mobiloutlet.id/panel/Api/getSelected_PhotoProspect/"+$stateParams.VehicleProspectId)
  352. .success(function (response)
  353. {
  354. $scope.photo = response.content;
  355. });
  356. })
  357.  
  358. .controller('PlaylistsCtrl', function($scope) {
  359. $scope.playlists = [
  360. { title: 'Reggae', id: 1 },
  361. { title: 'Chill', id: 2 },
  362. { title: 'Dubstep', id: 3 },
  363. { title: 'Indie', id: 4 },
  364. { title: 'Rap', id: 5 },
  365. { title: 'Cowbell', id: 6 }
  366. ];
  367. })
  368.  
  369. .controller('PlaylistCtrl', function($scope, $stateParams) {
  370. })
  371.  
  372. .controller('LoginCtrl', function($scope, $ionicModal, $timeout, $ionicLoading, $http, $location) {
  373. event.preventDefault();
  374. $scope.saveConn = function(config) {
  375.  
  376. $scope.config = {};
  377.  
  378. $ionicLoading.show({
  379. template: 'Checking server connection...',
  380. showBackdrop: true
  381. });
  382. console.log($scope.config);
  383. $http({
  384. url: 'http://mobiloutlet.id/panel/Api/Login',
  385. method: 'POST',
  386. headers: { 'Content-Type': 'application/json' },
  387. timeout: 5000,
  388. data: {username:config.username,password:config.password}
  389. }).
  390. then(function(response) {
  391.  
  392. if (response.data.hasil == 'benar') {
  393. $ionicLoading.hide();
  394. $location.path("app/home");
  395. } else {
  396. // $ionicLoading.hide();
  397. console.log(response.data.Success);
  398. $ionicLoading.show({
  399. template: 'Username atau Password Salah',
  400. showBackdrop: true
  401. });
  402. $location.path("app/home");
  403. }
  404. // }
  405. });
  406. }
  407. })
  408.  
  409. .directive('fileModel', ['$parse', function ($parse) {
  410. return {
  411. restrict: 'A',
  412. link: function (scope, element, attrs) {
  413. var model = $parse(attrs.fileModel);
  414. var modelSetter = model.assign;
  415.  
  416. element.bind('change', function () {
  417. scope.$apply(function () {
  418. modelSetter(scope, element[0].files);
  419. });
  420. });
  421. }
  422. };
  423. }])
  424.  
  425. .service('fileUpload', ['$http', function ($http) {
  426. this.uploadFileToUrl = function (file, uploadUrl, callback) {
  427. var fd = new FormData();
  428. var files = file;
  429. fd.append('file', file);
  430.  
  431. $http.post(uploadUrl, fd, {
  432. transformRequest: angular.identity,
  433. headers: {
  434. 'Content-Type': 'application/json'
  435. }
  436. })
  437. .success(callback)
  438. .error(callback);
  439. };
  440. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement