当前位置: 首页>>代码示例>>Golang>>正文


Golang admin.RegisterViewPath函数代码示例

本文整理汇总了Golang中github.com/qor/qor/admin.RegisterViewPath函数的典型用法代码示例。如果您正苦于以下问题:Golang RegisterViewPath函数的具体用法?Golang RegisterViewPath怎么用?Golang RegisterViewPath使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了RegisterViewPath函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ConfigureQorResource

func (publish *Publish) ConfigureQorResource(res resource.Resourcer) {
	if res, ok := res.(*admin.Resource); ok {
		for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
			admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/qor/publish/views"))
		}
		res.UseTheme("publish")

		if event := res.GetAdmin().GetResource("PublishEvent"); event == nil {
			eventResource := res.GetAdmin().AddResource(&PublishEvent{}, &admin.Config{Invisible: true})
			eventResource.IndexAttrs("Name", "Description", "CreatedAt")
		}

		controller := publishController{publish}
		router := res.GetAdmin().GetRouter()
		router.Get(fmt.Sprintf("/%v/diff/:publish_unique_key", res.ToParam()), controller.Diff)
		router.Get(res.ToParam(), controller.Preview)
		router.Post(res.ToParam(), controller.PublishOrDiscard)

		res.GetAdmin().RegisterFuncMap("publish_unique_key", func(res *admin.Resource, record interface{}, context *admin.Context) string {
			return fmt.Sprintf("%s__%v", res.ToParam(), context.GetDB().NewScope(record).PrimaryKeyValue())
		})

		res.GetAdmin().RegisterFuncMap("is_publish_event_resource", func(res *admin.Resource) bool {
			return IsPublishEvent(res.Value)
		})
	}
}
开发者ID:8legd,项目名称:qor-qor,代码行数:27,代码来源:controller.go

示例2: InjectQorAdmin

func (s *Sorting) InjectQorAdmin(res *admin.Resource) {
	Admin := res.GetAdmin()
	res.UseTheme("sorting")

	if res.Config.Permission == nil {
		res.Config.Permission = roles.NewPermission()
	}

	if !injected {
		injected = true
		for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
			admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/qor/sorting/views"))
		}
	}

	role := res.Config.Permission.Role
	if _, ok := role.Get("sorting_mode"); !ok {
		role.Register("sorting_mode", func(req *http.Request, currentUser qor.CurrentUser) bool {
			return req.URL.Query().Get("sorting") != ""
		})
	}

	if res.GetMeta("Position") == nil {
		res.Meta(&admin.Meta{
			Name: "Position",
			Valuer: func(value interface{}, ctx *qor.Context) interface{} {
				db := ctx.GetDB()
				var count int
				var pos = value.(sortingInterface).GetPosition()

				if _, ok := modelValue(value).(sortingDescInterface); ok {
					if total, ok := db.Get("sorting_total_count"); ok {
						count = total.(int)
					} else {
						db.New().Model(modelValue(value)).Count(&count)
					}
					pos = count - pos + 1
				}

				primaryKey := ctx.GetDB().NewScope(value).PrimaryKeyValue()
				url := path.Join(ctx.Request.URL.Path, fmt.Sprintf("%v", primaryKey), "sorting/update_position")
				return template.HTML(fmt.Sprintf("<input type=\"number\" class=\"qor-sorting-position\" value=\"%v\" data-sorting-url=\"%v\" data-position=\"%v\">", pos, url, pos))
			},
			Permission: roles.Allow(roles.Read, "sorting_mode"),
		})
	}

	var attrs []string
	for _, attr := range res.IndexAttrs() {
		if attr != "Position" {
			attrs = append(attrs, attr)
		}
	}
	res.IndexAttrs(append(attrs, "Position")...)

	router := Admin.GetRouter()
	router.Post(fmt.Sprintf("^/%v/\\d+/sorting/update_position$", res.ToParam()), updatePosition)
}
开发者ID:pusparajm,项目名称:qor,代码行数:58,代码来源:controller.go

示例3: ConfigureQorMetaBeforeInitialize

func (Base) ConfigureQorMetaBeforeInitialize(meta resource.Metaor) {
	if meta, ok := meta.(*admin.Meta); ok {
		meta.Type = "file"

		if meta.GetFormattedValuer() == nil {
			meta.SetFormattedValuer(func(value interface{}, context *qor.Context) interface{} {
				return utils.Stringify(meta.GetValuer()(value, context))
			})
		}

		for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
			admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/media_library/views"))
		}
	}
}
开发者ID:8legd,项目名称:qor-media_library,代码行数:15,代码来源:base.go

示例4: ConfigureQorResource

