﻿
angular.module('umbraco.resources')
    .factory('nuPickers.Shared.SaveFormat.SaveFormatResource',
        function () {

            return {

                // pickedOptions: [{"key":"","label":""},{"key":"","label":""}...]
                // returns a string representation of the picked options as per the configured SaveFormat
                createSaveValue: function (config, pickedOptions) {
                    
                    if (pickedOptions == null || pickedOptions.length == 0 || pickedOptions[0] == null) {
                        return null;
                    }

                    switch (config.saveFormat) {

                        case 'csv': // "key, key..."                        
                            return pickedOptions.map(function (option) { return option.key; }).join();
                            break;

                        case 'json': // some pickers add extra data to the options, so only return a key/label collection
                            return JSON.stringify(
                                            pickedOptions.map(function (option) {
                                                return { 'key': option.key, 'label': option.label }
                                            }));
                            break;

                        case 'xml':
                            var xml = '<Picker>';

                            for (var i = 0; i < pickedOptions.length; i++) {
                                xml += '<Picked Key="' + pickedOptions[i].key + '">'
                                xml += '<![CDATA[' + pickedOptions[i].label + ']]>';
                                xml += '</Picked>';
                            }

                            xml += '</Picker>';

                            return xml;
                            break;

                        case 'relationsOnly': // when saving to relations only
                        default: 
                            return null;
                            break;
                    }
                },

                getSavedKeys: function (saveValue) {

                    if (saveValue instanceof Array) // json
                    {
                        return saveValue.map(function (option) { return option.key }).join().split(',');
                    }

                    try {
                        var xml = $.parseXML(saveValue);
                        var keys = new Array();
                        $(xml).find('Picked').each(function () {
                            keys.push($(this).attr('Key'));
                        });

                        return keys;
                    }
                    catch (error) {
                    }

                    return saveValue.split(','); // csv
                },

                // saveValue will be either json or xml, so both key/label can be returned
                getSavedItems: function(saveValue) {
                    if (saveValue instanceof Array) // json
                    {
                        return saveValue;
                    }

                    try {
                        var xml = $.parseXML(saveValue);
                        var items = new Array();
                        $(xml).find('Picked').each(function () {
                            items.push({
                                'key': $(this).attr('Key'),
                                'label': $(this).text()
                            });
                        });

                        return items;
                    }
                    catch (error) {

                    }

                    return null;
                }
            };
        }
);
@@ -2131,32 +2131,34 @@
              if 
+(!
 (saveValue insta
@@ -2173,39 +2173,110 @@
 ray)
- // json%0A                    %7B%0A
+) %7B%0A                        try %7B%0A%0A                            saveValue = JSON.parse(saveValue);%0A    
     
@@ -2376,16 +2376,21 @@
 t(',');%0A
+%0A    
         
@@ -2394,34 +2394,119 @@
                %7D
-%0A%0A
+ catch (error) %7B%0A%0A                            //its not json its probably XML.%0A        
                 
@@ -2507,32 +2507,40 @@
           try %7B%0A
+        
                 
@@ -2600,24 +2600,32 @@
             
+      
+  
 var keys = n
@@ -2628,32 +2628,40 @@
  = new Array();%0A
+        
                 
@@ -2737,16 +2737,24 @@
         
+    
+    
 keys.pus
@@ -2793,32 +2793,40 @@
                 
+        
 %7D);%0A%0A           
@@ -2834,24 +2834,32 @@
             
+ 
+       
 return keys;
@@ -2871,34 +2871,50 @@
                 
+       
+ 
 %7D%0A
+        
                 
@@ -2932,16 +2932,218 @@
 error) %7B
+%7D%0A%0A                        %7D%0A                    %7D else // its json already%0A                    %7B%0A                        return saveValue.map(function (option) %7B return option.key %7D).join().split(',');
 %0A       
@@ -3364,16 +3364,17 @@
 function
+ 
 (saveVal
@@ -3375,24 +3375,25 @@
 aveValue) %7B%0A
+%0A
             
@@ -3403,16 +3403,18 @@
      if 
+(!
 (saveVal
@@ -3437,39 +3437,109 @@
 ray)
- // json%0A                    %7B%0A
+) %7B%0A                        try %7B%0A%0A                            saveValue = JSON.parse(saveValue);%0A   
     
@@ -3550,32 +3550,33 @@
                 
+ 
 return saveValue
@@ -3577,16 +3577,19 @@
 eValue;%0A
+%0A  
         
@@ -3592,35 +3592,122 @@
                 
-%7D%0A%0A
+  %7D catch (error) %7B%0A%0A                            //its not json its probably XML.%0A        
                 
@@ -3732,32 +3732,40 @@
                 
+    
+    
 var xml = $.pars
@@ -3801,24 +3801,32 @@
             
+        
 var items = 
@@ -3854,32 +3854,40 @@
                 
+  
+      
 $(xml).find('Pic
@@ -3939,16 +3939,24 @@
         
+       
+ 
 items.pu
@@ -3960,16 +3960,24 @@
 .push(%7B%0A
+        
         
@@ -4060,16 +4060,24 @@
         
+     
+   
 'label':
@@ -4116,24 +4116,32 @@
             
+        
 %7D);%0A        
@@ -4148,37 +4148,53 @@
                 
+   
-%7D);%0A%0A
+     %7D);%0A%0A        
                 
@@ -4227,34 +4227,48 @@
                 
-%7D%0A
+        %7D%0A      
                 
@@ -4263,32 +4263,34 @@
                 
+  
 catch (error) %7B%0A
@@ -4280,34 +4280,39 @@
  catch (error) %7B
+%7D
 %0A%0A
+    
                 
@@ -4308,32 +4308,187 @@
                %7D
+%0A                    %7D else // its json already%0A                    %7B%0A                        return saveValue;%0A                    %7D%0A%0A                    
 %0A%0A              
@@ -4551,8 +4551,9 @@
     %7D%0A);
+%0A

FIX for issue #85