1 /**
2 * Copyright © Webd 2018
3 * License: MIT (https://github.com/DiamondMVC/Webd/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module webd.web.loop;
7 
8 import webd.web.tagcollection;
9 
10 /// Wrapper representing a loop of tags.
11 struct Loop
12 {
13   private:
14   /// The original collection of tags.
15   TagCollection!()[] _original;
16   /// A slice of the tags currently in the loop.
17   TagCollection!()[] _items;
18 
19   public:
20   @property
21   {
22     /// Gets the amount of items in the loop.
23     size_t length() { return _original ? _original.length : 0; }
24   }
25 
26   /// Boolean determining whether the loop is empty or not.
27   bool empty()
28   {
29     return !_original || _original.length == 0 || (_items &&  !_items.length);
30   }
31 
32   /// Gets the current item of the loop.
33   TagCollection!() front()
34   {
35     if (!_items)
36     {
37       _items = _original;
38     }
39 
40     return _items[0];
41   }
42 
43   /// Pops the next item of the loop.
44   void popFront()
45   {
46     _items = _items[1 .. $];
47   }
48 
49   /// Adds an item to the loop.
50   void add(TagCollection!() tags)
51   {
52     _original ~= tags;
53   }
54 
55   /// Duplicates the original loop.
56   Loop dup()
57   {
58     Loop loop;
59     loop._original = _original;
60 
61     return loop;
62   }
63 }