コード例 #1
0
ファイル: UsonMainTest.cs プロジェクト: Qorpent/qorpent.sys
        public void ArrayDefaults()
        {
            dynamic uobj  = new UObj();
            dynamic uobj2 = new UObj();

            uobj.push(null, 1, null, 2, null, 3);
            uobj2.push(-1, 2, -2, null, -3, 4);
            uobj.defaults(uobj2);
            Console.WriteLine(uobj.ToJson());
            Assert.AreEqual(@"[-1,1,-2,2,-3,3]", uobj.ToJson());
        }
コード例 #2
0
        private string GenerateSingleController(IBSharpClass targetclass)
        {
            var xml      = targetclass.Compiled;
            var code     = xml.Attr("fullcode").Split('.').Last();
            var sb       = new StringBuilder();
            var deps     = new Dictionary <string, XElement>();
            var advanced = new StringBuilder();

            if (xml.Elements().Any(_ => _.GetSmartValue("persistent", "persistentCode").ToBool()) && deps.All(_ => _.Value.Attr("type") != "settings"))
            {
                deps["settings"] = new XElement("service", new XAttribute("type", "settings"), new XAttribute("before", "true"));
            }
            foreach (var s in xml.Elements("service"))
            {
                var scode = s.Attr("code");
                deps[scode] = s;
            }
            var deplist = string.Join("','", deps.Values.Select(_ => _.ChooseAttr("type", "code")).Distinct().OrderBy(_ => _));

            if (!string.IsNullOrWhiteSpace(deplist))
            {
                deplist = ", '" + deplist + "'";
            }
            var calllist = string.Join(",", deps.Values.Select(_ => _.ChooseAttr("type", "code")).Distinct().OrderBy(_ => _));

            if (!string.IsNullOrWhiteSpace(calllist))
            {
                calllist = ", " + calllist;
            }
            sb.Append(string.Format(@"
        .controller('{3}_{0}', ['$scope','$http','$rootScope'{1},'$element', function ($scope, $http, $rootScope{2},$element) {{ 
                $scope.api = Api($http, $rootScope);
				$scope.$services = {{}};
				$scope.$services.$element = $element;
				$scope.$services.$http = $http;
				$scope.$services.$rootScope = $rootScope;
", code, deplist, calllist, Project.ProjectName));
            foreach (var dep in deps)
            {
                sb.Append("\t\t\t\t$scope.$services." + dep.Key + "=" + dep.Key + ";");
                sb.AppendLine();
            }
            sb.AppendFormat(
                @"				$scope.{2} = '{0}.html';
				$scope.title= '{1}';
",
                targetclass.Compiled.ChooseAttr("view", "code"),
                targetclass.Compiled.ChooseAttr("name", "code"),
                (targetclass.Compiled.Element("menu") == null)?"view":"_view"
                );



            var items = xml.Elements("item");

            foreach (var e in deps.Where(_ => _.Value.Attr("before").ToBool()).OrderBy(_ => _.Key))
            {
                var type = e.Value.ChooseAttr("type", "code");
                UnifyPersistentCode(targetclass, e);
                if (type == "refresh")
                {
                    SetupRefresh(targetclass, e, advanced);
                }
                sb.AppendLine("\t\t\t\t" + type + "($scope," + e.Value.ToJson() + ");");
            }
            foreach (var item in items.OrderBy(_ => _.Attr("code")))
            {
                var type    = item.Attr("type");
                var typestr = "{}";
                var watcher = "";
                if (!string.IsNullOrWhiteSpace(type))
                {
                    var persistent = item.GetSmartValue("persistent", "persistentCode");
                    if (persistent == "1")
                    {
                        persistent = Project.ProjectName + "-" + xml.Attr("code") + "-" + item.Attr("code");
                    }

                    dynamic ext        = new UObj();
                    var     parameters = item.Element("parameters");
                    if (null != parameters)
                    {
                        foreach (var a in parameters.Attributes())
                        {
                            ext[a.Name.LocalName] = a.Value;
                        }
                    }
                    string ctor = ext.ToJson(UObjSerializeMode.Javascript);
                    if (!string.IsNullOrWhiteSpace(persistent))
                    {
                        ctor = "$.extend(" + ctor + ",$scope.settings.get('" + persistent + "'))";
                    }
                    typestr = "new Types." + type.Split('.').Last() + "(" + ctor + ")";
                    watcher = "\t\t\t\t$scope.$watch('" + item.Attr("code") + "',function(n,o){$scope.settings.set('" + persistent +
                              "',n)},true);";
                }
                else if (item.HasElements)
                {
                    var uson = new UObj();
                    foreach (var e in item.Elements())
                    {
                        var u = e.XmlToUson();
                        u.Properties["itemtype"] = e.Name.LocalName;
                        uson.push(u);
                    }

                    typestr = uson.ToJson(UObjSerializeMode.Javascript);
                }


                sb.AppendLine("\t\t\t\t$scope." + item.Attr("code") + " = " + typestr + ";");
                if (!string.IsNullOrWhiteSpace(watcher))
                {
                    sb.AppendLine(watcher);
                }
            }

            foreach (var e in deps.Where(_ => !_.Value.Attr("before").ToBool()).OrderBy(_ => _.Key))
            {
                var type = e.Value.ChooseAttr("type", "code");
                UnifyPersistentCode(targetclass, e);
                if (type == "refresh")
                {
                    SetupRefresh(targetclass, e, advanced);
                }
                sb.AppendLine("\t\t\t\t" + type + "($scope," + e.Value.ToJson() + ");");
            }
            sb.AppendLine(advanced.ToString());

            sb.AppendLine("\t\t}])");


            //sb.AppendLine();
            return(sb.ToString());
        }