// Pages returns the pages stored with key "pages". If this is a func,
// it will be invoked.
972462
func (d Data) Pages() Pages {
	v, found := d["pages"]
	if !found {
		return nil
	}

	switch vv := v.(type) {
	case Pages:
		return vv
	case func() Pages:
		return vv()
	default:
		panic(fmt.Sprintf("%T is not Pages", v))
	}
}
474735
func (b *Bucket) forEachPage(fn func(*page, int)) {
	// If we have an inline page then just use that.
	if b.page != nil {
		fn(b.page, 0)
		return
	}

	// Otherwise traverse the page hierarchy.
	b.tx.forEachPage(b.root, 0, fn)
}
485599
func (tx *Tx) Page(id int) (*PageInfo, error) {
	if tx.db == nil {
		return nil, ErrTxClosed
	} else if pgid(id) >= tx.meta.pgid {
		return nil, nil
	}

	// Build the page info.
	p := tx.db.page(pgid(id))
	info := &PageInfo{
		ID:            id,
		Count:         int(p.count),
		OverflowCount: int(p.overflow),
	}

	// Determine the type (or if it's free).
	if tx.db.freelist.freed(pgid(id)) {
		info.Type = "free"
	} else {
		info.Type = p.typ()
	}

	return info, nil
}
486118
func (p *Pages) RemovePage(name string) *Pages {
	var isVisible bool
	hasFocus := p.HasFocus()
	for index, page := range p.pages {
		if page.Name == name {
			isVisible = page.Visible
			p.pages = append(p.pages[:index], p.pages[index+1:]...)
			if page.Visible && p.changed != nil {
				p.changed()
			}
			break
		}
	}
	if isVisible {
		for index, page := range p.pages {
			if index < len(p.pages)-1 {
				if page.Visible {
					break // There is a remaining visible page.
				}
			} else {
				page.Visible = true // We need at least one visible page.
			}
		}
	}
	if hasFocus {
		p.Focus(p.setFocus)
	}
	return p
}
456615
func (p Pages) GroupByParam(key string, order ...string) (PagesGroup, error) {
	if len(p) < 1 {
		return nil, nil
	}

	direction := "asc"

	if len(order) > 0 && (strings.ToLower(order[0]) == "desc" || strings.ToLower(order[0]) == "rev" || strings.ToLower(order[0]) == "reverse") {
		direction = "desc"
	}

	var tmp reflect.Value
	var keyt reflect.Type
	for _, e := range p {
		param := resource.GetParamToLower(e, key)
		if param != nil {
			if _, ok := param.([]string); !ok {
				keyt = reflect.TypeOf(param)
				tmp = reflect.MakeMap(reflect.MapOf(keyt, pagesType))
				break
			}
		}
	}
	if !tmp.IsValid() {
		return nil, errors.New("there is no such a param")
	}

	for _, e := range p {
		param := resource.GetParam(e, key)

		if param == nil || reflect.TypeOf(param) != keyt {
			continue
		}
		v := reflect.ValueOf(param)
		if !tmp.MapIndex(v).IsValid() {
			tmp.SetMapIndex(v, reflect.MakeSlice(pagesType, 0, 0))
		}
		tmp.SetMapIndex(v, reflect.Append(tmp.MapIndex(v), reflect.ValueOf(e)))
	}

	var r []PageGroup
	for _, k := range sortKeys(tmp.MapKeys(), direction) {
		r = append(r, PageGroup{Key: k.Interface(), Pages: tmp.MapIndex(k).Interface().(Pages)})
	}

	return r, nil
}
16173
func (p Pages) GroupBy(key string, order ...string) (PagesGroup, error) {
	if len(p) < 1 {
		return nil, nil
	}

	direction := "asc"

	if len(order) > 0 && (strings.ToLower(order[0]) == "desc" || strings.ToLower(order[0]) == "rev" || strings.ToLower(order[0]) == "reverse") {
		direction = "desc"
	}

	var ft interface{}
	m, ok := pagePtrType.MethodByName(key)
	if ok {
		if m.Type.NumOut() == 0 || m.Type.NumOut() > 2 {
			return nil, errors.New(key + " is a Page method but you can't use it with GroupBy")
		}
		if m.Type.NumOut() == 1 && m.Type.Out(0).Implements(errorType) {
			return nil, errors.New(key + " is a Page method but you can't use it with GroupBy")
		}
		if m.Type.NumOut() == 2 && !m.Type.Out(1).Implements(errorType) {
			return nil, errors.New(key + " is a Page method but you can't use it with GroupBy")
		}
		ft = m
	} else {
		ft, ok = pagePtrType.Elem().FieldByName(key)
		if !ok {
			return nil, errors.New(key + " is neither a field nor a method of Page")
		}
	}

	var tmp reflect.Value
	switch e := ft.(type) {
	case reflect.StructField:
		tmp = reflect.MakeMap(reflect.MapOf(e.Type, pagesType))
	case reflect.Method:
		tmp = reflect.MakeMap(reflect.MapOf(e.Type.Out(0), pagesType))
	}

	for _, e := range p {
		ppv := reflect.ValueOf(e)
		var fv reflect.Value
		switch ft.(type) {
		case reflect.StructField:
			fv = ppv.Elem().FieldByName(key)
		case reflect.Method:
			fv = ppv.MethodByName(key).Call([]reflect.Value{})[0]
		}
		if !fv.IsValid() {
			continue
		}
		if !tmp.MapIndex(fv).IsValid() {
			tmp.SetMapIndex(fv, reflect.MakeSlice(pagesType, 0, 0))
		}
		tmp.SetMapIndex(fv, reflect.Append(tmp.MapIndex(fv), ppv))
	}

	sortedKeys := sortKeys(tmp.MapKeys(), direction)
	r := make([]PageGroup, len(sortedKeys))
	for i, k := range sortedKeys {
		r[i] = PageGroup{Key: k.Interface(), Pages: tmp.MapIndex(k).Interface().(Pages)}
	}

	return r, nil
}
1289996
func (by pageBy) Sort(pages Pages) {
	ps := &pageSorter{
		pages: pages,
		by:    by, // The Sort method's receiver is the function (closure) that defines the sort order.
	}
	sort.Stable(ps)
}
121152
func (v Values) Keys() []string {
	keys := make([]string, len(v))
	var i int
	for key := range v {
		keys[i] = key
		i += 1
	}
	sort.Strings(keys)
	return keys
}
1136867
func (c *LRUExpireCache) Keys() []interface{} {
	c.lock.Lock()
	defer c.lock.Unlock()
	return c.cache.Keys()
}
379602
func (h *handler) fetchDocumentByPages(pages []blob.Ref) (*document, error) {
	return h.fetchDocumentFunc(pages, h.searchDocumentByPages)
}
1242540
func (zones *Zones) listPages(ctx context.Context, page int) (list []*Zone, err error) {
	response, err := httpDo(ctx, zones.Options, "GET", apiURL("/zones?page=%d&per_page=50", page), nil)
	if err != nil {
		return
	}
	defer response.Body.Close()
	result, err := readResponse(response.Body)
	if err != nil {
		return
	}
	err = json.Unmarshal(result.Result, &list)
	if err != nil {
		return
	}
	if result.ResultInfo == nil || page >= result.ResultInfo.TotalPages {
		return
	}
	next, err := zones.listPages(ctx, page+1)
	if err != nil {
		return
	}
	return append(list, next...), nil
}
459579
func (wp WeightedPages) Pages() Pages {
	pages := make(Pages, len(wp))
	for i := range wp {
		pages[i] = wp[i].Page
	}
	return pages
}
745954
func (self *SimpleIdComponent) Page(pb PBundle, path []string, trailingSlash bool) ComponentResult {

	//allow singular to be used as synonym for plural
	if len(path) == 0 {
		return ComponentResult{
			Path:   "/" + filepath.Join(self.UrlPrefix(), "index.html"),
			Status: http.StatusOK,
		}
	}

	if (len(path) == 1 && path[0] == "new") || (len(path) == 1 && path[0] == "new.html") {
		if self.newcheck != nil {
			ok, err := self.newcheck(pb)
			if err != nil {
				return ComponentResult{
					Status:  http.StatusInternalServerError,
					Message: err.Error(),
				}
			}
			if !ok {
				return ComponentResult{
					Message: "you lose",
					Status:  http.StatusUnauthorized,
				}
			}
		}
		return ComponentResult{
			Path:   "/" + filepath.Join(self.UrlPrefix(), "new.html"),
			Status: http.StatusOK,
		}
	}

	if (len(path) == 1 && path[0] == "index") || (len(path) == 1 && path[0] == "index.html") {
		return ComponentResult{
			Path:   "/" + filepath.Join(self.UrlPrefix(), "index.html"),
			Status: http.StatusOK,
		}
	}

	// parse the id in the URL
	rawId := path[0]
	id, err := strconv.ParseInt(rawId, 10, 64)
	if err != nil {
		//handle the case of /<singular>/new.js
		return ComponentResult{
			Status:           CONTINUE,
			ContinueAt:       self.singular,
			ContinueConsumed: 1,
		}
	}
	if self.exists != nil {
		ok, err := self.exists(pb, id)
		if err != nil {
			return ComponentResult{
				Status:  http.StatusInternalServerError,
				Message: err.Error(),
			}
		}
		if !ok {
			return ComponentResult{
				Status:  http.StatusNotFound,
				Message: fmt.Sprintf("could not find %d", id),
			}
		}
	}

	// show view.html for /foo/1 or /foo/1/
	if len(path) == 1 {
		if self.viewedit != nil {
			ok, err := self.viewedit(pb, id, true)
			if err != nil {
				return ComponentResult{
					Status:  http.StatusInternalServerError,
					Message: err.Error(),
				}
			}
			if !ok {
				return ComponentResult{
					Message: "you lose",
					Status:  http.StatusUnauthorized,
				}
			}
		}
		return ComponentResult{
			Path:   "/" + filepath.Join(self.UrlPrefix(), "view.html"),
			Status: http.StatusOK,
		}
	}

	// see if it's a verb we understand
	if len(path) == 2 {
		verb := path[1]
		switch verb {
		case "edit", "edit.html", "view", "view.html":
			if !strings.HasSuffix(verb, ".html") {
				verb = verb + ".html"
			}
			if self.viewedit != nil {
				ok, err := self.viewedit(pb, id, verb == "view.html")
				if err != nil {
					return ComponentResult{
						Status:  http.StatusInternalServerError,
						Message: err.Error(),
					}
				}
				if !ok {
					return ComponentResult{
						Message: "you lose",
						Status:  http.StatusUnauthorized,
					}
				}
				return ComponentResult{
					Path:   "/" + filepath.Join(self.UrlPrefix(), verb),
					Status: http.StatusOK,
				}

			}
		default:
			return ComponentResult{
				Status:           CONTINUE,
				ContinueAt:       self.UrlPrefix(),
				ContinueConsumed: 2,
			}
		}
	}

	//we don't accept any more parts to the URL
	return ComponentResult{
		Status:  http.StatusBadRequest,
		Message: fmt.Sprintf("Unacceptable url for %s", self.singular),
	}
}
490156
func Paging(curPage int, pageSize int, totalRecords int, items interface{}) Page {

	totalPages := int(totalRecords) / pageSize
	if totalPages == 0 {
		totalPages = 1
	}

	if int(totalRecords)%pageSize != 0 {
		totalPages++
	}

	return Page{
		Page:         curPage,
		Size:         pageSize,
		TotalRecords: int(totalRecords),
		TotalPages:   totalPages,
		Items:        items,
	}
}
1100994
func (p *pageState) Group(key interface{}, in interface{}) (interface{}, error) {
	pages, err := page.ToPages(in)
	if err != nil {
		return nil, err
	}
	return page.PageGroup{Key: key, Pages: pages}, nil
}
684872
func (pr PaginatedResources) Resources() ([]interface{}, error) {
	slicePtr := reflect.New(reflect.SliceOf(pr.resourceType))
	err := json.Unmarshal([]byte(pr.ResourcesBytes), slicePtr.Interface())
	slice := reflect.Indirect(slicePtr)

	contents := make([]interface{}, 0, slice.Len())
	for i := 0; i < slice.Len(); i++ {
		contents = append(contents, slice.Index(i).Interface())
	}
	return contents, err
}
286802
func (pdfg *PDFGenerator) AddPage(p page) {
	pdfg.pages = append(pdfg.pages, p)
}