Code Duplication    Length = 128-128 lines in 2 locations

controls/js/script-legacy.js 1 location

@@ 1799-1926 (lines=128) @@
1796
		}
1797
	},
1798
1799
	initKirkiControl: function() {
1800
1801
		var control       = this,
1802
		    value         = control.getValue(),
1803
		    saveAs        = ( ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.save_as ) ) ? control.params.choices.save_as : 'url',
1804
		    preview       = control.container.find( '.placeholder, .thumbnail' ),
1805
		    previewImage  = ( 'array' === saveAs ) ? value.url : value,
1806
		    removeButton  = control.container.find( '.image-upload-remove-button' ),
1807
		    defaultButton = control.container.find( '.image-default-button' );
1808
1809
		control.container.find( '.kirki-controls-loading-spinner' ).hide();
1810
1811
		// Tweaks for save_as = id.
1812
		if ( ( 'id' === saveAs || 'ID' === saveAs ) && '' !== value ) {
1813
			wp.media.attachment( value ).fetch().then( function() {
1814
				setTimeout( function() {
1815
					var url = wp.media.attachment( value ).get( 'url' );
1816
					preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + url + '" alt="" />' );
1817
				}, 700 );
1818
			} );
1819
		}
1820
1821
		// If value is not empty, hide the "default" button.
1822
		if ( ( 'url' === saveAs && '' !== value ) || ( 'array' === saveAs && ! _.isUndefined( value.url ) && '' !== value.url ) ) {
1823
			control.container.find( 'image-default-button' ).hide();
1824
		}
1825
1826
		// If value is empty, hide the "remove" button.
1827
		if ( ( 'url' === saveAs && '' === value ) || ( 'array' === saveAs && ( _.isUndefined( value.url ) || '' === value.url ) ) ) {
1828
			removeButton.hide();
1829
		}
1830
1831
		// If value is default, hide the default button.
1832
		if ( value === control.params['default'] ) {
1833
			control.container.find( 'image-default-button' ).hide();
1834
		}
1835
1836
		if ( '' !== previewImage ) {
1837
			preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + previewImage + '" alt="" />' );
1838
		}
1839
1840
		control.container.on( 'click', '.image-upload-button', function( e ) {
1841
			var image = wp.media({ multiple: false }).open().on( 'select', function() {
1842
1843
					// This will return the selected image from the Media Uploader, the result is an object.
1844
					var uploadedImage = image.state().get( 'selection' ).first(),
1845
					    previewImage  = uploadedImage.toJSON().sizes.full.url;
1846
1847
					if ( ! _.isUndefined( uploadedImage.toJSON().sizes.medium ) ) {
1848
						previewImage = uploadedImage.toJSON().sizes.medium.url;
1849
					} else if ( ! _.isUndefined( uploadedImage.toJSON().sizes.thumbnail ) ) {
1850
						previewImage = uploadedImage.toJSON().sizes.thumbnail.url;
1851
					}
1852
1853
					if ( 'array' === saveAs ) {
1854
						control.saveValue( 'id', uploadedImage.toJSON().id );
1855
						control.saveValue( 'url', uploadedImage.toJSON().sizes.full.url );
1856
						control.saveValue( 'width', uploadedImage.toJSON().width );
1857
						control.saveValue( 'height', uploadedImage.toJSON().height );
1858
					} else if ( 'id' === saveAs ) {
1859
						control.saveValue( 'id', uploadedImage.toJSON().id );
1860
					} else {
1861
						control.saveValue( 'url', uploadedImage.toJSON().sizes.full.url );
1862
					}
1863
1864
					if ( preview.length ) {
1865
						preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + previewImage + '" alt="" />' );
1866
					}
1867
					if ( removeButton.length ) {
1868
						removeButton.show();
1869
						defaultButton.hide();
1870
					}
1871
			    });
1872
1873
			e.preventDefault();
1874
		});
1875
1876
		control.container.on( 'click', '.image-upload-remove-button', function( e ) {
1877
1878
			var preview,
1879
			    removeButton,
1880
			    defaultButton;
1881
1882
			e.preventDefault();
1883
1884
			control.saveValue( 'id', '' );
1885
			control.saveValue( 'url', '' );
1886
			control.saveValue( 'width', '' );
1887
			control.saveValue( 'height', '' );
1888
1889
			preview       = control.container.find( '.placeholder, .thumbnail' );
1890
			removeButton  = control.container.find( '.image-upload-remove-button' );
1891
			defaultButton = control.container.find( '.image-default-button' );
1892
1893
			if ( preview.length ) {
1894
				preview.removeClass().addClass( 'placeholder' ).html( 'No file selected' );
1895
			}
1896
			if ( removeButton.length ) {
1897
				removeButton.hide();
1898
				if ( jQuery( defaultButton ).hasClass( 'button' ) ) {
1899
					defaultButton.show();
1900
				}
1901
			}
1902
		});
1903
1904
		control.container.on( 'click', '.image-default-button', function( e ) {
1905
1906
			var preview,
1907
			    removeButton,
1908
			    defaultButton;
1909
1910
			e.preventDefault();
1911
1912
			control.saveValue( 'url', control.params['default'] );
1913
1914
			preview       = control.container.find( '.placeholder, .thumbnail' );
1915
			removeButton  = control.container.find( '.image-upload-remove-button' );
1916
			defaultButton = control.container.find( '.image-default-button' );
1917
1918
			if ( preview.length ) {
1919
				preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + control.params['default'] + '" alt="" />' );
1920
			}
1921
			if ( removeButton.length ) {
1922
				removeButton.show();
1923
				defaultButton.hide();
1924
			}
1925
		});
1926
	},
