@@ -712,26 +712,41 @@ transparency">Transparency</label>\
712
712
} ) ;
713
713
714
714
//Delete the palette when the delete button is clicked
715
- ColorPicker . Object . favDelete . on ( 'click' , function ( ) {
716
-
717
- //First delete the palette while we still have the selected option
718
- ColorPicker . Object . palettes . find ( '[palette=' + ColorPicker . Object . favouriteSelect . val ( ) + ']' ) . remove ( ) ;
719
-
720
- //Delete the array
721
- delete ColorPicker . Object . userColors [ ColorPicker . Object . favouriteSelect . val ( ) ] ;
722
-
723
- //After we remove the palette we can remove the option from the select
724
- ColorPicker . Object . favouriteSelect . find ( ':selected' ) . remove ( ) ;
715
+ ColorPicker . Object . favDelete . on ( 'click' , function ( ) {
716
+ ColorPicker . DeletePalette ( ColorPicker . Object . favouriteSelect . val ( ) ) ;
717
+ } ) ;
718
+
719
+ }
720
+
721
+ static DeletePalette ( name ) {
722
+
723
+ if ( ColorPicker . Object . userColors [ name ] == null ) return ;
724
+
725
+ //First delete the palette while we still have the selected option
726
+ ColorPicker . Object . palettes . find ( '[palette=' + name + ']' ) . remove ( ) ;
725
727
726
- //Set the favourite select back to the old palette
727
- ColorPicker . Object . favouriteSelect . val ( ColorPicker . Object . favouriteSelect . find ( 'option' ) . val ( ) ) ;
728
+ //Delete the array
729
+ delete ColorPicker . Object . userColors [ name ] ;
728
730
729
- //Hide the button
730
- ColorPicker . Object . favDelete . removeClass ( 'show' ) ;
731
+ //After we remove the palette we can remove the option from the select
732
+ ColorPicker . Object . favouriteSelect . find ( ':selected' ) . remove ( ) ;
731
733
734
+ //Set the favourite select back to the old palette
735
+ ColorPicker . Object . favouriteSelect . val ( ColorPicker . Object . favouriteSelect . find ( 'option' ) . val ( ) ) ;
732
736
733
- } ) ;
737
+ //Hide the button
738
+ ColorPicker . Object . favDelete . removeClass ( 'show' ) ;
739
+ }
740
+
741
+ static ClearPalette ( name ) {
742
+ if ( ColorPicker . Object . userColors [ name ] == null ) return ;
743
+
744
+ //First delete the palette while we still have the selected option
745
+ ColorPicker . Object . palettes . find ( '[palette=' + name + ']' ) . html ( '' ) ;
734
746
747
+ //Delete the array and recreate it
748
+ delete ColorPicker . Object . userColors [ name ] ;
749
+ ColorPicker . Object . userColors [ name ] = [ ] ;
735
750
}
736
751
737
752
//Setup events for importing and exporting palettes.
@@ -759,6 +774,73 @@ transparency">Transparency</label>\
759
774
760
775
}
761
776
777
+ static AddPallete ( name , colors ) {
778
+
779
+ if ( colors instanceof String )
780
+ colors = JSON . parse ( colors ) ;
781
+
782
+ if ( Object . keys ( colors ) [ 0 ] == name )
783
+ colors = colors [ name ] ;
784
+
785
+ //attribute checker : just checking to see if the palette exists
786
+ var ac = $ ( '[palette=' + name + ']' ) . attr ( 'palette' ) ;
787
+
788
+ //If there is no palette with the name then we need to create one
789
+ if ( ac == null || ac == 'undefined' ) {
790
+ ColorPicker . CreatePalette ( name ) ;
791
+ } else {
792
+ ColorPicker . ActivateFavourite ( name ) ;
793
+ ColorPicker . ClearPalette ( name ) ;
794
+ }
795
+
796
+ //Overwrite the palette with our new array
797
+ ColorPicker . Object . userColors [ name ] = colors ;
798
+
799
+ //Log it to the console
800
+ console . log ( 'Loaded Palette : ' + name + " | Colors : " + colors . length ) ;
801
+
802
+ //Create all the color elements so the user can select them.
803
+ for ( var i = 0 ; i < colors . length ; i ++ ) {
804
+ ColorPicker . CreateColorElement ( $ ( '[palette=' + name + ']' ) , colors [ i ] ) ;
805
+ }
806
+
807
+ ColorPicker . StorePalette ( name , colors ) ;
808
+ }
809
+
810
+ //Store use palette in local storage
811
+ static StorePalette ( name , colors ) {
812
+ var palObj = localStorage . getItem ( 'palettes' ) ;
813
+ if ( palObj == null )
814
+ palObj = { } ;
815
+ else
816
+ palObj = JSON . parse ( palObj ) ;
817
+
818
+ palObj [ name ] = colors ;
819
+
820
+ var jpalettes = JSON . stringify ( palObj , null , 2 ) ;
821
+
822
+ localStorage . setItem ( 'palettes' , jpalettes ) ;
823
+ }
824
+
825
+ //Store all user palettes in local storage
826
+ static StorePalettes ( ) {
827
+ for ( let key in ColorPicker . Object . userColors ) {
828
+ StorePalette ( name , ColorPicker . Object . userColors [ key ] ) ;
829
+ }
830
+ }
831
+
832
+ static LoadLocalPalettes ( ) {
833
+ var palettes = localStorage . getItem ( 'palettes' ) ;
834
+
835
+ if ( palettes == null ) return ;
836
+
837
+ var palObj = JSON . parse ( palettes ) ;
838
+
839
+ for ( let key in palObj ) {
840
+ ColorPicker . AddPallete ( key , palObj [ key ] ) ;
841
+ }
842
+ }
843
+
762
844
//Imports a .pal file and creates the palette.
763
845
static ImportFavouritePal ( ) {
764
846
@@ -789,28 +871,11 @@ transparency">Transparency</label>\
789
871
//The reason we aren't using JSON.parse is because we want the user to be able to edit
790
872
//and add colors easily, and directly to the .pal file.
791
873
//All they need to do is add a color in web format (rgb, rgba, hex) and seperate them with |
792
- var colors = fr . result . split ( '|' ) ; // JSON.parse(fr.result);
874
+ var colors = JSON . parse ( fr . result ) ;
793
875
794
- //Overwrite the palette with our new array
795
- ColorPicker . Object . userColors [ palette ] = colors ;
876
+ var name = Object . keys ( colors ) [ 0 ] ;
796
877
797
- //attribute checker : just checking to see if the palette exists
798
- var ac = $ ( '[palette=' + palette + ']' ) . attr ( 'palette' ) ;
799
-
800
- //If there is no palette with the name then we need to create one
801
- if ( ac == null || ac == 'undefined' ) {
802
- ColorPicker . CreatePalette ( palette ) ;
803
- } else {
804
- ColorPicker . ActivateFavourite ( palette ) ;
805
- }
806
-
807
- //Log it to the console
808
- console . log ( 'Loaded Palette : ' + palette + " | Colors : " + colors . length ) ;
809
-
810
- //Create all the color elements so the user can select them.
811
- for ( var i = 0 ; i < colors . length ; i ++ ) {
812
- ColorPicker . CreateColorElement ( $ ( '[palette=' + palette + ']' ) , colors [ i ] ) ;
813
- }
878
+ ColorPicker . AddPallete ( name , colors ) ;
814
879
815
880
} ;
816
881
@@ -822,34 +887,39 @@ transparency">Transparency</label>\
822
887
//Exports the current favourite palette as a .pal file.
823
888
static ExportFavouritePal ( ) {
824
889
890
+ //Get name of palette and store
891
+ let name = ColorPicker . Object . favouriteSelect . val ( ) ;
892
+
893
+ //Store palette in temporary object.
894
+ let tempObj = { } ;
895
+ tempObj [ name ] = ColorPicker . Object . userColors [ name ] ;
896
+
825
897
//Create the variable which we will store the array in (as a string)
826
- //The reason we aren't using JSON.stringify is because we want the user to be able to edit
827
- //and add colors easily, and directly to the .pal file.
828
- //All they need to do is add a color in web format (rgb, rgba, hex) and seperate them with |
829
- let dataStr = "" ; //JSON.stringify(ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()]);
898
+ let dataStr = JSON . stringify ( tempObj , null , 2 ) ;
899
+
830
900
831
- //First we need to convert the color array into a JSON string.
901
+ ` //First we need to convert the color array into a JSON string.
832
902
$.each(ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()], function(index, value){
833
903
dataStr += value;
834
904
835
905
//We don't want to add the splitter if its the last one, otherwise on import we will get a blank color
836
906
if(ColorPicker.Object.userColors[ColorPicker.Object.favouriteSelect.val()].length - 1 != index)
837
907
dataStr + '|';
838
- } ) ;
908
+ }); `
839
909
840
- //Then we will encode the json string into a temporary file.
910
+ //Encode the json string into a temporary file.
841
911
let dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent ( dataStr ) ;
842
912
843
- //Now we need to set our hidden links, link to our temporary file
913
+ //Set our Gidden links, and link to our temporary file
844
914
ColorPicker . Object . favExportInput . setAttribute ( 'href' , dataUri ) ;
845
915
846
916
//Create a download attribute that has the name of our file
847
- ColorPicker . Object . favExportInput . setAttribute ( 'download' , ColorPicker . Object . favouriteSelect . val ( ) + '.pal' ) ;
917
+ ColorPicker . Object . favExportInput . setAttribute ( 'download' , name + '.pal' ) ;
848
918
849
919
//Simulate a click on the link to begin the download
850
920
ColorPicker . Object . favExportInput . click ( ) ;
851
921
852
- console . log ( 'Exported Palette : ' + ColorPicker . Object . favouriteSelect . text ( ) + " | Colors : " + ColorPicker . Object . userColors [ ColorPicker . Object . favouriteSelect . val ( ) ] . length ) ;
922
+ console . log ( 'Exported Palette : ' + name + " | Colors : " + tempObj [ name ] . length ) ;
853
923
}
854
924
855
925
//Add or remove current color from the selected palette.
@@ -964,7 +1034,9 @@ transparency">Transparency</label>\
964
1034
965
1035
//Create the element and append it to the palette
966
1036
ColorPicker . CreateColorElement ( $ ( '[palette=' + list + ']' ) , color ) ;
967
- }
1037
+ }
1038
+
1039
+ ColorPicker . StorePalette ( list , ColorPicker . Object . userColors [ list ] ) ;
968
1040
}
969
1041
970
1042
}
@@ -988,8 +1060,10 @@ transparency">Transparency</label>\
988
1060
ColorPicker . Object . userColors [ list ] . splice ( index , 1 ) ;
989
1061
//ColorPicker.Object.userColors[list].splice(ColorPicker.Object.userColors[list].indexOf(color), 1);
990
1062
//$('[palette='+list+']').find('[style*="background-color: '+ color +';"]').remove();
991
- $ ( '[value="' + color + '"]' ) . remove ( ) ;
1063
+ //$('#cp_palettes [value="'+color+'"]').remove();
1064
+ $ ( '#cp_palettes [palette="' + list + '"]' ) . children ( ) . eq ( index ) . remove ( ) ;
992
1065
//$('[palette='+list+']').find('[style*="box-shadow: '+ color +' '+ ColorPicker.Object.boxshadow_overlay +';"], [value="'+color+'"]').remove();
1066
+ ColorPicker . StorePalette ( list , ColorPicker . Object . userColors [ list ] ) ;
993
1067
}
994
1068
995
1069
/*
@@ -2174,6 +2248,11 @@ transparency">Transparency</label>\
2174
2248
//ColorPicker.CreateColorElement($('[palette=simple]'), Colors["simple"][i]);
2175
2249
}
2176
2250
2251
+ //Load user palettes
2252
+ ColorPicker . LoadLocalPalettes ( ) ;
2253
+
2254
+ ColorPicker . ActivateFavourite ( 'favourite' ) ;
2255
+
2177
2256
}
2178
2257
2179
2258
0 commit comments