Merge pull request #19461 from owncloud/reuse_code

reuse code
This commit is contained in:
Thomas Müller 2015-12-03 13:55:50 +01:00
commit 2d1cc8aaeb
2 changed files with 129 additions and 43 deletions

View File

@ -457,19 +457,12 @@ class OC_Util {
*/
public static function addScript($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'js', $file);
//TODO eliminate double code
if (!in_array($path, self::$scripts)) {
// core js files need separate handling
if ($application !== 'core' && $file !== null) {
self::addTranslations($application);
}
if ($prepend===true) {
array_unshift(self::$scripts, $path);
}
else {
self::$scripts[] = $path;
}
// core js files need separate handling
if ($application !== 'core' && $file !== null) {
self::addTranslations ( $application );
}
self::addExternalResource($application, $prepend, $path, "script");
}
/**
@ -482,14 +475,7 @@ class OC_Util {
*/
public static function addVendorScript($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'vendor', $file);
//TODO eliminate double code
if (! in_array ( $path, self::$scripts )) {
if ($prepend === true) {
array_unshift ( self::$scripts, $path );
} else {
self::$scripts [] = $path;
}
}
self::addExternalResource($application, $prepend, $path, "script");
}
/**
@ -508,14 +494,7 @@ class OC_Util {
} else {
$path = "l10n/$languageCode";
}
//TODO eliminate double code
if (!in_array($path, self::$scripts)) {
if ($prepend === true) {
array_unshift ( self::$scripts, $path );
} else {
self::$scripts [] = $path;
}
}
self::addExternalResource($application, $prepend, $path, "script");
}
/**
@ -528,14 +507,7 @@ class OC_Util {
*/
public static function addStyle($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'css', $file);
//TODO eliminate double code
if (!in_array($path, self::$styles)) {
if ($prepend === true) {
array_unshift ( self::$styles, $path );
} else {
self::$styles[] = $path;
}
}
self::addExternalResource($application, $prepend, $path, "style");
}
/**
@ -548,13 +520,36 @@ class OC_Util {
*/
public static function addVendorStyle($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'vendor', $file);
//TODO eliminate double code
if (!in_array($path, self::$styles)) {
if ($prepend === true) {
array_unshift ( self::$styles, $path );
} else {
self::$styles[] = $path;
}
self::addExternalResource($application, $prepend, $path, "style");
}
/**
* add an external resource css/js file
*
* @param string $application application id
* @param bool $prepend prepend the file to the beginning of the list
* @param string $path
* @param string $type (script or style)
* @return void
*/
private static function addExternalResource($application, $prepend, $path, $type = "script") {
if ($type === "style") {
if (!in_array($path, self::$styles)) {
if ($prepend === true) {
array_unshift ( self::$styles, $path );
} else {
self::$styles[] = $path;
}
}
} elseif ($type === "script") {
if (!in_array($path, self::$scripts)) {
if ($prepend === true) {
array_unshift ( self::$scripts, $path );
} else {
self::$scripts [] = $path;
}
}
}
}

View File

@ -401,6 +401,97 @@ class Test_Util extends \Test\TestCase {
$this->assertNotEmpty($errors);
}
}
protected function setUp() {
parent::setUp();
\OC_Util::$scripts = [];
\OC_Util::$styles = [];
}
protected function tearDown() {
parent::tearDown();
\OC_Util::$scripts = [];
\OC_Util::$styles = [];
}
public function testAddScript() {
\OC_Util::addScript('core', 'myFancyJSFile1');
\OC_Util::addScript('myApp', 'myFancyJSFile2');
\OC_Util::addScript('core', 'myFancyJSFile0', true);
\OC_Util::addScript('core', 'myFancyJSFile10', true);
// add duplicate
\OC_Util::addScript('core', 'myFancyJSFile1');
$this->assertEquals([
'core/js/myFancyJSFile10',
'core/js/myFancyJSFile0',
'core/js/myFancyJSFile1',
'myApp/l10n/en',
'myApp/js/myFancyJSFile2',
], \OC_Util::$scripts);
$this->assertEquals([], \OC_Util::$styles);
}
public function testAddVendorScript() {
\OC_Util::addVendorScript('core', 'myFancyJSFile1');
\OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
\OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
\OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
// add duplicate
\OC_Util::addVendorScript('core', 'myFancyJSFile1');
$this->assertEquals([
'core/vendor/myFancyJSFile10',
'core/vendor/myFancyJSFile0',
'core/vendor/myFancyJSFile1',
'myApp/vendor/myFancyJSFile2',
], \OC_Util::$scripts);
$this->assertEquals([], \OC_Util::$styles);
}
public function testAddTranslations() {
\OC_Util::addTranslations('appId', 'de');
$this->assertEquals([
'appId/l10n/de'
], \OC_Util::$scripts);
$this->assertEquals([], \OC_Util::$styles);
}
public function testAddStyle() {
\OC_Util::addStyle('core', 'myFancyCSSFile1');
\OC_Util::addStyle('myApp', 'myFancyCSSFile2');
\OC_Util::addStyle('core', 'myFancyCSSFile0', true);
\OC_Util::addStyle('core', 'myFancyCSSFile10', true);
// add duplicate
\OC_Util::addStyle('core', 'myFancyCSSFile1');
$this->assertEquals([], \OC_Util::$scripts);
$this->assertEquals([
'core/css/myFancyCSSFile10',
'core/css/myFancyCSSFile0',
'core/css/myFancyCSSFile1',
'myApp/css/myFancyCSSFile2',
], \OC_Util::$styles);
}
public function testAddVendorStyle() {
\OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
\OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
\OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
\OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true);
// add duplicate
\OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
$this->assertEquals([], \OC_Util::$scripts);
$this->assertEquals([
'core/vendor/myFancyCSSFile10',
'core/vendor/myFancyCSSFile0',
'core/vendor/myFancyCSSFile1',
'myApp/vendor/myFancyCSSFile2',
], \OC_Util::$styles);
}
}
/**