﻿
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)) {
                        try {

                            saveValue = JSON.parse(saveValue);
                            return saveValue.map(function (option) { return option.key }).join().split(',');

                        } catch (error) {

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

                                return keys;
                            }
                            catch (error) {}

                        }
                    } else // its json already
                    {
                        return saveValue.map(function (option) { return option.key }).join().split(',');
                    }

                    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)) {
                        try {

                            saveValue = JSON.parse(saveValue);
                            return saveValue;

                        } catch (error) {

                            //its not json its probably XML.
                            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) {}

                        }
                    } else // its json already
                    {
                        return saveValue;
                    }

                    

                    return null;
                }
            };
        }
);

@@ -396,36 +396,16 @@
 ions) %7B%0A
-                    
 %0A       
@@ -1922,17 +1922,16 @@
 default:
- 
 %0A       
@@ -2110,34 +2110,32 @@
              if 
-(!
 (saveValue insta
@@ -2142,33 +2142,33 @@
 nceof Array)
-) %7B%0A     
+ // json%0A
             
@@ -2178,82 +2178,11 @@
     
-try %7B%0A%0A                            saveValue = JSON.parse(saveValue);%0A    
+ %7B%0A
     
@@ -2282,21 +2282,16 @@
 t(',');%0A
-%0A    
         
@@ -2307,95 +2307,10 @@
    %7D
- catch (error) %7B%0A%0A                            //its not json its probably XML.%0A        
+%0A%0A
     
@@ -2327,32 +2327,24 @@
       try %7B%0A
-        
             
@@ -2408,32 +2408,24 @@
             
-      
-  
 var keys = n
@@ -2432,32 +2432,24 @@
 ew Array();%0A
-        
             
@@ -2529,24 +2529,16 @@
         
-    
-    
 keys.pus
@@ -2581,32 +2581,24 @@
             
-        
 %7D);%0A%0A       
@@ -2610,32 +2610,24 @@
             
- 
-       
 return keys;
@@ -2647,34 +2647,18 @@
         
-       
- 
 %7D%0A
-        
         
@@ -2688,210 +2688,8 @@
 r) %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   
@@ -2921,33 +2921,32 @@
 n (saveValue) %7B%0A
-%0A
                 
@@ -2956,10 +2956,8 @@
  if 
-(!
 (sav
@@ -2980,25 +2980,25 @@
 f Array)
-) %7B%0A     
+ // json%0A
         
@@ -3012,82 +3012,11 @@
     
-try %7B%0A%0A                            saveValue = JSON.parse(saveValue);%0A    
+ %7B%0A
     
@@ -3053,21 +3053,16 @@
 eValue;%0A
-%0A    
         
@@ -3078,95 +3078,10 @@
    %7D
- catch (error) %7B%0A%0A                            //its not json its probably XML.%0A        
+%0A%0A
     
@@ -3122,32 +3122,24 @@
             
-    
-    
 var xml = $.
@@ -3179,32 +3179,24 @@
             
-        
 var items = 
@@ -3228,32 +3228,24 @@
             
-  
-      
 $(xml).find(
@@ -3301,24 +3301,16 @@
         
-       
- 
 items.pu
@@ -3314,24 +3314,16 @@
 .push(%7B%0A
-        
         
@@ -3406,24 +3406,16 @@
         
-     
-   
 'label':
@@ -3454,32 +3454,24 @@
             
-        
 %7D);%0A        
@@ -3486,37 +3486,21 @@
         
-   
-     %7D);%0A%0A        
+%7D);%0A%0A
         
@@ -3549,32 +3549,18 @@
         
-        %7D%0A      
+%7D%0A
         
@@ -3563,34 +3563,32 @@
                 
-  
 catch (error) %7B%7D
@@ -3586,23 +3586,18 @@
 error) %7B
-%7D
 %0A%0A
-    
         
@@ -3613,163 +3613,8 @@
    %7D
-%0A                    %7D else // its json already%0A                    %7B%0A                        return saveValue;%0A                    %7D%0A%0A                    
 %0A%0A  
@@ -3689,9 +3689,8 @@
     %7D%0A);
-%0A

reset SaveFormatResource.js