reuse code

This commit is contained in:
Individual IT Services 2015-09-30 09:54:29 +05:45 committed by Morris Jobke
parent 79bbda994b
commit 1835462ec4
1 changed files with 38 additions and 43 deletions

View File

@ -457,19 +457,12 @@ class OC_Util {
*/ */
public static function addScript($application, $file = null, $prepend = false) { public static function addScript($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'js', $file); $path = OC_Util::generatePath($application, 'js', $file);
//TODO eliminate double code
if (!in_array($path, self::$scripts)) {
// core js files need separate handling // core js files need separate handling
if ($application !== 'core' && $file !== null) { if ($application !== 'core' && $file !== null) {
self::addTranslations ( $application ); self::addTranslations ( $application );
} }
if ($prepend===true) { self::addExternalResource($application, $prepend, $path, "script");
array_unshift(self::$scripts, $path);
}
else {
self::$scripts[] = $path;
}
}
} }
/** /**
@ -482,14 +475,7 @@ class OC_Util {
*/ */
public static function addVendorScript($application, $file = null, $prepend = false) { public static function addVendorScript($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'vendor', $file); $path = OC_Util::generatePath($application, 'vendor', $file);
//TODO eliminate double code self::addExternalResource($application, $prepend, $path, "script");
if (! in_array ( $path, self::$scripts )) {
if ($prepend === true) {
array_unshift ( self::$scripts, $path );
} else {
self::$scripts [] = $path;
}
}
} }
/** /**
@ -508,14 +494,7 @@ class OC_Util {
} else { } else {
$path = "l10n/$languageCode"; $path = "l10n/$languageCode";
} }
//TODO eliminate double code self::addExternalResource($application, $prepend, $path, "script");
if (!in_array($path, self::$scripts)) {
if ($prepend === true) {
array_unshift ( self::$scripts, $path );
} else {
self::$scripts [] = $path;
}
}
} }
/** /**
@ -528,14 +507,7 @@ class OC_Util {
*/ */
public static function addStyle($application, $file = null, $prepend = false) { public static function addStyle($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'css', $file); $path = OC_Util::generatePath($application, 'css', $file);
//TODO eliminate double code self::addExternalResource($application, $prepend, $path, "style");
if (!in_array($path, self::$styles)) {
if ($prepend === true) {
array_unshift ( self::$styles, $path );
} else {
self::$styles[] = $path;
}
}
} }
/** /**
@ -548,7 +520,21 @@ class OC_Util {
*/ */
public static function addVendorStyle($application, $file = null, $prepend = false) { public static function addVendorStyle($application, $file = null, $prepend = false) {
$path = OC_Util::generatePath($application, 'vendor', $file); $path = OC_Util::generatePath($application, 'vendor', $file);
//TODO eliminate double code 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 (!in_array($path, self::$styles)) {
if ($prepend === true) { if ($prepend === true) {
array_unshift ( self::$styles, $path ); array_unshift ( self::$styles, $path );
@ -556,6 +542,15 @@ class OC_Util {
self::$styles[] = $path; self::$styles[] = $path;
} }
} }
} elseif ($type === "script") {
if (!in_array($path, self::$scripts)) {
if ($prepend === true) {
array_unshift ( self::$scripts, $path );
} else {
self::$scripts [] = $path;
}
}
}
} }
/** /**