func (publish *Publish) ConfigureQorResource(res *admin.Resource) {
	if !injected {
		injected = true
		for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
			admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/qor/publish/views"))
		}
	}
	res.UseTheme("publish")

	if event := res.GetAdmin().GetResource("PublishEvent"); event == nil {
		eventResource := res.GetAdmin().AddResource(&PublishEvent{}, &admin.Config{Invisible: true})
		eventResource.IndexAttrs("Name", "Description", "CreatedAt")
	}

	controller := publishController{publish}
	router := res.GetAdmin().GetRouter()
	router.Get(fmt.Sprintf("^/%v/diff/", res.ToParam()), controller.Diff)
	router.Get(fmt.Sprintf("^/%v", res.ToParam()), controller.Preview)
	router.Post(fmt.Sprintf("^/%v", res.ToParam()), controller.PublishOrDiscard)

	res.GetAdmin().RegisterFuncMap("render_publish_meta", func(value interface{}, meta *admin.Meta, context *admin.Context) template.HTML {
		var err error
		var result = bytes.NewBufferString("")
		var tmpl = template.New(meta.Type + ".tmpl").Funcs(context.FuncMap())

		if tmpl, err = context.FindTemplate(tmpl, fmt.Sprintf("metas/publish/%v.tmpl", meta.Type)); err != nil {
			if tmpl, err = context.FindTemplate(tmpl, fmt.Sprintf("metas/index/%v.tmpl", meta.Type)); err != nil {
				tmpl, _ = tmpl.Parse("{{.Value}}")
			}
		}

		data := map[string]interface{}{"Value": context.ValueOf(value, meta), "Meta": meta}
		if err := tmpl.Execute(result, data); err != nil {
			utils.ExitWithMsg(err.Error())
		}
		return template.HTML(result.String())
	})

	res.GetAdmin().RegisterFuncMap("publish_unique_key", func(res *admin.Resource, record interface{}, context *admin.Context) string {
		return fmt.Sprintf("%s__%v", res.ToParam(), context.GetDB().NewScope(record).PrimaryKeyValue())
	})

	res.GetAdmin().RegisterFuncMap("is_publish_event_resource", func(res *admin.Resource) bool {
		return IsPublishEvent(res.Value)
	})
}
开发者ID:kingland,项目名称:qor,代码行数:46,代码来源:controller.go

示例5: ConfigureQorResource

func (i18n *I18n) ConfigureQorResource(res *admin.Resource) {
	res.UseTheme("i18n")
	res.GetAdmin().I18n = i18n
	res.SearchAttrs("value") // generate search handler for i18n

	res.GetAdmin().RegisterFuncMap("lt", func(locale, key string, withDefault bool) string {
		if translations := i18n.Translations[locale]; translations != nil {
			if t := translations[key]; t != nil && t.Value != "" {
				return t.Value
			}
		}

		if withDefault {
			if translations := i18n.Translations[Default]; translations != nil {
				if t := translations[key]; t != nil {
					return t.Value
				}
			}
		}

		return ""
	})

	res.GetAdmin().RegisterFuncMap("i18n_available_keys", func(context *admin.Context) (keys []string) {
		translations := i18n.Translations[Default]
		if translations == nil {
			for _, values := range i18n.Translations {
				translations = values
				break
			}
		}

		keyword := context.Request.URL.Query().Get("keyword")

		for key, translation := range translations {
			if (keyword == "") || (strings.Index(strings.ToLower(translation.Key), strings.ToLower(keyword)) != -1 ||
				strings.Index(strings.ToLower(translation.Value), keyword) != -1) {
				keys = append(keys, key)
			}
		}

		sort.Strings(keys)

		pagination := context.Searcher.Pagination
		pagination.Total = len(keys)
		pagination.PrePage = 25
		pagination.CurrentPage, _ = strconv.Atoi(context.Request.URL.Query().Get("page"))
		if pagination.CurrentPage == 0 {
			pagination.CurrentPage = 1
		}
		if pagination.CurrentPage > 0 {
			pagination.Pages = pagination.Total / pagination.PrePage
		}
		context.Searcher.Pagination = pagination

		if pagination.CurrentPage == -1 {
			return keys
		}

		lastIndex := pagination.CurrentPage * pagination.PrePage
		if pagination.Total < lastIndex {
			lastIndex = pagination.Total
		}

		return keys[(pagination.CurrentPage-1)*pagination.PrePage : lastIndex]
	})

	res.GetAdmin().RegisterFuncMap("i18n_primary_locale", func(context admin.Context) string {
		if locale := context.Request.Form.Get("primary_locale"); locale != "" {
			return locale
		}
		return getAvailableLocales(context.Request, context.CurrentUser)[0]
	})

	res.GetAdmin().RegisterFuncMap("i18n_editing_locale", func(context admin.Context) string {
		if locale := context.Request.Form.Get("to_locale"); locale != "" {
			return locale
		}
		return getLocaleFromContext(context.Context)
	})

	res.GetAdmin().RegisterFuncMap("i18n_viewable_locales", func(context admin.Context) []string {
		return getAvailableLocales(context.Request, context.CurrentUser)
	})

	res.GetAdmin().RegisterFuncMap("i18n_editable_locales", func(context admin.Context) []string {
		return getEditableLocales(context.Request, context.CurrentUser)
	})

	controller := i18nController{i18n}
	router := res.GetAdmin().GetRouter()
	router.Get(fmt.Sprintf("^/%v", res.ToParam()), controller.Index)
	router.Post(fmt.Sprintf("^/%v", res.ToParam()), controller.Update)
	router.Put(fmt.Sprintf("^/%v", res.ToParam()), controller.Update)

	for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
		admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/qor/i18n/views"))
	}
}
开发者ID:kingland,项目名称:qor,代码行数:99,代码来源:i18n.go

