/** * @inheritdoc */ public function init() { static::$instance = $this; $this->_varName = Inflector::variablize($this->name); $this->requires[] = 'ui.router'; $this->requires = array_unique($this->requires); $view = $this->getView(); // Asset Dependency $am = Yii::$app->getAssetManager(); $key = md5(serialize([__CLASS__, Yii::$app->controller->route, $this->name])); $bundle = ['basePath' => '', 'depends' => [AngularAsset::className()], 'js' => []]; foreach ($this->requires as $module) { if (isset(AngularAsset::$assetMap[$module])) { $bundle['depends'][] = AngularAsset::$assetMap[$module]; } } $js = $this->generate(); if ($this->remote) { $path = sprintf('%x', crc32($key)); $jsName = Inflector::camel2id($this->name) . '.js'; $bundle['js'][] = $jsName; $bundle['basePath'] = $am->basePath . '/' . $path; $bundle['baseUrl'] = $am->baseUrl . '/' . $path; FileHelper::createDirectory(Yii::getAlias($bundle['basePath'])); file_put_contents(Yii::getAlias($bundle['basePath'] . '/' . $jsName), $js); } else { $view->registerJs($js, WebView::POS_END); } $am->bundles[$key] = new AssetBundle($bundle); $view->registerAssetBundle($key); $options = $this->options; if ($this->tag !== 'ui-view' && !isset($options['ui-view'])) { $options['ui-view'] = true; } if ($this->ngApp && !isset($options['ng-app'])) { $options['ng-app'] = $this->name; } echo Html::beginTag($this->tag, $options); }
/** * @inheritdoc */ public function run() { $view = $this->getView(); if (!$this->remote || Yii::$app->getRequest()->get($this->queryParam) === 'script') { $js = $this->generate(); if ($this->remote) { $response = Yii::$app->getResponse(); $response->format = 'raw'; $response->getHeaders()->set('Content-Type', 'application/javascript'); $response->content = $js; $response->send(); Yii::$app->end(); } else { $view->registerJs($js, WebView::POS_END); } } // Asset Dependency $key = md5(Yii::$app->controller->route . $this->name); $bundle = ['baseUrl' => '', 'depends' => [AngularAsset::className()], 'js' => []]; foreach ($this->requires as $module) { if (isset(static::$requireAssets[$module])) { $bundle['depends'][] = static::$requireAssets[$module]; } } if ($this->remote) { $key = md5(Yii::$app->controller->route . $this->name); $url = Url::current([$this->queryParam => 'script']); $bundle['js'][] = strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/'); } Yii::$app->getAssetManager()->bundles[$key] = new AssetBundle($bundle); $view->registerAssetBundle($key); static::$instance = null; return Html::tag($this->tag, '', ['ng-app' => $this->useNgApp ? $this->name : false, 'ng-view' => $this->tag != 'ng-view']); }
/** * Render script create module. The result are * ```javascript * module = angular.module('appName',[requires,...]); * ``` */ protected function renderModule() { $view = $this->getView(); if (!empty($this->resources)) { $this->requires = array_unique(array_merge($this->requires, ['ngResource'])); } $requires = array_unique(array_merge(['ngRoute'], $this->requires)); AngularAsset::register($view); foreach ($requires as $module) { if (isset(static::$requireAssets[$module])) { $class = static::$requireAssets[$module]; $class::register($view); } } $js = "var module = angular.module('{$this->name}'," . Json::htmlEncode($requires) . ");\n" . "var {$this->_varName} = module;"; return $js; }
/** * Render script create module. The result are * ```javascript * appName = angular.module('appName',[requires,...]); * ``` */ protected function renderModule() { $view = $this->getView(); $requires = array_unique(array_merge(['ngRoute'], $this->requires)); AngularAsset::register($view); foreach ($requires as $module) { if (isset(static::$requireAssets[$module])) { $class = static::$requireAssets[$module]; $class::register($view); } } $js = "{$this->_varName} = angular.module('{$this->name}'," . Json::htmlEncode($requires) . ");"; $view->registerJs($js, View::POS_END); if ($this->jsFile !== null) { $this->renderJs($this->jsFile); } }