1927
1928
	/**
1929
	 * Gets the value.

controls/js/script.js 1 location

@@ 1772-1899 (lines=128) @@
1769
		}
1770
	},
1771
1772
	initKirkiControl: function() {
1773
1774
		var control       = this,
1775
		    value         = control.getValue(),
1776
		    saveAs        = ( ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.save_as ) ) ? control.params.choices.save_as : 'url',
1777
		    preview       = control.container.find( '.placeholder, .thumbnail' ),
1778
		    previewImage  = ( 'array' === saveAs ) ? value.url : value,
1779
		    removeButton  = control.container.find( '.image-upload-remove-button' ),
1780
		    defaultButton = control.container.find( '.image-default-button' );
1781
1782
		control.container.find( '.kirki-controls-loading-spinner' ).hide();
1783
1784
		// Tweaks for save_as = id.
1785
		if ( ( 'id' === saveAs || 'ID' === saveAs ) && '' !== value ) {
1786
			wp.media.attachment( value ).fetch().then( function() {
1787
				setTimeout( function() {
1788
					var url = wp.media.attachment( value ).get( 'url' );
1789
					preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + url + '" alt="" />' );
1790
				}, 700 );
1791
			} );
1792
		}
1793
1794
		// If value is not empty, hide the "default" button.
1795
		if ( ( 'url' === saveAs && '' !== value ) || ( 'array' === saveAs && ! _.isUndefined( value.url ) && '' !== value.url ) ) {
1796
			control.container.find( 'image-default-button' ).hide();
1797
		}
1798
1799
		// If value is empty, hide the "remove" button.
1800
		if ( ( 'url' === saveAs && '' === value ) || ( 'array' === saveAs && ( _.isUndefined( value.url ) || '' === value.url ) ) ) {
1801
			removeButton.hide();
1802
		}
1803
1804
		// If value is default, hide the default button.
1805
		if ( value === control.params['default'] ) {
1806
			control.container.find( 'image-default-button' ).hide();
1807
		}
1808
1809
		if ( '' !== previewImage ) {
1810
			preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + previewImage + '" alt="" />' );
1811
		}
1812
1813
		control.container.on( 'click', '.image-upload-button', function( e ) {
1814
			var image = wp.media({ multiple: false }).open().on( 'select', function() {
1815
1816
					// This will return the selected image from the Media Uploader, the result is an object.
1817
					var uploadedImage = image.state().get( 'selection' ).first(),
1818
					    previewImage  = uploadedImage.toJSON().sizes.full.url;
1819
1820
					if ( ! _.isUndefined( uploadedImage.toJSON().sizes.medium ) ) {
1821
						previewImage = uploadedImage.toJSON().sizes.medium.url;
1822
					} else if ( ! _.isUndefined( uploadedImage.toJSON().sizes.thumbnail ) ) {
1823
						previewImage = uploadedImage.toJSON().sizes.thumbnail.url;
1824
					}
1825
1826
					if ( 'array' === saveAs ) {
1827
						control.saveValue( 'id', uploadedImage.toJSON().id );
1828
						control.saveValue( 'url', uploadedImage.toJSON().sizes.full.url );
1829
						control.saveValue( 'width', uploadedImage.toJSON().width );
1830
						control.saveValue( 'height', uploadedImage.toJSON().height );
1831
					} else if ( 'id' === saveAs ) {
1832
						control.saveValue( 'id', uploadedImage.toJSON().id );
1833
					} else {
1834
						control.saveValue( 'url', uploadedImage.toJSON().sizes.full.url );
1835
					}
1836
1837
					if ( preview.length ) {
1838
						preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + previewImage + '" alt="" />' );
1839
					}
1840
					if ( removeButton.length ) {
1841
						removeButton.show();
1842
						defaultButton.hide();
1843
					}
1844
			    });
1845
1846
			e.preventDefault();
1847
		});
1848
1849
		control.container.on( 'click', '.image-upload-remove-button', function( e ) {
1850
1851
			var preview,
1852
			    removeButton,
1853
			    defaultButton;
1854
1855
			e.preventDefault();
1856
1857
			control.saveValue( 'id', '' );
1858
			control.saveValue( 'url', '' );
1859
			control.saveValue( 'width', '' );
1860
			control.saveValue( 'height', '' );
1861
1862
			preview       = control.container.find( '.placeholder, .thumbnail' );
1863
			removeButton  = control.container.find( '.image-upload-remove-button' );
1864
			defaultButton = control.container.find( '.image-default-button' );
1865
1866
			if ( preview.length ) {
1867
				preview.removeClass().addClass( 'placeholder' ).html( 'No file selected' );
1868
			}
1869
			if ( removeButton.length ) {
1870
				removeButton.hide();
1871
				if ( jQuery( defaultButton ).hasClass( 'button' ) ) {
1872
					defaultButton.show();
1873
				}
1874
			}
1875
		});
1876
1877
		control.container.on( 'click', '.image-default-button', function( e ) {
1878
1879
			var preview,
1880
			    removeButton,
1881
			    defaultButton;
1882
1883
			e.preventDefault();
1884
1885
			control.saveValue( 'url', control.params['default'] );
1886
1887
			preview       = control.container.find( '.placeholder, .thumbnail' );
1888
			removeButton  = control.container.find( '.image-upload-remove-button' );
1889
			defaultButton = control.container.find( '.image-default-button' );
1890
1891
			if ( preview.length ) {
1892
				preview.removeClass().addClass( 'thumbnail thumbnail-image' ).html( '<img src="' + control.params['default'] + '" alt="" />' );
1893
			}
1894
			if ( removeButton.length ) {
1895
				removeButton.show();
1896
				defaultButton.hide();
1897
			}
1898
		});
1899
	},
1900
1901
	/**
1902
	 * Gets the value.