示例6: ConfigureQorResourceBeforeInitialize

func (serialize *SerializableMeta) ConfigureQorResourceBeforeInitialize(res resource.Resourcer) {
	if res, ok := res.(*admin.Resource); ok {
		for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
			admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/serializable_meta/views"))
		}

		if _, ok := res.Value.(SerializableMetaInterface); ok {
			if res.GetMeta("Kind") == nil {
				res.Meta(&admin.Meta{
					Name: "Kind",
					Type: "hidden",
					Valuer: func(value interface{}, context *qor.Context) interface{} {
						if context.GetDB().NewScope(value).PrimaryKeyZero() {
							return nil
						} else {
							return value.(SerializableMetaInterface).GetSerializableArgumentKind()
						}
					},
				})
			}

			if res.GetMeta("SerializableMeta") == nil {
				res.Meta(&admin.Meta{
					Name: "SerializableMeta",
					Type: "serializable_meta",
					Valuer: func(value interface{}, context *qor.Context) interface{} {
						if serializeArgument, ok := value.(SerializableMetaInterface); ok {
							return struct {
								Value    interface{}
								Resource *admin.Resource
							}{
								Value:    serializeArgument.GetSerializableArgument(serializeArgument),
								Resource: serializeArgument.GetSerializableArgumentResource(),
							}
						}
						return nil
					},
					Setter: func(result interface{}, metaValue *resource.MetaValue, context *qor.Context) {
						if serializeArgument, ok := result.(SerializableMetaInterface); ok {
							serializeArgumentResource := serializeArgument.GetSerializableArgumentResource()
							value := serializeArgumentResource.NewStruct()

							for _, meta := range serializeArgumentResource.GetMetas([]string{}) {
								for _, metaValue := range metaValue.MetaValues.Values {
									if meta.GetName() == metaValue.Name {
										if setter := meta.GetSetter(); setter != nil {
											setter(value, metaValue, context)
										}
									}
								}
							}

							serializeArgument.SetSerializableArgumentValue(value)
						}
					},
				})
			}

			res.NewAttrs("Kind", "SerializableMeta")
			res.EditAttrs("ID", "Kind", "SerializableMeta")
		}
	}
}
开发者ID:8legd,项目名称:qor-serializable_meta,代码行数:63,代码来源:serializable_meta.go

示例7: ConfigureQorResource


