Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DevExpress.Web.ASPxCallback
Imports DevExpress.Web.ASPxClasses
Imports DevExpress.Web.ASPxDataView
Imports DevExpress.Web.ASPxEditors
Imports DevExpress.Web.ASPxSplitter
Imports DevExpress.Web.ASPxUploadControl
Partial Public Class Contacts
Inherits System.Web.UI.Page
Protected ReadOnly Property SearchText() As String
Get
Return Utils.GetSearchText(Me)
End Get
End Property
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
Utils.ApplyTheme(Me)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If (Not IsPostBack) Then
ContactForm.Visible = False
End If
BindDataView()
PrepareMasterSplitter()
End Sub
Protected Sub ContactCountryEditor_Load(ByVal sender As Object, ByVal e As EventArgs)
If ContactFormPanel.IsCallback OrElse IsPostBack AndAlso (Not IsCallback) Then
Dim combo = CType(sender, ASPxComboBox)
combo.DataSource = CountryDataSource
combo.DataBindItems()
End If
ContactDataView.ContentStyle.Border.BorderWidth = 0
End Sub
Protected Sub ContactCityEditor_Callback(ByVal sender As Object, ByVal e As CallbackEventArgsBase)
If String.IsNullOrEmpty(e.Parameter) Then
Return
End If
Dim combo = CType(sender, ASPxComboBox)
CitiesDataSource.SelectParameters(0).DefaultValue = e.Parameter
combo.DataSource = CitiesDataSource
combo.DataBindItems()
End Sub
Protected Sub ContactDataView_CustomCallback(ByVal sender As Object, ByVal e As CallbackEventArgsBase)
If String.IsNullOrEmpty(e.Parameter) Then
Return
End If
Dim args = e.Parameter.Split("|"c)
If args(0) = "Delete" AndAlso args.Length = 2 Then
Dim id As Integer
If (Not Integer.TryParse(args(1), id)) Then
Return
End If
DemoModel.DataProvider.DeleteContact(id)
BindDataView()
End If
If args(0) = "SaveContact" Then
Dim name = ContactNameEditor.Text
Dim email = ContactEmailEditor.Text
Dim address = ContactAddressEditor.Text
Dim country = ContactCountryEditor.Text
Dim city = ContactCityEditor.Text
Dim phone = ContactPhoneEditor.Text
Dim photoUrl = Utils.GetUploadedPhotoUrl(args(2))
Dim id As Integer
If args.Length = 4 AndAlso args(1) = "Edit" AndAlso Integer.TryParse(args(3), id) Then
DemoModel.DataProvider.UpdateContact(id, name, email, address, country, city, phone, photoUrl)
ElseIf args.Length = 3 AndAlso args(1) = "New" Then
DemoModel.DataProvider.AddContact(name, email, address, country, city, photoUrl, photoUrl)
End If
BindDataView()
End If
End Sub
Protected Sub CallbackControl_Callback(ByVal sender As Object, ByVal e As CallbackEventArgs)
Dim args = e.Parameter.Split("|"c)
If args(0) = "Edit" AndAlso args.Length = 2 Then
Dim id As Integer
If (Not Integer.TryParse(args(1), id)) Then
e.Result = "NotFound"
Return
End If
Dim contact = DemoModel.DataProvider.Contacts.FirstOrDefault(Function(c) c.ID = id)
If contact Is Nothing Then
e.Result = "NotFound"
Return
End If
Dim dict = New Dictionary(Of String, Object)()
dict("Name") = contact.Name
dict("Email") = contact.Email
dict("Address") = contact.Address
dict("City") = contact.City
dict("Country") = contact.Country
dict("Phone") = contact.Phone
dict("ImageUrl") = Utils.GetContactPhotoUrl(contact.PhotoUrl)
CallbackControl.JSProperties("cpContact") = dict
e.Result = "Edit"
End If
End Sub
Protected Sub ContactPhotoImage_CustomJsProperties(ByVal sender As Object, ByVal e As CustomJSPropertiesEventArgs)
e.Properties("cpEmptyImageUrl") = Utils.GetContactPhotoUrl(String.Empty)
End Sub
Private Sub BindDataView()
ContactDataView.DataSource = SelectContacts()
ContactDataView.DataBind()
End Sub
Private Function SelectContacts() As List(Of IContact)
Dim result = DemoModel.DataProvider.Contacts.AsQueryable()
Dim showCollectedAdresses = Convert.ToInt32(FindAddressBookList().Value) = 1
result = result.Where(Function(c) Object.Equals(c.Collected, showCollectedAdresses))
If (Not String.IsNullOrEmpty(SearchText)) Then
Dim text = SearchText.ToLower()
result = result.Where(Function(c) c.Name.ToLower().Contains(text) OrElse Utils.GetAddressString(c).ToLower().Contains(text))
End If
Dim sortedFieldName = FindSortByCombo().Value.ToString()
Dim isDescending = Convert.ToInt32(FindSortDirectionCombo().Value) = 1
result = Utils.MakeContactsOrderBy(result, sortedFieldName, isDescending)
Return result.ToList()
End Function
Protected Function GetName(ByVal container As DataViewItemTemplateContainer) As String
Dim contact = CType(container.DataItem, IContact)
Return HighlightText(contact.Name)
End Function
Protected Function GetEmail(ByVal container As DataViewItemTemplateContainer) As String
Dim contact = CType(container.DataItem, IContact)
Return HighlightText(contact.Email)
End Function
Protected Function GetAddress(ByVal container As DataViewItemTemplateContainer) As String
Dim contact = CType(container.DataItem, IContact)
Return HighlightText(Utils.GetAddressString(contact))
End Function
Protected Function HighlightText(ByVal text As String) As String
If String.IsNullOrEmpty(SearchText) Then
Return text
End If
Return New Regex(SearchText, RegexOptions.IgnoreCase).Replace(text, "<span class='hgl'>$0</span>")
End Function
Private Function FindAddressBookList() As ASPxRadioButtonList
Return TryCast(ContactViewBar.Groups.FindByName("AddressBooks").FindControl("AddressBookList"), ASPxRadioButtonList)
End Function
Private Function FindSortByCombo() As ASPxComboBox
Return TryCast(ContactViewBar.Groups.FindByName("Sort").FindControl("SortByCombo"), ASPxComboBox)
End Function
Private Function FindSortDirectionCombo() As ASPxComboBox
Return TryCast(ContactViewBar.Groups.FindByName("Sort").FindControl("SortDirectionCombo"), ASPxComboBox)
End Function
Protected Function GetContactImageUrl(ByVal container As DataViewItemTemplateContainer) As String
Dim contact = CType(container.DataItem, IContact)
Return Utils.GetContactPhotoUrl(contact.PhotoUrl)
End Function
Protected Function HasAddress(ByVal container As DataViewItemTemplateContainer) As Boolean
Dim contact = CType(container.DataItem, IContact)
If String.IsNullOrEmpty(contact.Address) AndAlso String.IsNullOrEmpty(contact.City) AndAlso String.IsNullOrEmpty(contact.Country) Then
Return False
End If
Return True
End Function
Protected Function HasPhone(ByVal container As DataViewItemTemplateContainer) As Boolean
Dim contact = CType(container.DataItem, IContact)
Return Not String.IsNullOrEmpty(contact.Phone)
End Function
Protected Sub EditContactImage_Load(ByVal sender As Object, ByVal e As EventArgs)
PrepareContactCommandImage(CType(sender, ASPxImage))
End Sub
Protected Sub DeleteContactImage_Load(ByVal sender As Object, ByVal e As EventArgs)
PrepareContactCommandImage(CType(sender, ASPxImage))
End Sub
Protected Sub PrepareContactCommandImage(ByVal image As ASPxImage)
Dim container = CType(image.NamingContainer, DataViewItemTemplateContainer)
Dim contact = CType(container.DataItem, IContact)
image.JSProperties("cpContactKey") = contact.ID
End Sub
Protected Sub ContactPhotoUpload_FileUploadComplete(ByVal sender As Object, ByVal e As FileUploadCompleteEventArgs)
Dim uploadControl = CType(sender, ASPxUploadControl)
If (Not e.UploadedFile.IsValid) Then
Return
End If
Dim imageKey As Guid
Dim path = Utils.SaveContactPhoto(e.UploadedFile.FileContent, imageKey)
e.CallbackData = String.Format("{0}|{1}", path, imageKey)
End Sub
Private Sub PrepareMasterSplitter()
Dim rootHolder = TryCast(Page.Master.Master.FindControl("RootHolder"), ContentPlaceHolder)
Dim splitter = TryCast(rootHolder.FindControl("LayoutSplitter"), ASPxSplitter)
splitter.GetPaneByName("MainPane").ScrollBars = ScrollBars.Auto
End Sub
End Class