Advertisement
Guest User

AngularJS - Other

a guest
Aug 29th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*global angular */
  2. String.prototype.replaceAll = function(search, replacement) {
  3.     var target = this;
  4.     return target.replace(new RegExp(search, 'g'), replacement);
  5. };
  6.  
  7. /**
  8.  * The main HPR(Hamparan Plastindo Raya) app module
  9.  *
  10.  * @type {angular.Module}
  11.  */
  12. var hprApp = angular.module('hprApp', ['ngResource']);
  13. var apiPrefix = '/api/';
  14.  
  15. hprApp.factory('ApiFactory', function($resource) {
  16.   var temp = {
  17.     Item: $resource("/api/item/:id"),
  18.     Customer: $resource("/api/customer/:id"),
  19.     Order: $resource("/api/order/:id"),
  20.     OrderDetail: $resource("/api/order_detail/:id", {}, {
  21.       delete: {method: 'GET', url: "/api/order_detail/delete/:id"},
  22.       getByOrderId: {method:'GET',
  23.         url: "/api/order_detail/get_by_order_id/:order_id", isArray:true}
  24.     })
  25.   };
  26.  
  27.   return temp;
  28. });
  29.  
  30. hprApp.service('ItemService', function(ApiFactory) {
  31.   this.createEmptyJson = function() {
  32.     return {
  33.       id: null,
  34.       name: null,
  35.       qty: null,
  36.       price: null
  37.     };
  38.   }
  39. });
  40.  
  41. // Directive
  42. hprApp.directive('ngCurrencyInput', function($filter, $browser) {
  43.     return {
  44.         require: 'ngModel',
  45.         link: function($scope, $element, $attrs, ngModelCtrl) {
  46.             var listener = function() {
  47.                 var value = $element.val().replace(/,/g, '')
  48.                 $element.val($filter('number')(value, false))
  49.             }
  50.  
  51.             // This runs when we update the text field
  52.             ngModelCtrl.$parsers.push(function(viewValue) {
  53.                 return viewValue.replace(/,/g, '');
  54.             })
  55.  
  56.             // This runs when the model gets updated on the scope directly and keeps our view in sync
  57.             ngModelCtrl.$render = function() {
  58.                 $element.val($filter('number')(ngModelCtrl.$viewValue, false))
  59.             }
  60.  
  61.             $element.bind('change', listener)
  62.             $element.bind('keydown', function(event) {
  63.                 var key = event.keyCode
  64.                 // If the keys include the CTRL, SHIFT, ALT, or META keys, or the arrow keys, do nothing.
  65.                 // This lets us support copy and paste too
  66.                 if (key == 91 || (15 < key && key < 19) || (37 <= key && key <= 40))
  67.                     return
  68.                 $browser.defer(listener) // Have to do this or changes don't get picked up properly
  69.             })
  70.  
  71.             $element.bind('paste cut', function() {
  72.                 $browser.defer(listener)
  73.             })
  74.         }
  75.  
  76.     }
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement