Example #1
0
 /**
  * To String
  * @return string
  */
 public function __toString()
 {
     $content = parent::__toString();
     if (zbase_is_angular_template()) {
         $serviceName = zbase_angular_module_servicename($this->getModule(), $this);
         $scopeName = zbase_angular_module_scopename($this->getModule(), $this);
         $content = str_replace('ANGULAR_WIDGET_MODULE_SERVICENAME', $serviceName, $content);
         $content = str_replace('ANGULAR_WIDGET_MODULE_SCOPENAME', $scopeName, $content);
     }
     return $content;
 }
Example #2
0
/**
 * Create a Datatable Angular template and script
 * @param type $module
 * @param type $widget
 * @param type $options
 * @return boolean|array
 */
function zbase_angular_widget_datatable($module, $widget, $options = [])
{
    /**
     * @TODO Cache the output
     */
    $ret = [];
    if (!$module instanceof \Zbase\Module\Module) {
        $module = zbase()->module($module);
    }
    if (!$widget instanceof \Zbase\Widgets\Widget) {
        $widget = zbase()->widget($widget);
    }
    if ($module instanceof \Zbase\Module\Module && $widget instanceof \Zbase\Widgets\Widget) {
        $routeName = $widget->_v('angular.route.name', null);
        $templateFile = $widget->_v('angular.view.file', null);
        $templateFormat = $widget->_v('angular.view.format', null);
        $controllerName = $widget->_v('angular.controller', null);
        $selectedItemUrl = $widget->_v('angular.view.list.url', null);
        $pageTitle = $module->_v('controller.back.action.index.page.title', $module->_v('controller.action.index.page.title', null));
        $templateListingType = $widget->_v('angular.view.list.type', null);
        $dataUrl = zbase_url_from_route($widget->_v('angular.route.name', null));
        $serviceName = zbase_angular_module_servicename($module, $widget);
        $serviceScopeVariable = zbase_angular_module_scopename($module, $widget);
        $ret['scope'] = "\$scope.{$serviceScopeVariable} = {$serviceName};" . "\$scope.{$serviceScopeVariable}Item = \$routeParams.itemId ? {$serviceName}.getSelectedItem() : {};";
        $apiFactory = "\n\t\tapp.factory('{$serviceName}', {$serviceName});\n\t\t{$serviceName}.\$inject = ['\$rootScope', '\$http','\$location'];\n\t\tfunction {$serviceName}(\$rootScope, \$http, \$location)\n\t\t{\n\t\t\tvar service = {};\n\t\t\tservice.items = [];\n\t\t\tservice.busy = false;\n\t\t\tservice.page = 0;\n\t\t\tservice.maxPage = 1;\n\t\t\tservice.selectedItem = null;\n\t\t\tservice.nextPage = nextPage;\n\t\t\tservice.getSelectedItem = getSelectedItem;\n\t\t\tservice.setSelectedItem = setSelectedItem;\n\t\t\tservice.updateSelectedItem = updateSelectedItem;\n\t\t\tservice.deleteSelectedItem = deleteSelectedItem;\n\t\t\treturn service;\n\t\t\tfunction nextPage(){\n\t\t\t\tif(service.page == service.maxPage)\n\t\t\t\t{\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif (service.busy)\n\t\t\t\t{\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tif(service.page == 0)\n\t\t\t\t{\n\t\t\t\t\t\$rootScope.loading = true;\n\t\t\t\t}\n\t\t\t\tservice.busy = true;\n\t\t\t\t\$http.jsonp('{$dataUrl}?page=' + (service.page + 1) + '&jsonp=JSON_CALLBACK&angular=1').success(function (data) {\n\t\t\t\t\tif(data.{$serviceName} !== undefined)\n\t\t\t\t\t{\n\t\t\t\t\t\tif(data.{$serviceName}.rows !== undefined)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tvar items = data.{$serviceName}.rows;\n\t\t\t\t\t\t\tfor (var i = 0; i < items.length; i++)\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tservice.items.push(items[i]);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tservice.page = data.{$serviceName}.page;\n\t\t\t\t\t\tservice.maxPage = data.{$serviceName}.maxPage;\n\t\t\t\t\t}\n\t\t\t\t\t\$rootScope.loading = false;\n\t\t\t\t\tservice.busy = false;\n\t\t\t\t});\n\t\t\t}\n\t\t\tfunction getSelectedItem()\n\t\t\t{\n\t\t\t\tif(service.selectedItem === null){\n\t\t\t\t\t\$location.path('/users/');\n\t\t\t\t}\n\t\t\t\treturn service.selectedItem;\n\t\t\t}\n\t\t\tfunction setSelectedItem(item)\n\t\t\t{\n\t\t\t\tservice.selectedItem = item;\n\t\t\t\t\$location.path('{$selectedItemUrl}' + item.id);\n\t\t\t\tif(item.viewTitle !== undefined)\n\t\t\t\t{\n\t\t\t\t\t\$rootScope.viewTitle = item.viewTitle;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfunction updateSelectedItem()\n\t\t\t{\n\t\t\t\tconsole.log('updateSelectedItem');\n\t\t\t}\n\t\t\tfunction deleteSelectedItem()\n\t\t\t{\n\t\t\t\tconsole.log('deleteSelectedItem');\n\t\t\t}\n\t\t}";
        if (!empty($controllerName)) {
            if (!empty($templateFile)) {
                $templateString = zbase_view_render($templateFile, ['index' => $serviceName . 'Item']);
            }
            if (!empty($templateFormat)) {
                $templateString = str_replace('APINAME', 'item', $templateFormat);
            }
            $templateListingLink = str_replace('APINAME', 'item', $widget->_v('angular.view.list.link', '#'));
            $template = '<div ui-content-for="title">
								<span>' . $pageTitle . '</span>
							</div>
							<div class="scrollable" ng-controller="' . $controllerName . '">
								<div class="scrollable-content" ui-scroll-bottom="' . $serviceScopeVariable . '.nextPage()" infinite-scroll="' . $serviceScopeVariable . '.nextPage()" infinite-scroll-disabled="' . $serviceScopeVariable . '.busy" infinite-scroll-distance="1">

									<div class="list-group">
										<div ng-repeat="item in ' . $serviceScopeVariable . '.items">
											<a href="" class="list-group-item ng-binding ng-scope" ng-click="' . $serviceScopeVariable . '.setSelectedItem(item)">
												' . $templateString . '
												<i class="fa fa-chevron-right pull-right"></i>
											</a>
										</div>
									</div>
									<div ng-show="' . $serviceScopeVariable . '.busy" class="zbase-datatable-loader">Loading data...</div>
								</div>
							</div>';
            $ret['template'] = $template;
        }
        $ret['factory'] = $apiFactory;
        $ret['serviceName'] = $serviceName;
        $ret['serviceScopeVariable'] = $serviceScopeVariable;
        $ret['serviceGetSelectedItem'] = $serviceScopeVariable . '.getSelectedItem()';
        return $ret;
    }
    return false;
}