//.........这里部分代码省略.........
			if meta := res.GetMeta(field.Name); meta != nil {
				permission := meta.Permission
				if permission == nil {
					permission = roles.Allow(roles.CRUD, "global_admin").Allow(roles.Read, "locale_reader")
				} else {
					permission = permission.Allow(roles.CRUD, "global_admin").Allow(roles.Read, "locale_reader")
				}

				meta.Permission = permission
			} else {
				res.Meta(&admin.Meta{Name: field.Name, Permission: roles.Allow(roles.CRUD, "global_admin").Allow(roles.Read, "locale_reader")})
			}
		}
	}

	// Roles
	role := res.Config.Permission.Role
	if _, ok := role.Get("locale_admin"); !ok {
		role.Register("locale_admin", func(req *http.Request, currentUser qor.CurrentUser) bool {
			currentLocale := getLocaleFromContext(&qor.Context{Request: req})
			for _, locale := range getEditableLocales(req, currentUser) {
				if locale == currentLocale {
					return true
				}
			}
			return false
		})
	}

	if _, ok := role.Get("global_admin"); !ok {
		role.Register("global_admin", func(req *http.Request, currentUser qor.CurrentUser) bool {
			return getLocaleFromContext(&qor.Context{Request: req}) == Global
		})
	}

	if _, ok := role.Get("locale_reader"); !ok {
		role.Register("locale_reader", func(req *http.Request, currentUser qor.CurrentUser) bool {
			currentLocale := getLocaleFromContext(&qor.Context{Request: req})
			for _, locale := range getAvailableLocales(req, currentUser) {
				if locale == currentLocale {
					return true
				}
			}
			return false
		})
	}

	// Inject for l10n
	if !injected {
		injected = true
		for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
			admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/qor/l10n/views"))
		}

		// Middleware
		Admin.GetRouter().Use(func(context *admin.Context, middleware *admin.Middleware) {
			db := context.GetDB().Set("l10n:locale", getLocaleFromContext(context.Context))
			if mode := context.Request.URL.Query().Get("locale_mode"); mode != "" {
				db = db.Set("l10n:mode", mode)
			}
			if context.Request.URL.Query().Get("sorting") != "" {
				db = db.Set("l10n:mode", "locale")
			}
			context.SetDB(db)

			middleware.Next(context)
		})

		// FunMap
		Admin.RegisterFuncMap("current_locale", func(context admin.Context) string {
			return getLocaleFromContext(context.Context)
		})

		Admin.RegisterFuncMap("global_locale", func() string {
			return Global
		})

		Admin.RegisterFuncMap("viewable_locales", func(context admin.Context) []string {
			return getAvailableLocales(context.Request, context.CurrentUser)
		})

		Admin.RegisterFuncMap("editable_locales", func(context admin.Context) []string {
			return getEditableLocales(context.Request, context.CurrentUser)
		})

		Admin.RegisterFuncMap("createable_locales", func(context admin.Context) []string {
			editableLocales := getEditableLocales(context.Request, context.CurrentUser)
			if _, ok := context.Resource.Value.(localeCreatableInterface); ok {
				return editableLocales
			} else {
				for _, locale := range editableLocales {
					if locale == Global {
						return []string{Global}
					}
				}
			}
			return []string{}
		})
	}
}
开发者ID:NeoChow,项目名称:qor,代码行数:101,代码来源:l10n.go

示例8: ConfigureQorResource

// ConfigureQorResource configure sorting for qor admin
func (s *Sorting) ConfigureQorResource(res resource.Resourcer) {
	if res, ok := res.(*admin.Resource); ok {
		Admin := res.GetAdmin()
		res.UseTheme("sorting")

		if res.Config.Permission == nil {
			res.Config.Permission = roles.NewPermission()
		}

		for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
			admin.RegisterViewPath(path.Join(gopath, "src/github.com/qor/sorting/views"))
		}

		role := res.Config.Permission.Role
		if _, ok := role.Get("sorting_mode"); !ok {
			role.Register("sorting_mode", func(req *http.Request, currentUser interface{}) bool {
				return req.URL.Query().Get("sorting") != ""
			})
		}

		if res.GetMeta("Position") == nil {
			res.Meta(&admin.Meta{
				Name: "Position",
				Valuer: func(value interface{}, ctx *qor.Context) interface{} {
					db := ctx.GetDB()
					var count int
					var pos = value.(sortingInterface).GetPosition()

					if _, ok := modelValue(value).(sortingDescInterface); ok {
						if total, ok := db.Get("sorting_total_count"); ok {
							count = total.(int)
						} else {
							var result = res.NewStruct()
							db.New().Order("position DESC", true).First(result)
							count = result.(sortingInterface).GetPosition()
							db.InstantSet("sorting_total_count", count)
						}
						pos = count - pos + 1
					}

					primaryKey := ctx.GetDB().NewScope(value).PrimaryKeyValue()
					url := path.Join(ctx.Request.URL.Path, fmt.Sprintf("%v", primaryKey), "sorting/update_position")
					return template.HTML(fmt.Sprintf("<input type=\"number\" class=\"qor-sorting__position\" value=\"%v\" data-sorting-url=\"%v\" data-position=\"%v\">", pos, url, pos))
				},
				Permission: roles.Allow(roles.Read, "sorting_mode"),
			})
		}

		attrs := res.ConvertSectionToStrings(res.IndexAttrs())
		for _, attr := range attrs {
			if attr != "Position" {
				attrs = append(attrs, attr)
			}
		}
		res.IndexAttrs(res.IndexAttrs(), "Position")
		res.NewAttrs(res.NewAttrs(), "-Position")
		res.EditAttrs(res.EditAttrs(), "-Position")
		res.ShowAttrs(res.ShowAttrs(), "-Position", false)

		router := Admin.GetRouter()
		router.Post(fmt.Sprintf("/%v/%v/sorting/update_position", res.ToParam(), res.ParamIDName()), updatePosition)
	}
}
开发者ID:8legd,项目名称:qor-sorting,代码行数:64,代码来源:controller.go


注:本文中的github.com/qor/qor/admin.RegisterViewPath函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。