Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. angular.module('Cockpit.state.mademyday.BatchSend', [
  2.   'ui.router',
  3.   'Cockpit.service.mademyday.BatchSend'
  4. ])
  5.  
  6. .config(function config($stateProvider) {
  7.  
  8.     $stateProvider.state('private.orders.batch-send', {
  9.         url: '/batch-send/',
  10.         controller: 'CockpitMademydayBatchSendCtrl',
  11.         templateUrl: 'states/mademyday/batch-send/batch-send.tpl.html',
  12.         data: {
  13.             pageTitle: 'Batch-Send Gifts'
  14.         }
  15.     })
  16.  
  17.     ;
  18. })
  19.  
  20. .run(function (Navigation) {
  21.     Navigation.sidebar.register('private.orders', 'Orders', [
  22.         {
  23.             Label: 'Batch Send',
  24.             State: 'private.orders.batch-send'
  25.         }
  26.     ]);
  27. })
  28.  
  29. .controller('CockpitMademydayBatchSendCtrl', function CockpitMademydayBatchSendCtrl($scope, Product) {
  30.     function parseRawData(lines, useHeaders) {
  31.         var parsed = [];
  32.  
  33.         var headers = [];
  34.         for (var i in lines) {
  35.             // Store headers if any
  36.             if (i == 0 && useHeaders) {
  37.                 headers = lines[i].toLowerCase().split(';');
  38.                 continue;
  39.             }
  40.  
  41.             var cols = lines[i].split(';');
  42.  
  43.             var parsedLine = {};
  44.  
  45.             for (var j in cols) {
  46.                 parsedLine[j] = cols[j];
  47.                 if (useHeaders) {
  48.                     parsedLine[headers[j]] = cols[j];
  49.                 }
  50.             }
  51.  
  52.             parsed.push(parsedLine);
  53.         }
  54.  
  55.         return parsed;
  56.     }
  57.  
  58.     function parseMessage(message, data){
  59.         var shortcodes = message.match(/\[([^\]]+)\]/g);
  60.  
  61.         if (shortcodes == null || shortcodes.length == 0) {
  62.             return message;
  63.         }
  64.  
  65.         for (var i in shortcodes) {
  66.             var shortcode = shortcodes[i].replace('[', '').replace(']', '').replace('col-', '');
  67.            
  68.             if (!angular.isDefined(data[shortcode])) {
  69.                 continue;
  70.             }
  71.  
  72.             message = message.replace(shortcodes[i], data[shortcode]);
  73.         }
  74.  
  75.         return message;
  76.     }
  77.  
  78.     function updateExampleMessage() {
  79.         if ($scope.batchData.Message === "" || $scope.batchData.Data.length == 0) {
  80.             $scope.exampleMessage = $scope.batchData.Message;
  81.             return;
  82.         }
  83.  
  84.         $scope.exampleMessage = parseMessage($scope.batchData.Message, $scope.batchData.Data[0]);
  85.     }
  86.  
  87.     $scope.rawData = "";
  88.     $scope.exampleMessage = "";
  89.     $scope.batchData = {
  90.         Product: null,
  91.         Variant: null,
  92.         Message: "",
  93.         Price: null,
  94.         Data: [],
  95.         Headers: true
  96.     };
  97.     $scope.products = Product.query();
  98.  
  99.     $scope.$watch('rawData', function (data) {
  100.         if (data === null || data === "") {
  101.             $scope.batchData.Data = [];
  102.             return;
  103.         }
  104.  
  105.         var lines = data.split("\n");
  106.  
  107.         // Make sure we have more than one line of data
  108.         if (lines.length === 0) {
  109.             $scope.batchData.Data = [];
  110.             return;
  111.         }
  112.  
  113.         // Check if we're using headers, and if there's only one line
  114.         if ($scope.batchData.Headers && lines.length === 1) {
  115.             return;
  116.         }
  117.  
  118.         $scope.batchData.Data = parseRawData(lines, $scope.batchData.Headers);
  119.     });
  120.  
  121.     $scope.$watch(function () {
  122.         return $scope.batchData.Message;
  123.     }, updateExampleMessage);
  124.  
  125.     $scope.$watch(function () {
  126.         return $scope.batchData.Data;
  127.     }, updateExampleMessage);
  128. })
  129.  
  130.  
  131. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement