Пример #1
0
func (t *DesignerController) SetHideShow(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	panelsid := strings.Split(payload["panelid"], ",")
	content := config["content"].([]interface{})

	for i, each := range content {
		content[i].(map[string]interface{})["hide"] = false

		for _, panelid := range panelsid {
			if panelid == each.(map[string]interface{})["panelID"] {
				content[i].(map[string]interface{})["hide"] = true
			}
		}
	}
	config["content"] = content

	err = t.setConfig(_id, config)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #2
0
func (t *DesignerController) GetWidgetMetaData(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	for _, eachRaw := range config["content"].([]interface{}) {
		each := eachRaw.(map[string]interface{})

		if each["panelID"] == payload["panelID"] {
			for _, subRaw := range each["content"].([]interface{}) {
				sub := subRaw.(map[string]interface{})

				if sub["widgetID"] == payload["widgetID"] {
					return helper.Result(true, sub, "")
				}
			}
		}
	}

	return helper.Result(false, nil, "")
}
Пример #3
0
func (t *DataSourceController) GetDataSource(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["_id"]
	dsType := payload["type"]
	path := payload["path"]

	if _, ok := payload["type"]; !ok {
		data, err := t.getDataSourceMetaData(_id)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		dsType = data["type"].(string)
		path = data["path"].(string)
	}

	ds, err := helper.FetchDataSource(_id, dsType, path)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, ds, "")
}
Пример #4
0
func (t *DesignerController) ReoderPanel(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]interface{}{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"].(string)
	order := strings.Split(payload["order"].(string), ",")

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	content := map[string]interface{}{}
	newContent := []interface{}{}

	for _, each := range config["content"].([]interface{}) {
		panel := each.(map[string]interface{})
		content[panel["panelID"].(string)] = panel
	}

	for _, panelID := range order {
		newContent = append(newContent, content[panelID])
	}

	config["content"] = newContent
	err = t.setConfig(_id, config)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #5
0
func (t *DataSourceController) GetDataSourceSelector(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	payload := map[string]interface{}{}
	err := r.GetPayload(&payload)
	// fmt.Println(payload["item"].([]interface{}))
	// payload := map[string]string{}
	// err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["_id"].(string)
	dsType := ""
	path := ""

	if _, ok := payload["type"]; !ok {
		data, err := t.getDataSourceMetaData(_id)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		dsType = data["type"].(string)
		path = data["path"].(string)
	}

	ds, err := helper.FetchDataSource(_id, dsType, path)
	sds, err := helper.FetchQuerySelector(ds, payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, sds, "")
}
Пример #6
0
func (t *DataSourceController) RemoveDataSource(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/datasource.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	err = connection.NewQuery().Delete().Where(dbox.Eq("_id", payload["_id"])).Exec(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if payload["type"] == "file" {
		err = os.Remove(t.AppViewsPath + "data/datasource/" + payload["path"])
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, nil, "")
}
Пример #7
0
func (t *GridController) GetGridData(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/mapgrid.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("seq", "data").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	if len(res) == 0 {
		return helper.Result(false, nil, "No data found")
	}

	return helper.Result(true, res, "")
}
Пример #8
0
func (t *ChartController) RemoveChartConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	// remove chart
	filename := fmt.Sprintf("chart-%s.json", payload["_id"])
	err = os.Remove(t.AppViewsPath + "data/chart/" + filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	// remove chart meta data
	connection, err := helper.LoadConfig(t.AppViewsPath + "data/chart.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	err = connection.NewQuery().Delete().Where(dbox.Eq("_id", payload["_id"])).Exec(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #9
0
func (t *GridController) GetDetailGrid(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/grid/" + payload["recordid"])
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, dataSource.Data, "")
}
Пример #10
0
func (t *DesignerController) GetPanel(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	data, err := t.getConfig(payload["_id"])
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	for _, eachRaw := range data["content"].([]interface{}) {
		each := eachRaw.(map[string]interface{})
		if each["panelID"] == payload["panelID"] {
			offset := 0
			if _, ok := each["offset"]; ok {
				offset = int(each["offset"].(float64))
			}

			data := map[string]interface{}{
				"_id":    each["panelID"],
				"title":  each["title"],
				"width":  int(each["width"].(float64)),
				"offset": offset,
			}
			return helper.Result(true, data, "")
		}
	}

	return helper.Result(true, map[string]interface{}{}, "")
}
Пример #11
0
func (t *DesignerController) SavePanel(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]interface{}{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["_id"].(string)
	title := payload["title"].(string)
	hide, _ := strconv.ParseBool(payload["hide"].(string))
	var width int = int(payload["width"].(float64))
	var offset int = int(payload["offset"].(float64))
	hideContainerPanel, _ := strconv.ParseBool(payload["hideContainerPanel"].(string))

	panelID := payload["panelID"].(string)

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	contentOld := config["content"].([]interface{})

	if panelID == "" {
		panelID = helper.RandomIDWithPrefix("p")
		contentNew := map[string]interface{}{
			"panelID":            panelID,
			"title":              title,
			"width":              width,
			"offset":             offset,
			"hide":               hide,
			"hideContainerPanel": hideContainerPanel,
			"content":            []interface{}{},
		}
		config["content"] = append([]interface{}{contentNew}, contentOld...)
	} else {
		for i, eachRaw := range contentOld {
			each := eachRaw.(map[string]interface{})
			if each["panelID"] == payload["panelID"] {
				contentOld[i].(map[string]interface{})["title"] = title
				contentOld[i].(map[string]interface{})["width"] = width
				contentOld[i].(map[string]interface{})["offset"] = offset
				contentOld[i].(map[string]interface{})["hideContainerPanel"] = hideContainerPanel
			}
		}

		config["content"] = contentOld
	}

	err = t.setConfig(_id, config)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, panelID, "")
	//~ return helper.Result(true, config, config)
	//~ return fmt.printf("%v",config)
}
Пример #12
0
func (t *ChartController) GetChartConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	filename := fmt.Sprintf("chart-%s.json", payload["_id"])
	isWithDataSource, err := strconv.ParseBool(payload["isWithDataSource"])
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	fileContent, err := ioutil.ReadFile(t.AppViewsPath + "data/chart/" + filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	data := map[string]interface{}{}
	err = json.Unmarshal(fileContent, &data)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if isWithDataSource {
		dataSourceID := data["outsider"].(map[string]interface{})["dataSourceKey"].(string)

		connection, err := helper.LoadConfig(t.AppViewsPath + "data/datasource.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		cursor, err := connection.NewQuery().Where(dbox.Eq("_id", dataSourceID)).Cursor(nil)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer cursor.Close()

		res := []toolkit.M{}
		err = cursor.Fetch(&res, 0, false)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		if len(res) == 0 {
			return helper.Result(false, nil, "No data found")
		}

		dsID := res[0].GetString("_id")
		dsType := res[0].GetString("type")
		dsPath := res[0].GetString("path")
		dataSource, _ := helper.FetchDataSource(dsID, dsType, dsPath)
		data["dataSource"] = map[string]interface{}{"data": dataSource}
	}

	return helper.Result(true, data, "")
}
Пример #13
0
func (t *DataSourceController) GetDataSourceMetaData(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	data, err := t.getDataSourceMetaData(payload["_id"])
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, data, "")
}
Пример #14
0
func (t *DataSourceController) SaveDataSource(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	r.Request.ParseMultipartForm(32 << 20)

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["_id"]
	if _id == "" {
		_id = helper.RandomIDWithPrefix("ds")
	}

	// upload file
	if payload["type"] == "file" {
		filename := fmt.Sprintf("datasource-%s.json", _id)
		filepath := t.AppViewsPath + "data/datasource/" + filename

		_, _, err = helper.FetchThenSaveFile(r.Request, "file", filepath)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		payload["path"] = filename
	}

	delete(payload, "file")

	if payload["_id"] == "" {
		payload["_id"] = _id

		// insert
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/datasource.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		err = connection.NewQuery().Insert().Exec(toolkit.M{"data": payload})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	} else {
		// update
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/datasource.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		err = connection.NewQuery().Update().Where(dbox.Eq("_id", _id)).Exec(toolkit.M{"data": payload})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, nil, "")
}
Пример #15
0
func (t *DesignerController) SaveOtherConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]
	configString := payload["config"]

	config := map[string]interface{}{}
	json.Unmarshal([]byte(configString), &config)

	err = t.setConfig(_id, config)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #16
0
func (t *SelectorController) GetSelectorConfigs(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	configFilepath := t.AppViewsPath + "data/selector.json"

	if _, err := os.Stat(configFilepath); err != nil {
		if os.IsNotExist(err) {
			os.Create(configFilepath)
		} else {
			return helper.Result(false, nil, err.Error())
		}
	}

	connection, err := helper.LoadConfig(configFilepath)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if len(dataSource.Data) > 0 {
		return helper.Result(true, dataSource.Data, "")
	}

	return helper.Result(true, []interface{}{}, "")
}
Пример #17
0
func (t *DesignerController) GetConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	data, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "/data/routes.json")
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Where(dbox.Eq("_id", _id)).Cursor(nil)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	data["href"] = dataSource.Data[0].(map[string]interface{})["href"]

	return helper.Result(true, data, "")
}
Пример #18
0
func (t *DataSourceController) GetDataSources(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/datasource.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	dataSource, err := cursor.Fetch(nil, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, dataSource.Data, "")
}
Пример #19
0
func (t *SelectorController) RemoveSelectorConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "data/selector.json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	err = connection.NewQuery().Delete().Where(dbox.Eq("ID", payload["ID"])).Exec(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #20
0
func (t *DesignerController) SetDataSource(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	config["datasources"] = strings.Split(payload["datasources"], ",")

	err = t.setConfig(_id, config)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #21
0
func (t *DesignerController) RemovePanel(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["_id"]
	panelID := payload["panelID"]

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	contentOld := config["content"].([]interface{})
	contentNew := []interface{}{}

	for _, each := range contentOld {
		if each.(map[string]interface{})["panelID"] == panelID {
			continue
		}

		contentNew = append(contentNew, each)
	}

	config["content"] = contentNew

	err = t.setConfig(_id, config)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	return helper.Result(true, nil, "")
}
Пример #22
0
func (t *DataSourceController) GetDataSourceFields(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	meta, err := t.getDataSourceMetaData(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	dsType := meta["type"].(string)
	path := meta["path"].(string)

	data, err := helper.FetchDataSource(_id, dsType, path)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	fields := []string{}
	holder := map[string]bool{}
	for _, each := range data[:len(data)] {
		for key, _ := range each {
			if _, ok := holder[key]; !ok {
				fields = append(fields, key)
				holder[key] = true
			}
		}
	}

	return helper.Result(true, fields, "")
}
Пример #23
0
func (t *GridController) DeleteGrid(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	filename := t.AppViewsPath + "data/mapgrid.json"
	f, err := os.Open(filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	mapgrid := []m.MapGrid{}
	jsonParser := json.NewDecoder(f)
	if err = jsonParser.Decode(&mapgrid); err != nil {
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	newGrid := []m.DataMapGrid{}
	for _, eachRaw := range mapgrid[0].Data {
		if eachRaw.Value != payload["recordid"] {
			newGrid = append(newGrid, eachRaw)
		}
	}
	mapgrid[0].Data = newGrid
	b, err := json.Marshal(mapgrid)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	f, err = os.Create(filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	io.WriteString(f, string(b))
	err = os.Remove(t.AppViewsPath + "data/grid/" + payload["recordid"])
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	defer f.Close()

	return helper.Result(true, payload["recordid"], "")
}
Пример #24
0
func (t *SelectorController) SaveSelector(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_id := payload["ID"]
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if _id == "" {
		_id = helper.RandomIDWithPrefix("c")
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/selector.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		newData := toolkit.M{"ID": _id, "title": payload["title"], "fields": payload["fields"], "masterDataSource": payload["masterDataSource"]}
		err = connection.NewQuery().Insert().Exec(toolkit.M{"data": newData})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	} else {
		connection, err := helper.LoadConfig(t.AppViewsPath + "/data/selector.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		newData := toolkit.M{"ID": _id, "title": payload["title"], "fields": payload["fields"], "masterDataSource": payload["masterDataSource"]}
		err = connection.NewQuery().Update().Where(dbox.Eq("ID", _id)).Exec(toolkit.M{"data": newData})
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, _id, "")
}
Пример #25
0
func (t *DesignerController) GetConfig(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"]

	data, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	connection, err := helper.LoadConfig(t.AppViewsPath + "/data/routes.json")
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Where(dbox.Eq("_id", _id)).Cursor(nil)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		helper.Result(false, nil, err.Error())
	}
	if len(res) == 0 {
		return helper.Result(false, nil, "No data found")
	}
	data["href"] = res[0].GetString("href")

	return helper.Result(true, data, "")
}
Пример #26
0
func (t *ChartController) GetChartConfigs(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	configFilepath := t.AppViewsPath + "data/chart.json"

	if _, err := os.Stat(configFilepath); err != nil {
		if os.IsNotExist(err) {
			os.Create(configFilepath)
		} else {
			return helper.Result(false, nil, err.Error())
		}
	}

	connection, err := helper.LoadConfig(configFilepath)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer connection.Close()

	cursor, err := connection.NewQuery().Select("*").Cursor(nil)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	defer cursor.Close()

	res := []toolkit.M{}
	err = cursor.Fetch(&res, 0, false)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if len(res) > 0 {
		return helper.Result(true, res, "")
	}

	return helper.Result(true, res, "")
}
Пример #27
0
func (t *GridController) SaveJsonGrid(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson
	filename := t.AppViewsPath + "data/mapgrid.json"
	f, err := os.Open(filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	mapgrid := []m.MapGrid{}
	jsonParser := json.NewDecoder(f)
	if err = jsonParser.Decode(&mapgrid); err != nil {
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}
	datagrid := m.Grid{}
	r.GetPayload(&datagrid)

	datamapgrid := m.DataMapGrid{}
	datamapgrid.Name = datagrid.Outsider.Title
	if datagrid.Outsider.IdGrid == "" {
		mapgrid[0].Seq = mapgrid[0].Seq + 1
		datagrid.Outsider.IdGrid = "grid" + strconv.Itoa(mapgrid[0].Seq)
		datamapgrid.Value = "grid" + strconv.Itoa(mapgrid[0].Seq) + ".json"
		datamapgrid.ID = "grid" + strconv.Itoa(mapgrid[0].Seq)
		mapgrid[0].Data = append(mapgrid[0].Data, datamapgrid)
	} else {
		newGrid := []m.DataMapGrid{}
		for _, eachRaw := range mapgrid[0].Data {
			if eachRaw.Value == datagrid.Outsider.IdGrid+".json" {
				eachRaw.Name = datamapgrid.Name
			}
			newGrid = append(newGrid, eachRaw)
		}
		mapgrid[0].Data = newGrid
	}

	b, err := json.Marshal(mapgrid)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	f, err = os.Create(filename)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_, err = io.WriteString(f, string(b))
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	f, err = os.Create(t.AppViewsPath + "data/grid/" + datagrid.Outsider.IdGrid + ".json")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	b, err = json.Marshal(datagrid)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	_, err = io.WriteString(f, "["+string(b)+"]")
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	defer f.Close()

	return helper.Result(true, datagrid, "")
}
Пример #28
0
func (t *DesignerController) GetWidgets(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]string{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if payload["type"] == "chart" {
		bytes, err := ioutil.ReadFile(t.AppViewsPath + "data/chart.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		data := []map[string]interface{}{}
		err = json.Unmarshal(bytes, &data)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, data, "")
	} else if payload["type"] == "grid" {
		bytes, err := ioutil.ReadFile(t.AppViewsPath + "data/mapgrid.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		data := []map[string]interface{}{}
		err = json.Unmarshal(bytes, &data)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, data[0]["data"], "")
	} else if payload["type"] == "selector" {
		bytes, err := ioutil.ReadFile(t.AppViewsPath + "data/selector.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		data := []map[string]interface{}{}
		err = json.Unmarshal(bytes, &data)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, data, "")
	}

	return helper.Result(true, []map[string]interface{}{}, "")
}
Пример #29
0
func (t *DesignerController) GetWidget(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]interface{}{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}

	if payload["type"].(string) == "chart" {
		bytes, err := ioutil.ReadFile(t.AppViewsPath + "data/chart/chart-" + payload["widgetID"].(string) + ".json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		data := map[string]interface{}{}
		err = json.Unmarshal(bytes, &data)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, data, "")
	} else if payload["type"].(string) == "grid" {
		connection, err := helper.LoadConfig(t.AppViewsPath + "data/grid/" + payload["widgetID"].(string) + ".json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		cursor, err := connection.NewQuery().Select("*").Cursor(nil)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer cursor.Close()

		dataSource, err := cursor.Fetch(nil, 0, false)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, dataSource.Data, "")
	} else if payload["type"] == "selector" {
		connection, err := helper.LoadConfig(t.AppViewsPath + "data/selector.json")
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer connection.Close()

		cursor, err := connection.NewQuery().Where(dbox.Eq("_id", payload["widgetID"].(string))).Cursor(nil)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
		defer cursor.Close()

		dataSource, err := cursor.Fetch(nil, 0, false)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}

		return helper.Result(true, dataSource.Data, "")
	}

	return helper.Result(true, map[string]interface{}{}, "")
}
Пример #30
0
func (t *DesignerController) SaveWidget(r *knot.WebContext) interface{} {
	r.Config.OutputType = knot.OutputJson

	payload := map[string]interface{}{}
	err := r.GetForms(&payload)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	_id := payload["_id"].(string)
	panelWidgetID := payload["panelWidgetID"].(string)

	width := int(100)
	if _, ok := payload["width"]; ok {
		width = int(payload["width"].(float64))
	}

	height := int(400)
	if _, ok := payload["height"]; ok {
		height = int(payload["height"].(float64))
	}

	config, err := t.getConfig(_id)
	if !helper.HandleError(err) {
		return helper.Result(false, nil, err.Error())
	}
	content := config["content"].([]interface{})
	contentNew := map[string]interface{}{
		"panelWidgetID": panelWidgetID,
		"dataSource":    payload["dataSource"].(string),
		"title":         payload["title"].(string),
		"type":          payload["type"].(string),
		"widgetID":      payload["widgetID"].(string),
		"width":         width,
		"height":        height,
	}

	if panelWidgetID == "" {
		panelWidgetID = helper.RandomIDWithPrefix("pw")
		contentNew["panelWidgetID"] = panelWidgetID

		for i, eachRaw := range content {
			each := eachRaw.(map[string]interface{})
			if each["panelID"] == payload["panelID"] {
				each["content"] = append([]interface{}{contentNew}, each["content"].([]interface{})...)
			}

			config["content"].([]interface{})[i] = each
		}

		err = t.setConfig(_id, config)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	} else {
	outer:
		for i, eachRaw := range content {
			each := eachRaw.(map[string]interface{})
			if each["panelID"] == payload["panelID"] {
				for j, subRaw := range each["content"].([]interface{}) {
					sub := subRaw.(map[string]interface{})
					if sub["panelWidgetID"] == panelWidgetID {
						content[i].(map[string]interface{})["content"].([]interface{})[j] = contentNew
						break outer
					}
				}
			}
		}

		config["content"] = content
		err = t.setConfig(_id, config)
		if !helper.HandleError(err) {
			return helper.Result(false, nil, err.Error())
		}
	}

	return helper.Result(true, nil, "")
}