Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- angular.module('Cockpit.state.mademyday.BatchSend', [
- 'ui.router',
- 'Cockpit.service.mademyday.BatchSend'
- ])
- .config(function config($stateProvider) {
- $stateProvider.state('private.orders.batch-send', {
- url: '/batch-send/',
- controller: 'CockpitMademydayBatchSendCtrl',
- templateUrl: 'states/mademyday/batch-send/batch-send.tpl.html',
- data: {
- pageTitle: 'Batch-Send Gifts'
- }
- })
- ;
- })
- .run(function (Navigation) {
- Navigation.sidebar.register('private.orders', 'Orders', [
- {
- Label: 'Batch Send',
- State: 'private.orders.batch-send'
- }
- ]);
- })
- .controller('CockpitMademydayBatchSendCtrl', function CockpitMademydayBatchSendCtrl($scope, Product) {
- function parseRawData(lines, useHeaders) {
- var parsed = [];
- var headers = [];
- for (var i in lines) {
- // Store headers if any
- if (i == 0 && useHeaders) {
- headers = lines[i].toLowerCase().split(';');
- continue;
- }
- var cols = lines[i].split(';');
- var parsedLine = {};
- for (var j in cols) {
- parsedLine[j] = cols[j];
- if (useHeaders) {
- parsedLine[headers[j]] = cols[j];
- }
- }
- parsed.push(parsedLine);
- }
- return parsed;
- }
- function parseMessage(message, data){
- var shortcodes = message.match(/\[([^\]]+)\]/g);
- if (shortcodes == null || shortcodes.length == 0) {
- return message;
- }
- for (var i in shortcodes) {
- var shortcode = shortcodes[i].replace('[', '').replace(']', '').replace('col-', '');
- if (!angular.isDefined(data[shortcode])) {
- continue;
- }
- message = message.replace(shortcodes[i], data[shortcode]);
- }
- return message;
- }
- function updateExampleMessage() {
- if ($scope.batchData.Message === "" || $scope.batchData.Data.length == 0) {
- $scope.exampleMessage = $scope.batchData.Message;
- return;
- }
- $scope.exampleMessage = parseMessage($scope.batchData.Message, $scope.batchData.Data[0]);
- }
- $scope.rawData = "";
- $scope.exampleMessage = "";
- $scope.batchData = {
- Product: null,
- Variant: null,
- Message: "",
- Price: null,
- Data: [],
- Headers: true
- };
- $scope.products = Product.query();
- $scope.$watch('rawData', function (data) {
- if (data === null || data === "") {
- $scope.batchData.Data = [];
- return;
- }
- var lines = data.split("\n");
- // Make sure we have more than one line of data
- if (lines.length === 0) {
- $scope.batchData.Data = [];
- return;
- }
- // Check if we're using headers, and if there's only one line
- if ($scope.batchData.Headers && lines.length === 1) {
- return;
- }
- $scope.batchData.Data = parseRawData(lines, $scope.batchData.Headers);
- });
- $scope.$watch(function () {
- return $scope.batchData.Message;
- }, updateExampleMessage);
- $scope.$watch(function () {
- return $scope.batchData.Data;
- }, updateExampleMessage);
- })
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement